in reply to Re^3: Data structure / hash reference question
in thread Data structure / hash reference question

You were close with the first one - you just didn't need the dereferencing arrow ->. This should work:

$color = ${ ... }{color};

Why? Because if ... is a hashref, then %{...} is a plain old hash.

If our hash was called %foo then we'd access a value in it as $foo{color} - that is, change the sigil from % to $, and add the key in curlies. Our hash is called %{...} but we can still use the same technique.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^5: Data structure / hash reference question
by stevieb (Canon) on Apr 22, 2012 at 13:10 UTC

    I was curious as to how this could work, so I got playing around.

    I forgot that you could substitute the -> deref operator for ${}

    perl -E '$h={a=>1,b=>2}; say $h->{a}'

    ...is the same as:

    perl -E '$h={a=>1,b=>2}; say ${$h}{a}'

    In roboticus' attempt that you pointed out, you'd still need to use the parens inside of the deref block, or else perl will try to use the bareword 'first' as the name of the hash instead of evaluating the expression to its final value (I only found this after testing it):

    my $color = ${( first { $class =~ /$_->{'re'}/ } @rules )}{ color };