Tux / Text-CSV_XS

perl5 module for composition and decomposition of comma-separated values
17 stars 20 forks source link

Expressions with comma or fat comma should be inside parens: #30

Closed xsawyerx closed 4 years ago

xsawyerx commented 4 years ago

perl looks for a comma within a block to disambiguate between a hashref and a block. It is also difficult to determine by humans unless there's enough code. Both can get it wrong and do.

The following is a good way to prevent ambiguous syntax:

map {   $_ => 0   } @a; # not ok - fat comma, no parens
map { ( $_ => 0 ) } @a; # ok - fat comma, parens

This subroutine has a top-level statement with braces and a fat comma. It can be a block or a hashref.

sub foo { { 1 => 2 } }        # not ok

It's hard to tell so it should be diambiguated by somehting:

sub foo { return { 1 => 2 } } # ok - disambiguated with "return"

The following is the same problem:

sub foo { 1 => 2 } # not ok

We can fix it with:

sub foo { ( 1 => 2 ) } # ok

This, the rule is: