in reply to Re^3: Elaborate Records, arrays and references
in thread Elaborate Records, arrays and references
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
|
|---|