[ILUG] Re: [Webdev] Perl references

Fergal Daly fergal at esatclear.ie
Mon Sep 25 23:27:46 IST 2000


Is params a ref to a hash or hash who's values are refs to scalars? If the 
former, then something is a little weird and you can ignore the rest of 
this, if the latter, try

perl -e '$params={moo => "baa"};print $$params{"moo"}."\n"'

vs

perl -e '%params=(moo => "baa");print $$params{"moo"}."\n"'

vs

perl -e '%params=(moo => \"baa");print $$params{"moo"}."\n"'

the first gives 'baa' whereas the second and third give blankness. I think 
it's because dereferencing has a higher precedence than hash lookup (check 
the man page to find out for sure). So perl evaluates $$params first then 
looks up the key moo in the resulting hash. You want

perl -e '%params=(moo => \"baa");print ${$params{"moo"}}."\n"'

to look up moo then dereference the resulting scalar. Sticking a

use strict;

at the top of your code will stop this sort of thing happening,

Fergal





More information about the ILUG mailing list