local() gives a local value to a global variable. A local variable is visible to called subs but not calling subs. It seems to me that it would be difficult to use local to have class data. I think that this is one of those cases where a global is the correct solution to a problem. One shouldn't use globals to pass data to a subroutine - it's bad form, and it's confusing. However, since a class is a package in Perl, then a class global is the best way to store class data. Declaring the global with my makes it impossible for anyone else to accidentally change the variable outside of the lexical scope. I would also probably use a hash to make it very clear where the data is coming from:

package Class::Foo; my %ClassData = (); #methods here

Note however, that you have to be very careful if you are putting the class in the same file as the code which uses it - as a file (and NOT a package) delimits lexical scope, your class data might leak into the main program even if declared with my.

Cheers,
Erik

In reply to Re: Whether to use local() by erikharrison
in thread Whether to use local() by jerrygarciuh

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.