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

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.


Comment on Re: Re: if only blocks were avoidable...
Download Code
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 Foo.pm 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.

    Liz

      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.
      Thanks.

        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/Module.pm"} . ".pl"; 1;
        and
        package Your::Module::Unicode; no bytes; do $INC{"Your/Module.pm"} . ".pl"; 1;
        and
        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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-11-27 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (184 votes), past polls