Perl-Critic / PPI

54 stars 44 forks source link

parse list-embedded curlies as hash constructors #223

Closed s-nez closed 5 years ago

s-nez commented 6 years ago

This change fixes parsing of constructs like these:

foo({ %hash, key => 1 })
$ref = ({ %hash, key => 1 })

which were previously erroneously recognized as compound statements. It also fixes the original compound regression test, added in 0f74bdc1aa05fd5732c85d44e315046568e65641 as a failing test (since eval is not a compound statement as per perlsyn).

The logic is based on the assumption that curly braces inside parentheses are constructors unless they are arguments to one of the block built-ins (map, grep, sort, eval and do). Subs with the & prototype do not compile with parentheses and neither do loose blocks:

$ perl -e 'sub foo (&) { $_[0]->() } foo({ print "Hello" })'
Type of arg 1 to main::foo must be block or sub {} (not anonymous hash ({})) at -e line 1, near "})"
Execution of -e aborted due to compilation errors.
$ perl -e '({ print "Hello"; })'
syntax error at -e line 1, near "; }"
Execution of -e aborted due to compilation errors.

This also fixes issue #68.

wchristian commented 5 years ago

Thanks for the patch, this is released as https://metacpan.org/release/MITHALDU/PPI-1.248