You also might want to modify an arg to a subrutine. E.G.
With References
sub increment {
my $arg_ref = shift;
$$arg_ref++;
if (is_bad($$arg_ref)) {
return "FAILURE!";
}
return undef;
}
my $num = 0;
my $err;
$err = increment(\$num);
if ($err) {
die $err;
}
print "$num\n";
|
Without References
sub increment {
my $arg = shift
if (is_bad($arg)) {
die "FAILURE";
}
return ++$arg;
}
my $num = 0;
$num = increment($num);
print "$num\n";
|
I don't think my sub that uses references is any better than the
one that doesn't. It just shows two ways of doing the same thing.
Because you pass $num as a reference, the subrutine can change num.
Then you can use the function's return for something else.
Update: chas! I've fixed the example to use ++$arg rather than $arg++.
AND! Woah. Baked my noodle with that one. So the only reason I've been convinced that subrutine args were passed as values was because I was using shift all the time!? Man... aye aye aye...