perldereferenceperldocperlop

Perl dereferencing in non-strict mode


In Perl, if I have:

no strict;
@ARY = (58, 90);

To operate on an element of the array, say it, the 2nd one, I would write (possibly as part of a larger expression):

$ARY[1]  # The most common way found in Perldoc's idioms.

Though, for some reason these also work:

@ARY[1]
@{ARY[1]}

Resulting all in the same object:

print (\$ARY[1]);
print (\@ARY[1]);
print (\@{ARY[1]});

Output:

SCALAR(0x9dbcdc)
SCALAR(0x9dbcdc)
SCALAR(0x9dbcdc)

What is the syntax rules that enable this sort of constructs? How far could one devise reliable program code with each of these constructs, or with a mix of all of them either? How interchangeable are these expressions? (always speaking in a non-strict context).

On a concern of justifying how I come into this question, I agree "use strict" as a better practice, still I'm interested at some knowledge on build-up non-strict expressions.

In an attemp to find myself some help to this uneasiness, I came to:

  • The notion on "no strict;" of not complaining about undeclared variables and quirk syntax.
  • The prefix dereference having higher precedence than subindex [] (perldsc § "Caveat on precedence").
  • The clarification on when to use @ instead of $ (perldata § "Slices").
  • The lack of "[]" (array subscript / slice) description among the Perl's operators (perlop), which lead me to think it is not an operator... (yet it has to be something else. But, what?).

For what I learned, none of these hints, put together, make me better understand my issue.

Thanks in advance.


Solution

  • Quotation from perlfaq4:

    What is the difference between $array[1] and @array[1]?

    The difference is the sigil, that special character in front of the array name. The $ sigil means "exactly one item", while the @ sigil means "zero or more items". The $ gets you a single scalar, while the @ gets you a list.

    Please see: What is the difference between $array[1] and @array[1]?