test's $value and MAIN's $value are completly different variables despite the similarity in their names. Assigning to one will not change the other.
Fortunately, since parameters are passed by reference in Perl, you can change a variable in the caller by modifying the parameter ($_[0]) as opposed to a copy of it ($value).
sub test { $_[0] = "new value"; } { my $var = "old value"; print("$var\n"); test($var); print("$var\n"); }
If you wanted to use a meaningful name instead of $_[0], you could create an alias using Data::Alias or as follows:
sub test { # alias my $arg = $_[0]; our $arg; local *arg = \$_[0]; $arg = "new value"; } { my $var = "old value"; print("$var\n"); test($var); print("$var\n"); }
Update: Added aliasing bit.
In reply to Re: Return Value in Subroutine
by ikegami
in thread Return Value in Subroutine
by rose
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |