How do I reference a scalar in a hash reference in Perl?

Simple question:

How do I do this on one line:

my $foo = $bar->{baz};
fizz(\$foo);

I've tried \$bar->{baz}, \${$bar->{baz}}, and numerous others. Is this even possible?

-fREW

Update: Ok, the hashref is coming from DBI and I am passing the scalar ref into template toolkit. I guess now that I look more closely the issue is something to do with how TT does all of this. Effectively I want to say:

$template->process(\$row->{body}, $data);

But TT doesn't work that way, TT takes a scalar ref and puts the data there, so I'd have to do this:

$template->process(\$row->{body}, $shopdata, \$row->{data});

Anyway, thanks for the help. I'll at least only have one reference instead of two.

Answers


\$bar->{baz}

should work.

E.g.:

my $foo;
$foo->{bar} = 123;

my $bar = \$foo->{bar};

$$bar = 456;

print "$foo->{bar}\n";   # prints "456"

In answer to the update in the OP, you can do:

\@$row{qw(body data)};

This is not the same as \@array, which would create one reference to an array. The above will distribute the reference and make a list of two references.


Need Your Help

How to Map Result Set of Native Query to Variable in Entity

java spring hibernate jpa sqlresultsetmapping

I have two entities, Users and Annotations, and I want to add a set of results from a query on the Annotations table to a transient variable in the Users entity.