Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Well, this confused the hell out of me, so I thought I'd spend some time getting my head around it.

Probably best to show by example (apologies to Joseph Hall and merlyn for borrowing heavily here from Effective Perl Programming)

Quick summary: 'my' creates a new variable, 'local' temporarily amends the value of a variable

There is a subtle difference.

In the example below, $::a refers to $a in the 'global' namespace.

$a = 3.14159; { local $a = 3; print "In block, \$a = $a\n"; print "In block, \$::a = $::a\n"; } print "Outside block, \$a = $a\n"; print "Outside block, \$::a = $::a\n"; # This outputs In block, $a = 3 In block, $::a = 3 Outside block, $a = 3.14159 Outside block, $::a = 3.14159

ie, 'local' temporarily changes the value of the variable, but only within the scope it exists in.

so how does that differ from 'my'? 'my' creates a variable that does not appear in the symbol table, and does not exist outside of the scope that it appears in. So using similar code:

$a = 3.14159; { my $a = 3; print "In block, \$a = $a\n"; print "In block, \$::a = $::a\n"; } print "Outside block, \$a = $a\n"; print "Outside block, \$::a = $::a\n"; # This outputs In block, $a = 3 In block, $::a = 3.14159 Outside block, $a = 3.14159 Outside block, $::a = 3.14159

ie, 'my' has no effect on the global $a, even inside the block.

But in real life, they work virtually the same?

Yes. Sort of. So when should you use them?

  • use 'my' when you can (it's faster than local)
  • use local when:
    • you're amending code written in Perl 4, unless you are sure that changing 'local' to 'my' will not cause any lexical problems
    • you want to amend a special Perl variable, eg $/ when reading in a file. my $/; throws a compile-time error

If you use Perl 5 and strict (and I know you do :), you probably haven't noticed any difference between using 'my' and 'local', but will hopefully only use 'local' in the second instance above.

EPP also suggests you use 'local' when messing with variables in another module's namespace, but I can't think of a RL situation where that could be justified - why not just scope a local variable? Perhaps someone could enlighten me?

But, if you ever end up amending some old Perl 4 code that uses local, you need to be aware of the issues and not just do a s/\blocal\b/my/gs on the script :) - sometimes people use the 'features' of local in unusual ways...

Hope that's cleared a few things up.

cLive ;-)


In reply to 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":



  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-10-20 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (81 votes). Check out past polls.

    Notices?