Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Re: if only blocks were avoidable...

by yosefm (Friar)
on Sep 22, 2003 at 11:07 UTC ( #293108=note: print w/ replies, xml ) Need Help??

in reply to Re: if only blocks were avoidable...
in thread if only blocks were avoidable...

OK, it turns out that this isn't what I need, because if I use BEGIN, I can't use parameters passed to sub in order to decide on the condition. I can only use environment variables, perl variables and other stuff that already exists before anything happens.

What I need is this:

sub import { $_[1] eq 'StoneAge' and use bytes globally; #no such thing. }

BEGIN can't achieve that.

Replies are listed 'Best First'.
Re: Re: Re: if only blocks were avoidable...
by liz (Monsignor) on Sep 22, 2003 at 13:56 UTC
    Ok, I guess I missed that particular point. Well, I think it is possible, but it's rather yucky and it suffers from the same problems that AutoLoader and the likes have. Basically, you compile your code in a two step process. First to compile the "import" routine. Then, when the import routine is called, the rest of the source is compiled Consider module Foo:
    package Foo; require bytes; # make sure the module is loaded without (un)import $bytes = 0; # flag: whether bytes should be enforced $evalled = 0; # flag: whether rest of source has been compiled sub import { unless ($evalled++) { # we haven't evalled the rest +before $bytes = ($_[1] eq "usebytes"); # should bytes be enforced? local $/; # enable slurp mode eval <DATA>; # compile rest of source (afte +r __DATA__) die $@ if $@; # die now if an error occurred } }; 1; # in order for require of to be + successful __DATA__ # actual source of module starts here BEGIN {bytes->import if $bytes} # activate bytes if flag set sub new { bless {},shift } # example routine
    Then, in an example program, you would do:
    use Foo 'usebytes'; # or something else to not use bytes my $foo = Foo->new;
    Hope this will help you in your quest.


      Yes, that's exactly what I need. And it also shows a very interesting feature of perl - I never thought about evaling <DATA>, so ++. Also I always think of eval BLOCK while there's also an eval EXPR that I always forget.

        Note that a single script can't safely use your module twice. This means that any modules that try to use your module can't be safely used with any other such modules. I don't know what your module does, but it seems quite unfortunate to me.

        I'd probably have three packages. Here is a simplistic example:

        package Your::Module::Bytes; use bytes; do $INC{"Your/"} . ".pl"; 1;
        package Your::Module::Unicode; no bytes; do $INC{"Your/"} . ".pl"; 1;
        package Your::Module; sub import { # require the desired submodule # dispatch import to that submodule }

        Note, I'd have to test to make sure that 'use bytes' affects the code included via do, but I believe that it does.

        I'm also a bit curious what particular effect of 'use bytes' you need. I thought that such was mostly of use for the earliest versions of Perl Unicode support.

                        - tye

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://293108]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2016-07-26 16:06 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (236 votes). Check out past polls.