Perl-Critic / PPI

53 stars 44 forks source link

PPI differs from perl: // parseed as defined-or instead of regexp #302

Open happy-barney opened 1 hour ago

happy-barney commented 1 hour ago

In expression identifier // (when identifier is not method call), Perl treats // as:

For example, parsing ref // 1:

But current result should be preserved for parsing eg: my sub ref (); ref // 1

wchristian commented 1 hour ago

Can you provide the example code as valid perl? I get this error trying to compile it:

$ perl -e 'ref // 1'
Warning: Use of "ref" without parentheses is ambiguous at -e line 1.
Number found where operator expected at -e line 1, near "// 1"
        (Missing operator before  1?)
syntax error at -e line 1, near "// 1"
Execution of -e aborted due to compilation errors.
happy-barney commented 1 hour ago

valid perl will be:

wchristian commented 1 hour ago

In those examples Perl seems to treat // as the undef-or operator.

$ cat marp.pl
sub foo :prototype() {}; print(foo // 1)
$ perl marp.pl
1
$ cat marp.pl
sub foo :prototype() {2}; print(foo // 1)
$ perl marp.pl
2
wchristian commented 1 hour ago

Ah hm, it doesn't seem to for the first example. Impressively odd behavior.

Nevermind that, in the ref example it also treats it as the undef-or operator, because ref returns the empty string "".

$ perl -e 'print(length(ref () ))'
0
$ perl -e 'print(defined(ref () ))'
1
wchristian commented 1 hour ago

So, in conclusion: Right now i need an example of working Perl code where // is treated by Perl as a regex match, and evidence for this being the case. :)

happy-barney commented 1 hour ago

OK, probably some clarification: ref // 1 type of expressions will always be invalid. This issues is only about treating objects like Perl does.

IMHO it is fine when PPI creates broken expressions from invalid code. It should not create valid expression from invalid code. (using current behaviour creates valid expression of potential interpreter using PPI to parse code similar to Perl.

wchristian commented 1 hour ago

Note that you wrote:

Perl treats // as: [...] regexp-match otherwise

Can you clarify what your evidence for this is? (I find it hard to understand, given that Perl just crashes when presented with such code, instead of actually running it.)

happy-barney commented 1 hour ago

Note that you wrote:

Perl treats // as: [...] regexp-match otherwise

Can you clarify what your evidence for this is?

https://github.com/Perl/perl5/issues/18117