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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.