in reply to Re: Re: Re: Aborting loading of file in a BEGIN block
in thread Aborting loading of file in a BEGIN block

Thanks - that's great. I can see what I want to do now. Actually it's the require STRING flavour I'm dealing with, so it's maybe a bit easier than I thought. All I want to do is to make sure the subs using Maybe::Module don't blow up when called. I'm doing something like:

my $no_go; BEGIN { eval { require Maybe::Module }; if ( $@ ) { $no_go = 1 } } sub my_function { print "I'm the normal definition"; } # ... if ( $no_go ) { # safe definition *my_function = sub { carp "Function not available without Maybe::M +odule" }; } 1;

This works nicely for me; as an aside, I wonder why the BEGIN block can see the my $no_go? I would have expected the BEGIN to run first and so to complain about $no_go failing strict;

Thanks again
ViceRaid

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Aborting loading of file in a BEGIN block
by broquaint (Abbot) on Aug 15, 2003 at 17:05 UTC
    The BEGIN block can see $no_go because lexical scoping is done at compile-time whereas the initialisation of the variables isn't done 'til run-time e.g
    use strict; my $foo = "defined"; BEGIN { print "foo is ", defined($foo) ? $foo : 'undef', " during BEGIN phase\n"; }; print "foo is ", defined($foo) ? $foo : undef, " at runtime\n"; __output__ foo is undef during BEGIN phase foo is defined at runtime
    See. the my variables section of Lexical scoping like a fox for more details.
    HTH

    _________
    broquaint