Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
You can use both globals (package vars) and lexicals under use strict though globals need to be fully qualified ( $My::Mail::Server= "255.255.255.255";)
Though globals are usually (not always) the wrong way to go. The idea of lexicals are that they only exist in a scope thereby limiting access. What local does is let you temporarily save a value for a package var. It's a runtime trick and my is (mostly) a compile time trick.

The lowdown is package vars exist in packages and any can be accessed from any package. my vars exist in the scope they where declared in and cannot be accessed outside so you don't have to worry about other subs/modules etc clobbering your values. You usually use local to save the value of $/ or $_ so you don't screw up code that calls your code. It has some other uses but that's one of the main one. There are probably a lot better explainations than mine and I'm sure if you do a search for lexical / package / global you will find a wealth of info here in the monestary.

UPDATE
here is a great FAQ on "Coping with Scoping" by dominus. It's a far better explaination than mine.

-Lee

"To be civilized is to deny one's nature."

In reply to Re:(3) Whether to use local() by shotgunefx
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.