Perl: how to understand @ $ ref [0]?

perl question about links.

$ref = [11, 22, 33, 44]; print "$$ref[0]" . "\n"; print "@$ref[0]" . "\n"; 

when i run perl -d.

 DB<1> p @$ref 11223344 DB<2> p $ref ARRAY(0x9dbf480) DB<3> p \$$ref[0] SCALAR(0x9dbf470) DB<4> p \@$ref[0] SCALAR(0x9dbf470) 

$$ ref [0] is the first ARRAY scalar (0x9dbf480).

What does @ $ ref [0] mean? I can not understand.

+6
source share
2 answers

$ref = [11, 22, 33, 44]; - link to an anonymous array.

$$ref[0] or ${$ref}[0] or $ref->[0] causes the array to be dereferenced and the first element to be retrieved.

@$ref[0] or @{$ref}[0] causes the array to be dereferenced and get a slice of the array containing only the first element.

+10
source

Firstly, @$ref[0] is different from \@$ref[0] . You have the first in a debugging session, and the second in a script.

In any case, @$ref[0] means the same as @{$ref}[0] . If you have an array named @ref , @ref[0] will be equivalent. It uses slice notation to get the first element of an array.

The difference between @array[$x] and $array[$x] is that in the first you can specify more than one index and return a set of elements from the array, and not just one. But if you only put one index between the brackets, you will get the same result.

+4
source

Source: https://habr.com/ru/post/918265/


All Articles