Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array References

  • @array
  • ARRAY

To solve the problem we will use references. Prefixing the array with a back-slash \ creates a reference to it.

my $names_ref  = \@names;
print $names_ref;      # ARRAY(0x703dcf2)
@$names_ref

but it will be probably more readable to write

@{$names_ref}

or even

@{ $names_ref }

Once we know all this we can pass a reference to a function, within the function we can dereference the array and we get back the original array.

#!/usr/bin/env perl
use strict;
use warnings;

my @names = qw(Foo Bar Baz);

my $names_ref  = \@names;
print "$names_ref\n";         # ARRAY(0x703dcf2)

print "@$names_ref\n";        # Foo Bar Baz
print "@{ $names_ref }\n";    # Foo Bar Baz


print "$names[0]\n";          # Foo
print "$names_ref->[0]\n";    # Foo