Keith wrote: > I've been working with perl for a while now, and I still have not been > able to grasp the concept of references. I've got Beginning Perl by Simon > Cozens published by Wrox, I have the Camel, and the cookbook. After > reading the information, and trying to ask people as well I still cannot > understand what references are, and why the are used. To me they seem more > trouble than they are worth... I know that with some modules, I have to > use an array reference, and some spit out a reference to a hash... > References to hashes just totally blow my mind... Usually the program will > yell at me that I have to use an array reference and I know to put a '\' > in front of the '@'. > > If someone can help me out, or point me to a good explanation on the web > for references I would greatly appreciate it... >
ok, i am trying to give you my 2cent and explain(at least i will try) what reference is about and how easy they can be use and manipulated. when you say things like that in Perl: my %hash = (a => 1, b => 2); you are creating a hash. the hash is stored in memory and it has a memory address. a bit latter, you might say: print $hash{'a'}; when Perl sees this statement, it knows that you want the value of the key 'a' in %hash, what Perl will do is look up the %hash hash, find it's memory address, go to that address and give you whatever value is in the memory address. Now, if you ask yourself, if i have the memory address of %hash, i can simply jump to that address and fetch the value right? but how do i get to the memory address of the hash? it turns out that by using reference, you can figure out the memory address of %hash like this: my $ref = \%hash; after that, $ref will reference %hash and it will have the memory address of where %hash is actually stored. to prove that, you can try: print $ref; it should print something like: HASH(0x80fbb0c) the 'HASH' portion tells you that $ref is a reference to a hash and the '0x80fbb0c' portion tells you the address of $ref points to. now you have the address, how do you get what's stored in that memory address? the answer is to de-reference. there is a couple way of doing this. here is one: my %another_hash = %{$ref}; the above statment says "go to the memory address where $ref points to and de-reference it as a hash and assigne it to %another_hash" the {} portion is optional so the above can be rewritten as: my %another_hash = %$ref; the '%' symble tells Perl that $ref points to a hash reference so when it de-reference it, it should stay as a hash. now, the above de-refernce the whole hash. sometimes, you don't need that, you might just need a single value. in that case, you have to tell Perl that you just want a single value by doing: print $ref->{'a'}; the above tells Perl that $ref points to a hash reference ({} gives a hint to Perl that it's pointing to a hash) and you want Perl to give you the value of key 'a'. Array reference is similar except that you usually replce {} with [] like: #-- create an array my @array = (1..5); #-- make $ref pointing to @array my $ref = \@array; #-- de-reference what $ref points to and assign it to @another_array my @another_array = @{$ref}; or: my @another_array = @$ref; since the {} is optional. the '@' symble is not optional because you have to let Perl know what $ref is pointing to. '%' for hash and '@' for array. again, if you just want a single value out of the array ref, you can say: print $ref->[0]; #-- first slot pritn $ref->[1]; #-- second slot ...etc notice the syntax if almost the same as hash ref except that you replace {} with []. not only hash and array can be reference, virtually anything in Perl that has a memory address can be reference. this include scalar and subroutine: my $i = 1; my $j = \$i; #-- $j is a scalar reference that points to $i #-- to de-reference: print ${$i}; again the {} portion is optional so the above can be written as: print $$i; subroutine can also be referenced! sub hi{ print "hi\n"; } #-- make $s points to the subroutine hi(), notice the special symble #-- & my $s = \&hi; #-- to de-reference $s and call the function hi(): &{$s}; again the {} is optional: &$s; #-- do the same thing again you can use the -> syntax as well: $s->(); still reading? make sense? :-) since virtually everything in Perl have an address and since reference points to a memory location, you can tell directly ask Perl to create something in memory and then point your reference to that memory address: #-- create a hash reference my $hash_ref = { a => 1, b => 2 }; the '{a => 1, b => 2}' creates a anny. hash. it doesn't have a name but it does have a memory address and you are createing $hash_ref directly pointing to that memory address. you can dereference $hash_ref the same way i showed you like: #-- get back the whole hash my %hash = %{$hash_ref}; #-- get just a single slice print $hash_ref->{'a'}; array is the same: my $array_ref = [ 1..5 ]; my @array = @{$array_ref}; print $array_ref->[0]; subroutine is the same: my $sub_ref = sub { print "hi\n"; }; #-- calls the subroutine. it doesn't have a name but since it's stored in #-- memory and we have the memory address of it, we can jump to that #-- directly. $sub_ref->(); now, if a subroutine tells you that it's accepting a hash reference, array reference or code (subroutine) reference, you should know what to do right? for example: #-- needs a hash reference sub need_hash_ref{ my $ref = shift; print $ref->{'a'}; } #-- ok, give need_hash_ref a hash reference my %hash = ( a => 1, b => 2); my $ref = \%hash; #-- give it: need_hash_ref($ref); or just: need_hash_ref({ a => 1, b => 2}); Reference seems like a lot of trobule to grasp and understand at first, but they have good use: 1. they are efficient in term of memory usage. since $ref only reference a memory address, it stores very little. 2. they are efficient in term of process speed. when you pass something to/from a subroutine with ref, Perl only have to copy the memory address along with it. if doesn't care how big or small the reference points to. because of this, even the reference points to a huge hash or array, Perl only makes a one copy(it's memory address). there is no need to copy this huge hash or array. very efficient. 3. for callback or side effect. when you pass a reference to/from a subroutine. you are actually passing a memory address(you already know that), the subroutine goes to that memory address, do something with it and then it exit. the caller goes back to that memory address, looks up the value in there and can find out what the subroutine has done. there are other usage of reference. the explaination that i gave you is over simplify. a lot of details are omitted. i hope this will give you a good start once you read perldoc again. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]