Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

In my experience you can only really explain the difference between my and local once you've explained the difference between package and lexical variables.

A package variable is associated with a package (hence the name!) It lives in the package's symbol table and can be accessed from anywhere within the package. A package variable can also be qualified with its package name (e.g. $my_package::variable) and, in this form, can be accessed from anywhere in the program.

A lexical variable is associated with a block of code. It lives in a pad and can only be accessed from within the block.

local only works with package variables. It stores the existing value of the variable and gives it a new, undef, value. The previous value of the variable is restored when the innermost enclosing block of code is exited.

my creates a new lexical variable. The variable only exists until the innermost enclosing block of code is exited.

--
<http://www.dave.org.uk>

Perl Training in the UK <http://www.iterative-software.com>


In reply to Re: The difference between my and local by davorg
in thread The difference between my and local by cLive ;-)

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2014-10-22 05:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (112 votes), past polls