I believe Perl uses what's called a 'pass by reference' method to give variables to a function (only important to remember the name in other languages, e.g., C/C++, where you have a choice of by value or by reference). Anyway, what this means is that you can actually modify the variable inside the function without doing anything special. E.g.:
sub delete_first_char {
$_[0] =~ s/^.//
}
That code would delete the first character of a string (admittingly not the best way to do it). However, most subroutines you see do this:
sub whatever {
($var1,$var2,$etc) = @_;
...
}
They do this so that they don't mistakingly alter/change the variable that was passed to them. Usually this is a good coding practice, however, quite certainly situations do arise where you wish to modify the variable that you pass in, in which case you just refer to it directly; i.e., $_[--elm # here--]. | [reply] [d/l] [select] |
I feel rather silly, since i've been coding perl for around a year and have never tried to modify $_[0] directly, i've always assigned it to another var. Thank you.
| [reply] |
You've got to either pass a reference to a variable explicitly,
or define the function as something that takes a reference.
Here's two examples, with usage:
# This function takes a value by reference,
# which is the responsiblity of the caller.
sub fref
{
my ($var) = @_;
$$var = $$var . " Python";
return $$var;
}
# This function defines a prototype which takes
# a reference instead of a scalar. This way the
# caller doesn't have to do anything special.
sub fprot(\$)
{
my ($var) = @_;
$$var = $$var . " Python";
return $$var;
}
# The reference version requires a backslash before any
# passed parameters.
my $test = "Monty";
print fref(\$test),"\n";
print $test,"\n";
# While the prototype means that you don't have to worry
# about doing anything special.
my $test = "Learning";
print fprot($test),"\n";
print $test,"\n";
| [reply] [d/l] |
| [reply] |