in reply to if only blocks were avoidable...

The use bytes is basically setting $^H at compile time.

According to that documentation, you should be able to do:

BEGIN { require bytes; bytes->import if $condition }
inside the block where you want the use bytes to be conditionally set. Of course, if you don't put it in a block, the file in which the source sits, will be your scope.

This seems the right thing according to the docs and seems to work with stricture:

$foo = $a; BEGIN { require strict; strict->import if 1 } $foo = $a; __END__ Variable "$foo" is not imported at x line 3. Global symbol "$foo" requires explicit package name at x line 3. x had compilation errors.
If the import is not done, this runs without any errors:
$foo = $a; BEGIN { require strict; strict->import if 0 } $foo = $a; __END__

Hope this made sense and helps.

Liz

Replies are listed 'Best First'.
Re: Re: if only blocks were avoidable...
by yosefm (Friar) on Sep 22, 2003 at 11:07 UTC
    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.

      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.
Re: Re: if only blocks were avoidable...
by yosefm (Friar) on Sep 22, 2003 at 09:22 UTC
    So if I understand what you (and perlvar) say correctly, the BEGIN block is what's going to make my import globally scoped? If that's the case then I guess that's what I was looking for.
    Thanks.
      No. Wrapping an import call in a BEGIN block will make it happen at compile time.