Lexical scoping is your friend:
I think this will demonstrate what you're looking for?
#!/usr/bin/perl use strict; my $tossMeAbout = 'Initial value'; { my $updatedValue = &adjustValue($tossMeAbout); print "----------------------------------------------------------- +--------------------\n"; print " MAIN: \$tossMeAbout = [$tossMeAbout]\n"; print " MAIN: \$updatedValue = [$updatedValue]\n"; my $additionalValue = &adjustValue($updatedValue); print "----------------------------------------------------------- +--------------------\n"; print " MAIN: \$tossMeAbout = [$tossMeAbout]\n"; print " MAIN: \$updatedValue = [$updatedValue]\n"; print " MAIN: \$additionalValue = [$additionalValue]\n"; print "----------------------------------------------------------- +--------------------\n"; } exit; { my $persistentValue; sub adjustValue { my ($requestedValue, @garbage) = @_; if (!defined $requestedValue) { $requestedValue = ''; } my $returnValue = ''; print "------------------------------------------------------- +------------------------\n"; print "SUB BEGIN: \$requestedValue = [$requestedValue]\n"; print "SUB BEGIN: \$persistentValue = [$persistentValue]\n"; print "SUB BEGIN: \$returnValue = [$returnValue]\n"; # Append requested value to persistent value if (!defined $persistentValue) { $persistentValue = $requestedValue; } else { $persistentValue .= "/$requestedValue"; } $returnValue = "<$requestedValue>"; print "------------------------------------------------------- +------------------------\n"; print " SUB END: \$requestedValue = [$requestedValue]\n"; print " SUB END: \$persistentValue = [$persistentValue]\n"; print " SUB END: \$returnValue = [$returnValue]\n"; return $returnValue; } } __END__
Results:
C:\Steve\Dev\PerlMonks\P-2015-03-19@1121-Durable-Subroutine-Data>persi +stent1.pl ---------------------------------------------------------------------- +--------- SUB BEGIN: $requestedValue = [Initial value] SUB BEGIN: $persistentValue = [] SUB BEGIN: $returnValue = [] ---------------------------------------------------------------------- +--------- SUB END: $requestedValue = [Initial value] SUB END: $persistentValue = [Initial value] SUB END: $returnValue = [<Initial value>] ---------------------------------------------------------------------- +--------- MAIN: $tossMeAbout = [Initial value] MAIN: $updatedValue = [<Initial value>] ---------------------------------------------------------------------- +--------- SUB BEGIN: $requestedValue = [<Initial value>] SUB BEGIN: $persistentValue = [Initial value] SUB BEGIN: $returnValue = [] ---------------------------------------------------------------------- +--------- SUB END: $requestedValue = [<Initial value>] SUB END: $persistentValue = [Initial value/<Initial value>] SUB END: $returnValue = [<<Initial value>>] ---------------------------------------------------------------------- +--------- MAIN: $tossMeAbout = [Initial value] MAIN: $updatedValue = [<Initial value>] MAIN: $additionalValue = [<<Initial value>>] ---------------------------------------------------------------------- +---------
In reply to Re: Accessing Variables in Different Subroutines
by marinersk
in thread Accessing Variables in Different Subroutines
by stefl
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |