You shouldn't be using my on variables that another piece of code somewhere else ever might need to access. my is strictly for variables that will never need to be accessed from anywhere else -- loop counters and internal stuff that other code shouldn't mess with.

You've probably read or heard somewhere from someone an oversimplistic comment to the effect that you should declare all your variables with my, but if so that is fundamentally wrongheaded. You should declare variables with my when you want them to be accessible exclusively from the current code block (i.e., lexically scoped). (Or when you want to create a closure, but closures are beyond the scope of this discussion.) It especially makes no sense to declare all your varibles with my after a package declaration, because the package declaration already accomplishes the thing that is usually the major goal of lexical scoping (i.e., to keep other code from _accidentally_ tromping your variables or having theirs tromped by your code). There are certainly cases where _certain_ variables should be scoped with my inside a package, or even most of them, but if you're doing it to every single varibale without thinking, don't.

The Coping with Scoping article that chmrr suggested is (mostly) excellent, and you should read it if there's any part of what I said above that you don't fully understand. It neglects to explain why you would _want_ other code to be able to expressly alter your variables, but from your post it appears that you already understand that part. It also does not explain dynamic scope 100% correctly (and in fact is dead wrong on at least one point), but you don't need to understand dynamic scope in order to correctly use lexical and package scope. You _do_ need to understand the differences between package scope and lexical scope, and it explains that very well, so I won't repeat the explanation here. Go read it.


{my$c;$ x=sub{++$c}}map{$ \.=$_->()}map{my$a=$_->[1]; sub{$a++ }}sort{_($a->[0 ])<=>_( $b->[0])}map{my@x=(& $x( ),$ _) ;\ @x} split //, "rPcr t lhuJnhea eretk.as o";print;sub _{ord(shift)*($=-++$^H)%(42-ord("\r"))};

In reply to Re: How to use variables from other package? by jonadab
in thread How to use variables from other package? by Anonymous Monk

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.