Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Returning information from BEGIN

by dash2 (Hermit)
on Feb 21, 2001 at 20:06 UTC ( [id://59950]=note: print w/replies, xml ) Need Help??


in reply to Returning information from BEGIN

Not quite sure what the problem is - your BEGIN block gets run first, sets $Err if needed, then your program dies if $err is set. WFM.

Anyway, AFAIK everything gets "exported" from a BEGIN block - except "my" variables, obviously. In that respect it is just like a normal block. It just runs earlier. "eval" is kind of the same, context remains, it just gets run later.

Could you explain a bit more what you're trying to do?

Dave

Replies are listed 'Best First'.
Re: Re: Returning information from BEGIN
by leons (Pilgrim) on Feb 21, 2001 at 20:50 UTC
    Thanks! Yep, that was exactly what the problem was ... the "my"
    Which explains why this worked:

    BEGIN {$foo="hiya"}; print $foo."\n";

    Okay, feeling a bit silly, I thank you both and will continue
    writing this program after I drank a whole lotta coffee ;-)

    Bye, Leon

      Note that there is no problem with:

      my $Err; BEGIN { $Err= defined $ENV{SM} }

      Sure, the BEGIN happens at compile time but so does the declaration part of the my code. use strict didn't give you an error, did it? That is because $Err is getting declared at compile time before the BEGIN block is executed.

      What would be a "problem" is code like this:

      my $Err= 0; BEGIN { $Err= 1 if ! $ENV{SM} }
      because the my statement gets executed in two parts. The declaration part gets executed at compile time while the initialization part gets executed at run time. So the order or execution ends up looking more like this:
      my $Err; $Err= 1 if ! $ENV{SM}; $Err= 0;
      O-:

      Personally I prefer the my solution to using use vars inside the BEGIN block.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-21 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found