Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are @{$list_ref} and @$list_ref equivalent in Perl?

I am new to Perl and am curious whether @{$list_ref} and @$list_ref are perfectly equivalent.

They seem to be interchangeable, but I am nervous that there is something subtle going on that I may be missing.

like image 528
Frank Krueger Avatar asked Dec 09 '09 23:12

Frank Krueger


2 Answers

Yes, they're equivalent. You need braces when the expression is more than a simple scalar variable, e.g.,

push @{ $foo{$bar} } => "baz";

For more detail, see the Using References section of the documentation on references. The standard Perl documentation also includes several tutorials on using references:

  • Understand References Today (mentioned by hobbs in the question's comments)
  • Manipulating Arrays of Arrays in Perl
  • Perl Data Structures Cookbook
like image 159
Greg Bacon Avatar answered Nov 17 '22 03:11

Greg Bacon


I've always found it helpful to remember that the outer braces are not syntactical magic, they're just a block that returns a reference. The expression inside the block can be anything that returns a reference:

$ perl -le 'sub foo {[qw/ apple orange banana /]} print ${print "Do something here."; foo()} [1]'
Do something here.
orange
like image 45
converter42 Avatar answered Nov 17 '22 01:11

converter42