in reply to Re^2: Elaborate Records, arrays and references
in thread Elaborate Records, arrays and references

Just solved my own question. Below is the code i used, if you have another suggestion (especially with out using a variable like $hashref) I would like to hear it.
use warnings; use strict; use Data::Dumper; sub test (\%); my %Record1 = ( username => '$' , password => '$' , emailAddress => '@' ); my $hashref = \%Record1; $hashref->{username} = 'NOOoooooo!'; &test( $hashref ); sub test (\%) { my $reference = shift; my $test = "Pigsy's Perfect Ten"; $reference->{username} = $test; print $reference->{username}, "\n"; } print $Record1{username}, "\n";

Replies are listed 'Best First'.
Re^4: Elaborate Records, arrays and references
by AnomalousMonk (Archbishop) on Jan 07, 2011 at 21:19 UTC

    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
Re^4: Elaborate Records, arrays and references
by Marshall (Canon) on Jan 14, 2011 at 15:24 UTC
    In C when you do a malloc(), you are getting a hunk of memory and assigning that address to a variable which points to the memory. Pretty much the same concept in Perl... my $hashref={}; e.g. allocate some memory and assign a reference (pointer) to it to $hashref. There is a kind of weird duality between arrays and hash tables and () would work here. But in general, {} is used for a new anon hash table memory allocation, and [] means a new array memory allocation.

    In Perl, there is no need to "define" how you are going to use some memory, you just start using it. A wild concept for a C programmer, but this is true. If some hash key doesn't already exist, like below (username), it will spring automagically into existence! This is called autovivification.

    Like in C you can pass a reference (like a pointer) to a sub as is done below. There is no need to prefix a subroutine call with &. You have been reading some truly ancient books! You should buy a new copy of "Programming Perl". There is no need and this is actually a bad idea to attempt to define prototypes for a function (subroutine). Below I call test() before it is "seen by the compiler", this is fine in Perl although that would cause big trouble in C. Of course I would not normally split code around a subroutine, but below it is done to show that it is possible. You can put sub test() anywhere in the file you want.

    #!/usr/bin/perl -w # the above (-w) turns on warnings, Windows ignores the path!! use strict; use Data::Dumper; my $hashref={}; #a reference to anon hash table $hashref->{username} = 'NOOoooooo!'; print "User Name: $hashref->{username}\n"; test( $hashref ); sub test { my $reference = shift; my $test = "Pigsy's Perfect Ten"; $reference->{username} = $test; print "Inside Sub Test test\'s value is: $reference->{username}\n" +; $reference->{'accountid'} = '234piggy'; } print "User Name: $hashref->{username}\n"; print "Account ID: $hashref->{'accountid'}\n"; __END__ Prints: User Name: NOOoooooo! Inside Sub Test test's value is: Pigsy's Perfect Ten User Name: Pigsy's Perfect Ten Account ID: 234piggy