NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
global bad, lexical good (Score:1)
And preferred use of ....
my(orlocalfor certain built-ins) is repeated in a dozen or more specific contexts like filehandeles, builtin vars,Bill
# I had a sig when sigs were cool
use Sig;
Re: (Score:1)
I'm not referring to lexical variables (
my), as you pointed out that part is covered by the best practices. I was referring to writing a single script from the top to the bottom with out localizing all variables. Remember that my only declares the variable for the life time of the current block. If the variable is not declared in a block then the file is the block scope, making the variable global.Re:global bad, lexical good (Score:1)
hear hear!
Whenever I encounter a substantial (>100 lines) perl script with no main sub I get nervous.
A quick, one-off script may not require the extra structure and discipline, but we all know how easily a one-off can become mission-critical... and quickly grow in size scope and function.
In my opinion, file-scoped vars are nearly as bad as true globals, and packaged-scoped vars less-so, but still best to avoid, unless the alternative is sufficiently more complicated code somewhere else.
By using a main block/sub you make it much clearer each time a file, package, or global var is declared/defined, plus code that is intended to execute only once is also clearer.
In the end, the maintenance programmer will thank you for confining as much code as possible to lexical blocks. Always think of the maintenance programmer... remember, it may be YOU... or a violent psychopath who knows where you live.
Overall, the only reason I can think of to *not* use a main sub (and therefore minimize file/package/globals) is in the interest of the bad kind of laziness, the kind that saves a second now because of not caring for later.
Reply to This
Parent