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.
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
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.
| [reply] [d/l] [select] |
\@{ [map { $_ => 'fish' } qw(one two red blue)] }
Well, that works but it's a bit wasteful: first you're creating a arrayref with [], then you're dereferencing it with @{}, and then taking a reference to that again with \. It's easier to just use one set of []'s like 1nickt showed (although you may not have seen that yet since it was part of a large ninja edit).
| [reply] [d/l] [select] |
%{ {map { $_ => 'fish' } qw(one two red blue)} }
returned a hash ...
And I knew Dumper needed a reference ...
And I suspected that if I could successfully pass Dumper a reference to an array I might finally understand what was going on with
%{ { ... } }
that bit.
Thank you very much.
Thanks,
cbeckley
| [reply] [d/l] [select] |
Nope, one level too many of referencing / deferencing. See my example above.
With this kind of thing it's best to strip it down to the innermost element and print its output with a data dumper. Then when you understand what is going on, add a layer, repeat. Trying to get at it from the outer edge when you are confused is a bit like flailing. A methodical approach is most effective.
The way forward always starts with a minimal test.
| [reply] |