Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How to interpolate CONSTANTS in Here docs?

by BrowserUk (Patriarch)
on Feb 11, 2015 at 21:55 UTC ( [id://1116401]=note: print w/replies, xml ) Need Help??


in reply to How to interpolate CONSTANTS in Here docs?

C:\test>type junk94.pl #! perl -slw use strict; use constant { OUT_DIR => 'out', OUT_FIELDSEP => '=', OUT_FILENAME => 'ks', OUT_SUFXSTOR => 'storable', VAL_MIN => 15, VAL_MAX => 3600, }; print <<USAGE; This program does this and that with the @{[OUT_DIR]}. The filename is @{[OUT_FILENAME]} and the file suffix is @{[OUT_SUFXSTOR]}. Fields are separated with the @{[OUT_FIELDSEP]} character. Enter a val +ue between @{[VAL_MIN]} and @{[VAL_MAX]}. USAGE C:\test>junk94 This program does this and that with the out. The filename is ks and the file suffix is storable. Fields are separated with the = character. Enter a value between 15 and 3600.

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

Replies are listed 'Best First'.
Re^2: How to interpolate CONSTANTS in Here docs?
by CoVAX (Beadle) on Feb 11, 2015 at 22:32 UTC

    Thank you for your answer.

    I'm not understanding the @{[]} syntax, although I do recognize the array index [], the hash {}, and the array @ -- can you explain what is going on, please?

    Searched for donuts and crumpit. Found content instead.

      Anonymonk has explained the syntax; but I thought I'd add that I'm not keen on doing it that way. Personally I prefer:

      #! perl -slw use strict; use constant { OUT_DIR => 'out', OUT_FIELDSEP => '=', OUT_FILENAME => 'ks', OUT_SUFXSTOR => 'storable', VAL_MIN => 15, VAL_MAX => 3600, }; printf <<USAGE, OUT_DIR, OUT_FILENAME, OUT_SUFXSTOR, OUT_FIELDSEP, VAL +_MIN, VAL_MAX; This program does this and that with the %s. The filename is %s and the file suffix is %s. Fields are separated with the %s character. Enter a value between %s and %s. USAGE

      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

      [...] builds a reference to an anonymous array, which is in turn de-referenced by @{...}. It's roundabout, but necessary in this case because the constants built by constant are actually functions, like sub OUT_DIR () { 'out' }, and a function call like OUT_DIR() (or OUT_DIR) isn't interpolated in strings, while scalars ($foo) and arrays (@bar) are, including dereferenced arrayrefs (@{$arrayref} - see e.g. Using References).

      I'm not understanding the @{[]} syntax, although I do recognize the array index [], the hash {}, and the array @ -- can you explain what is going on, please?

      The array index isn't an array index, the hash isn't a hash.
      @{ } dereferences an array reference. ${ } dereferences a scalar reference. [ ] constructs an array refrence, \ constructs a scalar reference. So, instead of writing

      This program does this and that with the @{[OUT_DIR]}. The filename is @{[OUT_FILENAME]} and the file suffix is @{[OUT_SUFXSTOR]}. Fields are separated with the @{[OUT_FIELDSEP]} character. Enter a val +ue between @{[VAL_MIN]} and @{[VAL_MAX]}.

      as in BrowserUk's example, you could also write

      This program does this and that with the ${\OUT_DIR}. The filename is ${\OUT_FILENAME} and the file suffix is ${\OUT_SUFXSTOR}. Fields are separated with the ${\OUT_FIELDSEP} character. Enter a valu +e between ${\VAL_MIN} and ${\VAL_MAX}.

      The reference implementation in perl is the author's way of introducing ásbestos gélōs into the language, great fun and a good opportunity to distinguish yourself with expertise, once you grok cause and syntax.

      Quick, what does ~@~ mean?

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      If you spread it out,

      "foo @{[ F ]} bar"

      you get

      my @anon_array = F; my $anon_ref = \@anon_array; "foo @{ $anon_ref } bar"

      As you can see,

      • @BLOCK (@{ STATEMENTS }) is an array dereference, not "a hash".

      • [ LIST ] is an array constructor, not "an array index". It creates an array, assigns the result of LIST, and returns a reference to that array.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found