develooper Front page | perl.perl5.porters | Postings from September 2016

Re: [perl #123910] `undef $0;` does not work

Thread Previous
From:
Dave Mitchell
Date:
September 29, 2016 08:30
Subject:
Re: [perl #123910] `undef $0;` does not work
Message ID:
20160929082957.GM3193@iabyn.com
On Mon, Feb 23, 2015 at 10:46:35PM +0100, Lukas Mai wrote:
> Am 23.02.2015 um 22:18 schrieb (via RT):
> > # New Ticket Created by
> > # Please include the string:  [perl #123910]
> > # in the subject line of all future correspondence about this issue.
> > # <URL: https://rt.perl.org/Ticket/Display.html?id=123910 >
> > 
> > 
> > 
> > This is a bug report for perl from porton@narod.ru,
> > generated with the help of perlbug 1.40 running under perl 5.20.1.
> > 
> > 
> > -----------------------------------------------------------------
> > [Please describe your issue here]
> > 
> > "Use of uninitialized value $0 in undef operator" is wrong.
> > 
> > $ cat z.pl
> > print "$0\n";
> > undef $0;
> > $ perl -Mstrict -Mwarnings z.pl
> > z.pl
> > Use of uninitialized value $0 in undef operator at z.pl line 2.
> 
> Well, the warning message could be better. '$0 = undef' and '$| = undef'
> have similarly misleading diagnostics ("Use of uninitialized value $| in
> scalar assignment at -e line 1.").
> 
> $ perl -we '$0 = my $x'
> Use of uninitialized value $0 in scalar assignment at -e line 1.
> 
> This one is flat out wrong: The uninitialized value is $x, not $0.
> 
> The issue is that $0 and $| are special: $0 is not a real scalar but a
> string internally (an interface to argv[0]) whereas $| is a bit flag. So any
> assignment to $0 will stringify the value in question, which for undef
> triggers a warning.
> 
> So "Use of uninitialized value $0 in scalar assignment" should really be
> "Use of uninitialized value in scalar assignment to $0", and "Use of
> uninitialized value $0 in undef operator" should be "Use of uninitialized
> value undef in $0" or something.

I've fixed 'undef $0' with v5.25.5-45-g614f2ce, but the assignment ones
are more problematic. Technically the warning is correct. In:

    $ perl -we'my $x; $0 = $x'
    Use of uninitialized value $0 in scalar assignment at -e line 1.

while executing a scalar assignment operator, perl tries to retrieve the
string value of $0 (as part of applying set magic), and finds that $0
holds an undef value.

I can't think of any way of improving the warning message within the
current framework, where warnings are of the form

    Use of uninitialized value <name of var if possible> in <description
    of currently executing op>,

without breaking a whole bunch of other useful warning cases.

My feeling is that $0 = $undef should be regarded as correct but quirky,
and this ticket closed.

-- 
This is a great day for France!
    -- Nixon at Charles De Gaulle's funeral

Thread Previous


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