What does \@array mean in Perl?
10,184
Solution 1
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.
Solution 2
It means it's a reference to an array.
See the perl documentation that explains it well
Related videos on Youtube
Author by
shift66
Updated on June 04, 2022Comments
-
shift66 over 1 year
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?
-
silbana about 12 yearsPlease do not confuse pointers and references.
*(p + 3)
is perfectly valid in C (so long asp
points to valid storage for at least 4 elements). What do you think\@array + 3
is? First, you seem to think that pointers and arrays are the same in C, and, then,you are confusing C pointers with Perl references. Misleading. -
silbana about 12 yearsSubroutine arguments are always lists. If you do
func(@ary)
, the contents of@ary
are passed as a flat list. Inside offunc
, you know how many arguments were passed (because the arguments are in@_
). If you dofunc(\@ary)
, thenfunc
receives a list consisting of a single argument. You can access the third element of the anonymous array to which the first argument tofunc
refers using the standard arrow notation:$_[0]->[2]
. Of course, you'd normally put that ref into its own variable as the first thing:my ($ary) = @_; my $is_frobly = $ary->[2];
etc -
bebyx over 2 yearsIt's good to add that using a reference to array as an array is this:
@{$arrayref}