in reply to Scope of lexical variables in the main script
Perl doesn't treat lexical variables declared in the outer most block (at "file level") any different than lexical variables declared in a nested block. The scope for lexical variables is from their point of declaration to the end of the enclosing block. For file level variables that is to the end of the file.
Such variables are often known as global variables and should generally be avoided. It is common to use a run sub to ensure there are no unintentional global variables:
#!/usr/bin/perl use strict; use warnings; run(); sub run { my $tricky = "in run"; test_sub(); } ...
By the way using & to call subs is rather old school Perl style and probably isn't doing what you expect. Instead use the subname(); style shown above.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Scope of lexical variables in the main script
by sophate (Beadle) on Apr 20, 2012 at 05:52 UTC | |
by davido (Cardinal) on Apr 20, 2012 at 05:58 UTC | |
by Marshall (Canon) on Apr 20, 2012 at 06:33 UTC | |
by sophate (Beadle) on Apr 20, 2012 at 06:53 UTC | |
by Anonymous Monk on Apr 20, 2012 at 06:07 UTC | |
by nemesdani (Friar) on Apr 20, 2012 at 06:58 UTC |