develooper Front page | perl.perl5.porters | Postings from March 2017

Re: [perl #130328] "my" variable appears to inadvertently act as"state" variable

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
March 27, 2017 17:18
Subject:
Re: [perl #130328] "my" variable appears to inadvertently act as"state" variable
Message ID:
20170327171746.GS3342@iabyn.com
On Thu, Dec 15, 2016 at 01:21:52PM +0100, Sawyer X wrote:
> 
> 
> On 12/14/2016 06:05 PM, Matthew Horsfall (alh) wrote:
> > On Wed, Dec 14, 2016 at 9:08 AM, David Mertens <dcmertens.perl@gmail.com> wrote:
> >> I came upon this behavior completely unexpectedly. Would it be feasible to
> >> warn for *any* post-conditional on "my"? I suspect that the unpredictable
> >> value for a false post-conditional if-statement is almost never wanted by
> >> the programmer, except for the state emulation that you mention.
> > This is something I have been working on (slowly). It would go faster
> > if I just said "for now we will warn for the easy-to-detect general
> > case" -- maybe that's enough?
> >
> > But otherwise you can bury variable declarations deep in the op tree
> > of a single statement and detecting them right gets a little tricky.
> >
> > I think I've figured out what needs doing but I don't see having it in
> > time for 5.26 (unless we pick the easy route for now).
> 
> Iterations FTW?
> 
> Seriously though, nothing wrong with a start that catches some, if not all.

There's some history to 'my $x if ...' deprecations.

Back in 2004, when I were a lad, there was this thread:

    http://nntp.perl.org/group/perl.perl5.porters/88428
    Subject: Thoughts about my $x = $foo if $bar

which led to these commits of mine:

    perl-5.8.0-3308-g76df5e8 remove C<my $x if foo> construct from
                                core modules
    perl-5.8.0-3313-gdc9aa44 add deprecation warning for C<my $x if
                                foo> and C<foo && (my $x)>
    perl-5.8.0-3316-gedd7382 Add tests for the C<my $x if foo>
                                deprecation, and change the warning text

Then there was a later thread,

    http://nntp.perl.org/group/perl.perl5.porters/89114
    Subject: warning for my $x if 0

which decided that the general deprecation was wrong (and the
implementation was wrong too: a simple 'my $x if 0' didn't warn),
so the code from the earlier thread was reverted, and I added
these instead:

    perl-5.8.0-3389-g722969e retract 22328 and 22332: deprecation
                                warning for my $x if foo
    perl-5.8.0-3391-g7921d0f add deprecation warning for my $x if 0

I can't find any discussion as to why the first set of patches was such a
bad idea, but in the first thread, someone did suggest a valid use case:

    $cond = ...;
    my $only_needed_for_cond = foo() if $cond;
    ....
    if ($cond) {
        ... do something with $only_needed_for_cond ...;
    }

Also, we may need to distinguish between

    my $x if $cond;
    my $x = ... if $cond;

We may want to deprecate one but not the other.

-- 
In economics, the exam questions are the same every year.
They just change the answers.

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