Gossamer Forum
Home : General : Perl Programming :

What's the point of references?

Quote Reply
What's the point of references?
Can someone explain the point of references because I don't see why/when they'd be used?

The only example I can see would be useful is:
Code:
sub some_sub {

my ($array1,$array2) = @_;

}
Code:
some_sub(\@array1,\@array2);
Apart from that, why would you need to do something like:
Code:
$a = \$b;

print $$b;
??


Another example:
Code:
%hash = (
Fruit => 'apple',
Drink => 'cola'
);
my $bla = \%hash;
Code:
print $$bla{Fruit};
Why not just use
Code:
print $hash{Fruit};
??

Thanks.


Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: What's the point of references? In reply to
Complex data structures and objects, just to name a couple of reasons. In very simple situations like above, they don't (usually) make sense, though that depends on the situation.

They are very needed for passing arrays to subroutines as you indicated.

Quote Reply
Re: What's the point of references? In reply to
Here's a silly little example, but it illustrates a point (I hope) for possible use

Code:
my ($count1, $count2, $count3);

sub increase {
my $action = shift;

my %options = (
show => [\$count1, 'Show results'],
remove => [\$count2, 'Remove blah'],
edit => [\$count3, 'Edit foo'],
);

my ($count, $display) = @{$options{$action}};
++$$count;
print "The action '$display' has been done $$count times";
}
Basically, doing it like this removes the need to have a bunch of if-else statements to check which option was chosen, and increatse the appropriate counter. This just gets the appropriate reference, then increases the correct counter with standardized code.

Does that make sense? Not the greatst of examples, but hopefully shows you a little something

--mark

Quote Reply
Re: What's the point of references? In reply to
Ah yes thankyou, that helps a lot.

I was a little confused at first but I understand now.

So $action would either be show, remove or edit? Eg:

&increase("show");

That is how it would be called?....and then $count either references $count1, $count2 or $count3 depending on the action and then it is incremented by using ++$$count;

...and then the final result would be something like

"The action 'show results' has been done '1' times"

Yep I understand now. Thanks for that example!

Just as a side point...is there a difference between ++$$count and $$count++ ?

Thanks again.



Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: What's the point of references? In reply to
In Reply To:
...is there a difference between ++$$count and $$count++ ?
Not in the example above, no. there would be no difference.

However, if you changed the two lines of code that look like this:

Code:
++$$count;
print "The action '$display' has been done $$count times";
To this:

Code:
printf "The action '%s' has been done %d times", $display, $$count++;
Then it would be printed, before it was incremented. Changing it back to ++$$count, using the same printf line, like this:

Code:
printf "The action '%s' has been done %d times", $display, ++$$count;
Would increment the variable, then print it.

See the perlop manpage, and search for Auto-increment.

Regards,

-Steven


Quote Reply
Re: What's the point of references? In reply to
Thanks.

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: What's the point of references? In reply to
While on the topic of ++$var vs. $var++ I'll leave you with the following little tidbit:

Code:
$count = 3;
printf "%d %d", ++$count, ++$count
Now, what would you expect from this code? "4 5" perhaps?

Well, what you actually get is "5 5". I'll leave you to figure that one out, but it illustrates another difference to ++$var and $var++ other than when the increment of the variable happens.

To illustrate it further:
Code:
$var = 3;
print $var, $var++, ++$var, ++$var, $var++, $var;
Evaluating left to right, you would probably expect the following numbers: 3, 3, 5, 6, 6, 7. What you'll actually get is: 7, 3, 7, 7, 6, 7.

Anyone care to take a shot about why this happens? Wink

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What's the point of references? In reply to
In Reply To:
$count = 3;
printf "%d %d", ++$count, ++$count
Is it because placing the ++ after the variable increments the counter after returning but ++ before the variable increments before returning.

So basically both ++$count's are incremented before the value is returned so it will total 5.

Can't fathom the other example though.

Installs:http://www.wiredon.net/gt/
MODS:http://wiredon.net/gt/download.shtml

Quote Reply
Re: What's the point of references? In reply to
If it walks like a duck and quacks like a duck, it must be precedence Wink


Dan Cool

LotusLand
Vancouver, BC, Canada
Top Ranked City in World for Quality of Life
Quote Reply
Re: What's the point of references? In reply to
Actually, no :)

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What's the point of references? In reply to
The reason is that it ++$var returns $var - not a number equal to $var. $var++ returns a number (actually, sometimes a string, but let's not go there) equal to one less than $var, but not $var. Variables are subject to change, numbers are not. So, since $var and ++$var are all $var, whatever actually gets passed to the function is going to be the final variables, while $var++ returns a number that, although is equal to the old $var, is not tied in any way to $var.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: What's the point of references? In reply to
whooooooooooooooooooooooooooooosh...........

Code:
---------------->>>>
| /\/\/\/\/\
| | |
| | O O |
| 0 |
| ___ |
|________|
Installs:http://www.wiredon.net/gt/
MODS:http://wiredon.net/gt/download.shtml