Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re (2): I need a simple explantion of the difference between my() and local() (discussion)

by deprecated (Priest)
on May 03, 2001 at 20:15 UTC ( [id://77695]=note: print w/replies, xml ) Need Help??


in reply to Re: I need a simple explantion of the difference between my() and local()
in thread I need a simple explantion of the difference between my() and local()

    and when each should be used?
    That's easy: Always use my, and never use local.

Given Ovid's recent diatribe on questioning authority, I am going to call you on this one. I understand your wanting to make a simple answer here, but there is a slightly less simple but infinitely more valuable way to put this. "Always use my unless you are localizing a perl internal variable, such as one from perldoc perlvar." I think newbies will understand that they are not localizing a perl variable, and probably will glaze over it if it isnt applicable. To those whom it is applicable, they will understand right away that at the least they need to do something different, and hopefully ask a question.

For further clarity, I'd like to point out merlyn's excellent catch here, Re: Re: Re: Optimization for readability and speed (discussion), wherein he caught me messing about with $" when I should have been using local but wasnt. The node is in a simple context that I think most perl hackers can understand, even something-above-brand-new-newbies-to-perl'ers.

yours in perl
brother dep.

--
Laziness, Impatience, Hubris, and Generosity.

  • Comment on Re (2): I need a simple explantion of the difference between my() and local() (discussion)
  • Download Code

Replies are listed 'Best First'.
Re (tilly) 3: I need a simple explantion of the difference between my() and local() (discussion)
by tilly (Archbishop) on May 04, 2001 at 00:42 UTC
    Do you think that localizing special variables is something that needs to be encouraged in Perl beginners?

    Just to be clear, if you are going to change them, you should definitely localize them. If you are unclear on what I mean, read the very response of merlyn's that you are waving around.

    When you are done that question, do you think it is good to lie to people and say that the only legitimate reason for calling local is to modify special variables?

    In general I am with Dominus. Unless your knowledge level is to the point where you understand why you don't have any real choice but to call local, you should try to figure out how to solve your problem while calling my.

      Actually, I think that it is important to know how to use local() for special variables like $/, $_, and $SIG{INT}. Maybe not for absolute beginners, but then I personally wouldn't thrust my() on absolute beginners, either.

      In fact, I'm teaching a Perl class where I work, and I finally got around to demonstrating my() today. And this was my 11th class. (After all, I had to discuss how to use variables and write blocks of code, before I got into making variables private!) Although I didn't show them the local() command today, I probably will soon -- with an introduction just like deprecated used.

      And I believe that deprecated's post worded the situtation wonderfully. An absolute beginner would know to leave the local() function alone, but the description was also precise enough that others might learn even more. It doesn't hurt to be precise, as long as an explanation doesn't become too confusing. In this case, I thought it was pretty darned clear.

      buckaduck

        When I have taught co-workers, my and strict were in lesson 1. Mind you I was teaching people who were already programmers, else it might have waited for another lesson or 3.

        My list of items to make sure people know does not include $/ or $SIG{INT}, but does include closures. That reflects what I use in my code...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://77695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-26 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found