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 check if JSF resource exists before loading view

javascript jsf

In a project using JSF, we have JavaScript files located here:

I am trying to create an effect that when I hover into a div it would then effect all the children

html css hover children

I'm really new to HTML and CSS, I've been picking it up along the way as I've been creating my website. It's been going pretty well up until my portfolio page, what my problem is I've created a por...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.