develooper Front page | perl.perl5.porters | Postings from June 2021

Re: RFC - Issue a warning "-np better written as -p"

Thread Previous | Thread Next
From:
Ricardo Signes
Date:
June 18, 2021 16:07
Subject:
Re: RFC - Issue a warning "-np better written as -p"
Message ID:
40ea5736-b944-40dc-8d33-06bf028a84bb@beta.fastmail.com
I am opposed to this change.

Below is my rationale.

On Wed, Jun 16, 2021, at 4:51 AM, Nicholas Clark wrote:
>     perlrun says:
>     > A -p overrides a -n switch.
>     
>     OK, but `-pnw` should warn:
>     "-np better written as -p"
> […]
>     just like it warns:
>     ```
>     $ perl -awe 'print @F[1]'
>     Scalar value @F[1] better written as $F[1] at -e line 1.
> […]
> We explicitly document that `-p` overrides `-n`. Calling the `perl` binary with both is not correct - the `-n` will be ignored. We could help users better by reporting their mistake to them, if they have opted into warnings.

Using -p and -n is not causing one to be ignored.  "-p" is "-n and also more".  The gain to not writing both is saving a single (or perhaps 2-3) keystrokes.  If the user has written both, their program will work just as well as one with only one.  Removing one would require *more* work.  Also, we'd need a warning message sufficiently clear that the user knew, immediately, to go remove the "n", but also that no change was really *required*.  The benefit is very close to nil.

This is *not* the same as the @F[1] warning.

~$ perl -wE 'our @A = (1,2,3); say @A[0]'
Scalar value @A[0] better written as $A[0] at -e line 1.
1

Fine, no different, right?  Actually, there's a massively important one that's simply not demonstrated by that code snippet:"

~$ perl -wE 'our @A; @A[0] = foo(); sub foo { warn wantarray ? "1\n" : "0\n" }'
1
~$ perl -wE 'our @A; $A[0] = foo(); sub foo { warn wantarray ? "1\n" : "0\n" }'
0

Using @A[0] creates a list context.  Using $A[0] creates a scalar context.  It's a setup for potentially deep confusion based on sigil variance, worth the warning.

-- 
rjbs
Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About