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.