I always use a BEGIN block to initialize "static" variables:
#!/usr/bin/perl -w use strict; PutFoo(); my $foo; BEGIN { $foo = "Goo"; } PutFoo(); sub PutFoo { print "Boo hoo, foo is $foo\n"; }
As the declaration of $foo happens at compile time so I strongly feel that you should make the initialization also happen at compile time. This fixes your problem.
What is happening is that Perl first runs through the file once (compile time) and compiles all of the code and executes key bits that are required at compile time. The key bits include BEGIN blocks, use statements, and declaration of lexical variables. Then the compiled code is executed (run time) which usually behaves like Perl running through the file a second time executing the bits it didn't execute the first time.
- tye (but my friends call me "Tye")In reply to (tye)Re: Perl Turning a Blind Eye
by tye
in thread Perl Turning a Blind Eye
by tadman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |