I'm relatively new to Perl and not a programmer by trade so maybe this is just a gap in my education but there's something I don't understand. Why is it necessary to access array and hash elements as scalars? The current syntax seems to make element references a special case. To quote from the perldata manpage...

Scalar values are always named with '$', even when referring to a scal +ar that is part of an array. It works like the English word ``the''. +Thus we have: $days # the simple scalar value "days" $days[28] # the 29th element of array @days $days{'Feb'} # the 'Feb' value from hash %days $#days # the last index of array @days but entire arrays or array slices are denoted by '@', which works much + like the word ``these'' or ``those'': @days # ($days[0], $days[1],... $days[n]) @days[3,4,5] # same as @days[3..5] @days{'a','c'} # same as ($days{'a'},$days{'c'})

From a syntactical standpoint, it seems to me that it would be more consistent to access array elements like so:

@ary[0]

instead of like this:

$ary[0]

The entire array is accessed with @. An array slice is accessed with @. Why not access an array element with @ as well? And the same with hashes?

I've read the perldata manpage and I understand that each element is a scalar. I understand the 'the' vs 'these'/'those' argument but the syntax still trips me up every time. I'm not thinking 'the' $element out of 'these' @elements - I'm thinking 'this' @element out of 'this' @array.

Cogito cogito, ergo cogito sum
(I think I think, therefore I think I am)

In reply to The philosophy behind element reference syntax by jcoxen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.