http://www.perlmonks.org?node_id=1187147


in reply to Re^2: Syntax for casting map to a hash or an array
in thread Syntax for casting map to a hash or an array

Although it is technically a block, it's really an operator. %{} and @{} are often referred to as the "circumfix operators". They are available wholly to dereference what is within the block to whatever sigil is placed before it.

Replies are listed 'Best First'.
Re^4: Syntax for casting map to a hash or an array
by cbeckley (Curate) on Apr 05, 2017 at 16:09 UTC

    Ah, more light bulbs. Thank you.

    And in my trivial example, these work:

    say Dumper({map { $_ => 'fish' } qw(one two red blue)}); say Dumper([map { $_ => 'fish' } qw(one two red blue)]);
    but I'm not sure I would have had either light bulb go off if I'd started there. Learning is strange trip. Oh, wait, maybe that's just me.

    Thanks,
    cbeckley

      Yep, both of those work, however not for the same reason.

      Dumper takes a reference of any type.

      This:

      {map { $_ => 'fish' } qw(one two red blue)}

      ... takes whatever is compiled by map, then with the outer braces, converts that into a hash reference, then returns the hash ref (same for the next one with an array). Putting the sigil in front of that block, and it will deref the created reference into a hash, and return it as a hash as opposed to a reference. For Dumper, you'd then need to dereference it before sending it in as a parameter.

        Wait, what part works, but for not the same reason?

        These two work for exactly the same reason.

        say Dumper({map { $_ => 'fish' } qw(one two red blue)}); say Dumper([map { $_ => 'fish' } qw(one two red blue)]);

        These two work but NOT for the same reason, because there's an extra layer of ref->hash->ref going on?
        That's the difference you're talking about, right?

        say Dumper(\@{ {map { $_ => 'fish' } qw(one two red blue)} } ); say Dumper({map { $_ => 'fish' } qw(one two red blue)});

        Thanks,
        cbeckley