Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: How to interpolate CONSTANTS in Here docs?

by CoVAX (Beadle)
on Feb 11, 2015 at 22:32 UTC ( [id://1116403]=note: print w/replies, xml ) Need Help??


in reply to Re: How to interpolate CONSTANTS in Here docs?
in thread How to interpolate CONSTANTS in Here docs?

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.
  • Comment on Re^2: How to interpolate CONSTANTS in Here docs?

Replies are listed 'Best First'.
Re^3: How to interpolate CONSTANTS in Here docs?
by BrowserUk (Patriarch) on Feb 11, 2015 at 23:16 UTC

    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
Re^3: How to interpolate CONSTANTS in Here docs?
by Anonymous Monk on Feb 11, 2015 at 22:49 UTC

    [...] 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).

Re^3: How to interpolate CONSTANTS in Here docs?
by shmem (Chancellor) on Feb 14, 2015 at 22:09 UTC
    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'
Re^3: How to interpolate CONSTANTS in Here docs?
by ikegami (Patriarch) on Feb 16, 2015 at 02:15 UTC

    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://1116403]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-23 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found