[Za-pm] Passing values
Sean Carte
scarte at pan.uzulu.ac.za
Fri Jan 9 02:05:03 CST 2004
On Fri, 2004-01-09 at 08:58, Spike wrote:
> Hi All
>
> First query of the year!
First answer of the year!
>
> If I have a function (sub routine) like :
>
> sub double {
> my $num = shift;
> return $num * 2;
> }
>
> I could say:
>
> $value = 3;
> $value = double($value);
> print "$value\n"; # 6 expected
>
> My question is how could I write that function so that it works
> directly on the variable that is passed to it in the same way that
> functions like chomp do?
>
> $value = 3;
> double($value);
> print "$value\n"; # 6 expected
>
Use references:
$value = 3;
double(\$value); # pass a reference to $value
print "$value\n"; # 6 expected
sub double {
my $ref = shift;
$value = $$ref; # dereference $ref
$value *= 2;
}
--
Sean Carte <scarte at pan.uzulu.ac.za>
University of Zululand
______________________________________
Inflex - installed on mailserver for domain @pan.uzulu.ac.za
Queries to: postmaster at pan.uzulu.ac.za
More information about the Za-pm
mailing list