It looks like fuzzy thinking to me. You have two subroutines and you want then to share a variable, but you don't want that variable visible anywhere else. The answer to this is closures.

Since subroutines form a closure, the easiest way is to declare ref_test2() inside the body of ref_test1(). ref_test2() will have access to everything ref_test1() does, Note that creating a named subroutine puts a global entry in the symbol table, ref_test2() could still be called from outside of ref_test1() and still see the exact same variables.

But I don't like nested subroutines under any circumstances, so I wouldn't go there. I'd do it like this:

{ # start closure my $type; sub ref_test1() { ... } sub ref_test2() { ... } } # end of closure

which allows both to read/write/whatever $type as much as they like, yet $type is still invisible to the outside world.

- doug

PS: Someone posted that since ref_test1() was calling ref_test2() directly, then you should pass $type as a parameter, and I agree completely. I'm guessing that this example is simplified, and that in the real example ref_test2() can be called from other places too.


In reply to Re: Unable to declare local variable with "use strict". by doug
in thread Unable to declare local variable with "use strict". by mr_p

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.