G'day David,

DavidB wrote:
> Hi, I can think of some reasons we use $ instead of @ for array
> indices (in Perl5):

My usual explanation goes a little like this:

In Perl, $ is pronounced 'the' or 'a' or 'this' (ie, singular):

        $name                   # The name
        $friend[0]              # The first friend
        $colour_of{grass}       # The colour of grass
        $ref->{$key}            # The value $key using this reference.

On the other hand, @ means 'these' or 'those':

        @friend                 # These friends
        @friend[0..3]           # These four friends

When we write an @ sign when trying to retrieve a single element, we're
really asking Perl for a list of elements.  If what we really want back is
just a single element, then Perl can get confused.

I don't actually show the cases where Perl gets it wrong, instead I mention
that Perl:

        (a) Will warn you if it figures out that you wanted a scalar when
            you asked for a list.

        (b) Will *not* warn you when it decides you actually meant a list
            of one element.  This means debugging your code will be a
            painful experience, because Perl won't help you.

Hence the result of using @ incorrectly is almost always a warning or a
hard-to-find bug, both of which are extremely undesirable.

Cheerio,

        Paul

-- 
Paul Fenwick <[EMAIL PROTECTED]> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

Reply via email to