You can't (and, while not strictly crazy to want this, you are... misguided... or unwise...).

The point about statically scoped (my) variables is that what they refer to can be completely determined by their point of definition. So if I say

my $foo=17; sub quux { my $bar=29; $foo = baz($foo,++$bar) }
then it is possible, just by examining the point of definition of quux, to determine what gets modified where.

What you're after is global variables. You want some variable defined in one place in your program to get modified in an entirely different place. Why is this bad?

Well, consider this code:

use vars qw/$foo/; sub quux { my $bar=29; $foo = baz($foo,++$bar) } # ...code passes... $foo = 11; print quux($foo), "\n"; # ...more code... { local $foo = 39; print quux($foo), "\n"; }
Note how every time some other $foo gets modified. Just by examining the new definition of quux you get no clues as to what gets modified. Instead, you have to examine the point where quux gets called, then work your way up the call stacks to surrounding blocks. You examine each block to see if local $foo sets up a new copy, or find $foo at the top level. And that's the copy of $foo that gets modified.

Variable lookup with global variables is akin to spaghetti. You can eat it, but there are healthier alternatives...


In reply to Re: subroutines and namespace by ariels
in thread subroutines and namespace by George_Sherston

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.