You are having a problem because you are defining the function test as a prototyped function and then invoking it in a way that disables prototype behavior.
Had the function declared and defined with the (\%) prototype
sub test (\%);
...
sub test (\%) { ... }
been invoked as
test(%Record1);
a hash reference would have been passed to the function. Invocation as
&test(%Record1);
(note the & sigil in front of test) causes the %Record1 hash to be 'flattened' (the prototyped behavior of taking a reference to an explicit hash is ignored) and the first parameter in the argument list happens, in the example given, to be the string 'password', which doesn't work as a hash reference. If an explicit hash reference is passed, e.g.
&test($hashref);
everything works again, but then
test($hashref);
(note no & sigil) doesn't work!
The take-away lesson: Don't use prototypes unless you really understand what they do and really need that to be done.
Update: See Prototypes in perlsub (and, for good measure, Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen).
Update: Added code example:
>perl -wMstrict -le "sub test (\%); ;; my %Record1 = ( username => '$' , password => '$' , emailAddress => '@' ); my $hashref = \%Record1; ;; print $Record1{username}; $hashref->{username} = 'NOOoooooo!'; print $Record1{username}; test(%Record1); print $Record1{username}; ;; sub test (\%) { my $reference = shift; my $test = q{Pigsy's Perfect Ten}; $reference->{username} = $test; print 'in test(): ', $reference->{username}; } " $ NOOoooooo! in test(): Pigsy's Perfect Ten Pigsy's Perfect Ten
In reply to Re^4: Elaborate Records, arrays and references
by AnomalousMonk
in thread Elaborate Records, arrays and references
by KyussRyn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |