in reply to Subroutine that modifies actual variable

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";

Replies are listed 'Best First'.
Re (tilly) 2: Subroutine that modifies actual variable
by tilly (Archbishop) on Jul 11, 2001 at 21:10 UTC
    The first answer already showed how to do it without using Perl's (IMHO broken) prototype semantics or with an explicit reference.

    That said, I think it is stylistically a bad idea to modify variables passed in by reference without warning in the usage or the name of the function.