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

I understand what ikegami and Marshall are saying but now I am seeking clarification on how it should be done in perl. In C what I would usually do is:
typedef struct s_USERINFO{ CString m_username; CString m_password; CString m_emailAddress[20]; } USERINFO; int main () { USERINFO currentUserInfo; test( currentUserInfo ); printf ("%s", currentUserInfo.m_username); } void test ( USERINFO* thisUserInfo ) { thisUserInfo->m_username = "Pigsy's Perfect Ten"; }
What I want to do is update the struct/hash in the subroutine/function and then be able to access the updated value in the main thread. In Perl my assumption on how to do this is:
use warnings; use strict; use Data::Dumper; sub test (\%); #no strict 'refs'; my $username = "PerlMonk"; my $password = "VeryGoodPassword"; my @email_addresses = ( "Monk1", "Monk2", "Monk3" ); my %Record1 = ( username => '$' , password => $password , emailAddress => [@email_addresses] ); $Record1{username} = 'NOOoooooo!'; &test( %Record1 ); sub test (\%) { my $reference = shift; my $test = "Pigsy's Perfect Ten"; $$reference->{username} = $test; print $$reference->{username}, "\n"; } print $Record1{username}, "\n";
So what I am hoping to have as an outcome of this is for the final print command to print out "Pigsy's Perfect Ten". I have two problems with this, I keep getting 'NOOoooooo!' printed out, and also I am getting the error "Can't use string ("password") as a HASH ref while "strict refs" in use at C:/xxx/test.pl line 24 ( &test( %Record1 );)." With both problems I am assuming there are problems with my syntax that I just can't figure out. Any help is appreciated.

Replies are listed 'Best First'.
Re^3: Elaborate Records, arrays and references
by KyussRyn (Acolyte) on Jan 07, 2011 at 06:14 UTC
    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";

      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 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