in reply to Elaborate Records, arrays and references

The closest thing in Perl to the C "array of struct" is a Perl "Array of Hash".

Just like in C, normally you want to make "deep copies" of each "element of the struct". In C, you could perhaps do a malloc of some space for m_username and then copy $stg_username into that allocated memory.

In Perl this is easier. No malloc() is required. And no explicit copy is required.

If you actually DO mean a reference to a string to be used in place of a copy of the string, then that is of course possible also. Most of the time, I use a deep copy instead of a reference to the string in Perl as it is so easy to do in Perl with just "=", malloc() and then strcpy()is not required.

\[@ary_email_address] makes no sense. [ ary_email_address ] IS a reference to anon memory with email addresses. Another thing that is different than in C, is that each type of variable has its own name space. WOW! @a,$a,sub a,%a can all co-exist in the same program. So there is no need to prefix @email_address with @ary_email_address.

I made a ArrayofHash (similar to C Array of Struct) below. In Perl, it is possible to find out whether a value is a "pointer" to an array or not and I demo'ed that below also. Also the Data::Dumper module is VERY handy - there is no such equivalent in C...also demo'd below.

Hope this helps...

#!/usr/bin/perl -w use strict; use Data::Dumper; my $username = "PerlMonk"; my $password = "VeryGoodPassword"; my @email_addresses = ( "Monk1", "Monk2", "Monk3" ); my %Record1 = ( username => $username, password => $password, emailAddress => [@email_addresses] ); my %Record2 = ( username => 'SomeOtherPerlMonk', password => 'AntoherVeryGoodPassword', emailAddress => ["monka", "monkb", "monkc"], ); my @AoH; push (@AoH, {%Record1}); #{%Record1} allocates new memory #for the anonymous hash, makes a copy, #of the whole hash table, then adds a #reference to that anon hash #to the array @AoH (similar to a C pointer). push (@AoH, {%Record2}); print Dumper \@AoH; $username = "XXXXXXXXXXXXXXXXXXXX"; # the record in AoH is unchanged because $username was copied # into that record #print Dumper \@AoH; #un-coment to see same thing again print "\nDumping records....\n"; foreach my $record (@AoH) { foreach my $key (keys %$record) { if ( ref ($record->{$key}) eq 'ARRAY' ) { print "$key => @{$record->{$key}}\n"; } else { print "$key => $record->{$key}\n"; } } print "\n"; } __END__ $VAR1 = [ { 'password' => 'VeryGoodPassword', 'emailAddress' => [ 'Monk1', 'Monk2', 'Monk3' ], 'username' => 'PerlMonk' }, { 'password' => 'AntoherVeryGoodPassword', 'emailAddress' => [ 'monka', 'monkb', 'monkc' ], 'username' => 'SomeOtherPerlMonk' } ]; Dumping records.... password => VeryGoodPassword emailAddress => Monk1 Monk2 Monk3 username => PerlMonk password => AntoherVeryGoodPassword emailAddress => monka monkb monkc username => SomeOtherPerlMonk

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