3.4 Dropping Those Braces


Most of the time, the dereferenced array reference is contained in a simple scalar variable, such as @{$items} or ${$items}[1] . In those cases, the curly braces can be dropped, unambiguously, forming @$items or $$items[1] .

However, the braces cannot be dropped if the value within the braces is not a simple scalar variable. For example, for @{$_[1]} from that last subroutine rewrite, you can't remove the braces.

This rule also means that it's easy to see where the "missing" braces need to go. When you see $$items[1] , a pretty noisy piece of syntax, you can tell that the curly braces must belong around the simple scalar variable, $items . Therefore, $items must be a reference to an array.

Thus, an easier-on-the-eyes version of that subroutine might be:

 sub check_required_items {   my $who = shift;   my $items = shift;   my @required = qw(preserver sunscreen water_bottle jacket);   for my $item (@required) {     unless (grep $item eq $_, @$items) { # not found in list?       print "$who is missing $item.\n";     }   } } 

The only difference here is that the braces were removed for @$items .



Learning Perl Objects, References & Modules
Learning Perl Objects, References, and Modules
ISBN: 0596004788
EAN: 2147483647
Year: 2003
Pages: 199

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net