It might be noteworthy that Conway's Perl Best Practices recommends against constant,
Grrr. That damn book has done infinitely more harm than good. (As predicted!)
It completely misses the point of constant; namely that it works with Perl to optimise your code.
Because constants are defined as invariant subroutines, Perl can optimise away whole chunks of code at compile time:
C:\test>perl -MO=Deparse -Mconstant=DEBUG,1 -le"print 'hi'; DEBUG and +print 'here'; print 'bye'" BEGIN { $/ = "\n"; $\ = "\n"; } use constant (split(/,/, 'DEBUG,1', 0)); print 'hi'; print 'here'; ## When DEBUG is defined; it doesn't need to be teste +d for at runtime. (No conditional!) print 'bye'; -e syntax OK C:\test>perl -MO=Deparse -Mconstant=DEBUG,0 -le"print 'hi'; DEBUG and +print 'here'; print 'bye'" BEGIN { $/ = "\n"; $\ = "\n"; } use constant (split(/,/, 'DEBUG,0', 0)); print 'hi'; '???'; ## And when it's not; both the conditional and the cod +e it controls are simply optimised away. print 'bye'; -e syntax OK
Write-once variables are a piss poor substitute; an anathema to Perl; and an abomination.
In reply to Re^2: How to interpolate CONSTANTS in Here docs?
by BrowserUk
in thread How to interpolate CONSTANTS in Here docs?
by CoVAX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |