Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to interpolate CONSTANTS in Here docs?

by Anonymous Monk
on Feb 11, 2015 at 23:19 UTC ( #1116407=note: print w/replies, xml ) Need Help??


in reply to How to interpolate CONSTANTS in Here docs?

It might be noteworthy that Conway's Perl Best Practices recommends against constant, partly for exactly the reason you're having trouble (they don't interpolate) - a similar issue arises when they're used in hashes, as documented in "Caveats" in constant.

The suggested alternative is Readonly, but it has a few bugs, and I've found Readonly is not really necessary if you just stick to not modifying variables whose names are in $ALL_CAPS (people with a more defensive coding philosophy might disagree).

Of course you are free to disagree with PBP on this point and use constant, in which case I think the solutions given by AnomalousMonk and BrowserUk above are preferable to the following, because they catch errors due to typos in the constant names better. But since this is Perl, and in the spirit of TIMTOWTDI, here's another solution:

my $USAGE=<<'USAGE'; Fields are separated with the OUT_FIELDSEP character. Enter a value between VAL_MIN and VAL_MAX. USAGE $USAGE=~s/\b\Q$_\E\b/$_/ee for qw/ OUT_FIELDSEP VAL_MIN VAL_MAX /;

Replies are listed 'Best First'.
Re^2: How to interpolate CONSTANTS in Here docs?
by BrowserUk (Pope) on Feb 14, 2015 at 21:12 UTC
    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.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      That damn book has done infinitely more harm than good. (As predicted!)

      I can't gainsay; and I wouldn't if I could.

      Write-once variables are a piss poor substitute; an anathema to Perl; and an abomination.

      True, true and true. We would upvote all of this if I were four of me.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^2: How to interpolate CONSTANTS in Here docs?
by SankoR (Prior) on Feb 14, 2015 at 20:26 UTC
    FFS, please report these 'bugs' on the tracker. I've cleared up all the valid issues and sped it up to the point that Readonly::XS can be withered. If you've found something new, the only way to get it fixed is to report it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1116407]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2020-09-27 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?