Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Check if key exist in hash but not exact match

by ibm1620 (Hermit)
on Apr 04, 2023 at 21:36 UTC ( [id://11151472]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Check if key exist in hash but not exact match
in thread Check if key exist in hash but not exact match

my $fmt = "%-7s %-@{[max map length, keys %$hash]}s ";
Definitely adding this to my snippet file! But I've never seen this syntax before. What is triggering the code evaluation of max(...) here?

Replies are listed 'Best First'.
Re^5: Check if key exist in hash but not exact match ( meaning of " @{[...]} " )
by LanX (Saint) on Apr 04, 2023 at 21:51 UTC
    > What is triggering the code evaluation of max(...) here?

    "  @{[...]}  " is an idiomatic pseudo-operator by combining several mechanisms

    • String interpolation work not only for arrays but also for dereferencing of arrays.

      " @{$a_ref} "

    • The [...] marks an anonymous array-ref.

      " @{[ 1, 2, 3 ]} "

    • Lists can also include code statements to be executed

      " @{[ 1, 2, some_code() ]} "

    • The returned list will be included into the string.

      DB<6> p ">>> @{[ 1, 2, time() ]} <<<" >>> 1 2 1680647910 <<< DB<7>

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

    update

    added code examples for each step

Re^5: Check if key exist in hash but not exact match
by eyepopslikeamosquito (Archbishop) on Apr 04, 2023 at 22:23 UTC
Re^5: Check if key exist in hash but not exact match
by kcott (Archbishop) on Apr 04, 2023 at 22:41 UTC

    G'day ibm1620,

    The @{[...]} construct is one of many that are referred to as "Perl secret operators and constants".

    This one, called "Baby cart", has been around for a very long time. Follow that link for an answer to your question as well as other information.

    I have been using it since the 1990s and, as I recall, wasn't even aware that it was a perlsecret until much later. I think I first encountered it in one of the "O'Reilly Perl books". I don't remember which one; although, "Advanced Perl Programming" is a likely candidate. Note that's the original edition; "Advanced Perl Programming, 2nd edition" is completely different — a very poor choice of title in my opinion.

    — Ken

      *contented sigh*

      I love Perl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2025-07-17 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.