in reply to Subroutine that modifies actual variable
# 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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: Subroutine that modifies actual variable
by tilly (Archbishop) on Jul 11, 2001 at 21:10 UTC |