Nested dereferencing arrows in Perl: to omit or not to omit?

Posted by DVK on Stack Overflow See other posts from Stack Overflow or by DVK
Published on 2010-03-19T04:46:12Z Indexed on 2010/03/19 4:51 UTC
Read the original article Hit count: 493

Filed under:
|
|
|

In Perl, when you have a nested data structure, it is permissible to omit de-referencing arrows to 2d and more level of nesting. In other words, the following two syntaxes are identical:

my $hash_ref = { 1 => [ 11, 12, 13 ], 3 => [31, 32] };

my $elem1 = $hash_ref->{1}->[1]; 
my $elem2 = $hash_ref->{1}[1]; # exactly the same as above

Now, my question is, is there a good reason to choose one style over the other?

It seems to be a popular bone of stylistic contention (Just on SO, I accidentally bumped into this and this in the space of 5 minutes).

So far, none of the usual suspects says anything definitive:

  • perldoc merely says "you are free to omit the pointer dereferencing arrow".
  • Conway's "Perl Best Practices" says "whenever possible, dereference with arrows", but it appears to only apply to the context of dereferencing the main reference, not optional arrows on 2d level of nested data structures.
  • "MAstering Perl for Bioinfirmatics" author James Tisdall doesn't give very solid preference either:

    "The sharp-witted reader may have noticed that we seem to be omitting arrow operators between array subscripts. (After all, these are anonymous arrays of anonymous arrays of anonymous arrays, etc., so shouldn't they be written [$array->[$i]->[$j]->[$k]?) Perl allows this; only the arrow operator between the variable name and the first array subscript is required. It make things easier on the eyes and helps avoid carpal tunnel syndrome. On the other hand, you may prefer to keep the dereferencing arrows in place, to make it clear you are dealing with references. Your choice."

Personally, i'm on the side of "always put arrows in, since itg's more readable and obvious tiy're dealing with a reference".

© Stack Overflow or respective owner

Related posts about perl

Related posts about syntax