in reply to Re: Re (tilly) 3: 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()

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...

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

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

      Personally I would be inclined to introduce things in this order.

      Basic introduction, how to write a program, how to run it. Hello World level.

      Scalars.

      Arrays.

      Functions.

      Strict and my.

      Taking user input.

      Scalar vs list context. (Arises very naturally while trying to read STDIN into various things.)

      Hashes.

      etc.

      The route is very direct. Straight to the simplest construct in which you can see why my is useful, then straight to how to protect yourself. The detailed scoping rules, loops, blocks, and context can all come after I hit the basic habit I want people to have. How that habit intertwines with each additional concept can be covered when I hit the concept in question...