Why do all these array accessors work?

It seems to me that some of them should fail, but they all output what they should:

$, = "\n";
%test = (
    "one" => ["one_0", "one_1"],
    "two" => ["two_0", "two_1"]
);

print @{$test{"one"}}[0],
      @{$test{"one"}}->[0],
      $test{"two"}->[0],
      $test{"one"}[1];

      

Why is this?

+2


a source to share


1 answer


Your first example is different from the others. This is a slice of the array - but is hidden since you are only asking for one element.

@{$test{"one"}}[0];

@{$test{"one"}}[1, 0];  # Another slice example.

      

Your other examples are alternative ways to remove object references in a multi-level data structure. However, using an array for de-referencing is not recommended (see perldiag ).



@{$test{"one"}}->[0]; # Deprecated. Turn on 'use warnings'.
$test{"two"}->[0];    # Valid.
$test{"one"}[1];      # Valid but easier to type.

      

For the last example, two subscripts next to each other are implied ->

between them. See, for example, the discussion on Arrow Rules in perlreftut.

+8


a source







All Articles