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.
In reply to Re: Scope of lexical variables in the main script
by GrandFather
in thread Scope of lexical variables in the main script
by sophate
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |