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

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.

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

Replies are listed 'Best First'.
Re: Re (tilly) 3: I need a simple explantion of the difference between my() and local() (discussion)
by buckaduck (Chaplain) on May 04, 2001 at 01:32 UTC
    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...

        That would have been a nice luxury. My class is not made up of programmers. Most of them are research scientists. Many of them are web developers, and some are merely managers. (Sorry, boss!)

        So, week 1 of my class looked like this:

        • How to install Perl on Win95 and UNIX platforms
        • Examples of text editors that can be used to write a Perl program
        • print "Hello, World!";
        • Running the program on Win95 and UNIX platforms
        • Accessing documentation via perldoc, books, and web sites (including PerlMonks)
        • Demos of some easy but useful programs that they would learn to write during the course

        Maybe you can see why it took me a while to get where I am now.

        And consider this: how could I explain the dangerous difference between my $var and my ($var) until they knew about scalar context vs. list context? And how could I explain context until they knew about scalars and lists? Furthermore, how could I explain the scoping rules until they knew about blocks and loops and subroutines?

        buckaduck