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

Share:
10,184

Related videos on Youtube

shift66
Author by

shift66

Updated on June 04, 2022

Comments

  • shift66
    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
    silbana about 12 years
    Please do not confuse pointers and references. *(p + 3) is perfectly valid in C (so long as p 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
    silbana about 12 years
    Subroutine arguments are always lists. If you do func(@ary), the contents of @ary are passed as a flat list. Inside of func, you know how many arguments were passed (because the arguments are in @_). If you do func(\@ary), then func receives a list consisting of a single argument. You can access the third element of the anonymous array to which the first argument to func 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
    bebyx over 2 years
    It's good to add that using a reference to array as an array is this: @{$arrayref}