in reply to Anonymous Data Structures
I had to look up anonymous data structures to figure out what you were referring to and it appears that they are just talking about references. THAT I can help you with.
Normally there are two ways of working with a variable, making a copy of it, or using a reference to it. A simplified example would be a sign-in sheet for a writing class. You want everyone to write their name on the sheet so you have to get information from everyone. There are two ways of doing this,
The first way sounds stupid, but you will be surprised how often it occurs, especially with beginners. Here is code to show how it works.
#!/usr/bin/perl use strict; use warnings; # Make a copy of a variable my $counter = 0; print "Copying and replacing the variable\n"; for (1..10) { $counter = increment_number( $counter ); print "Count is $counter\n"; } $counter = 0; print "Passing a reference to the variable\n"; for (1..10) { increment_number( \$counter ); print "Count is $counter\n"; } sub increment_number { my $number = shift; if (ref $number eq 'SCALAR') { $$number++; return; } return ++$number; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Anonymous Data Structures
by lwicks (Friar) on Jun 01, 2007 at 15:56 UTC | |
by Herkum (Parson) on Jun 01, 2007 at 21:12 UTC |