I have some Perl code where I noticed an array is used with a leading backslash like \@array
Can anybody explain what does it mean?
To access a single element of a Perl array, use ($) sign before variable name. You can assume that $ sign represents singular value and @ sign represents plural values. Variable name will be followed by square brackets with index number inside it. Indexing will start with 0 from left side and with -1 from right side.
Dereferencing an array If you have a reference to an array and if you would like to access the content of the array you need to dereference the array reference. It is done by placing the @ symbol (the sigil representing arrays) in-front of the reference.
Note: In Perl arrays, the size of an array is always equal to (maximum_index + 1) i.e. And you can find the maximum index of array by using $#array. So @array and scalar @array is always used to find the size of an array.
Access arrays with numeric valuesAll arrays in Perl start at 0. Arrays can also be accessed from the end by using negative offsets from the end. Reading a non-existent element gives undef .
the \@
notation will return a reference (or pointer) to the array provided, so:
$arrayref = \@array
will make $arrayref
a reference to @array
- this is similar to using the *p
pointer notation in C.
It means it's a reference to an array.
See the perl documentation that explains it well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With