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

Re: [perl #128709] Variable "$x" is not available triggered bylexical subs

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
August 4, 2016 11:06
Subject:
Re: [perl #128709] Variable "$x" is not available triggered bylexical subs
Message ID:
20160804110638.GD2632@iabyn.com
On Sat, Jul 30, 2016 at 11:15:43PM -0700, Father Chrysostomos via RT wrote:
> On Sat Jul 30 20:46:25 2016, aristotle wrote:
> > * Father Chrysostomos via RT <perlbug-followup@perl.org> [2016-07-24 07:24]:
> > > I’m ambivalent about how we should resolve this. Either we expand the
> > > ‘not available’ documentation in perldiag, or we change it to the
> > > other warning.
> > 
> > As I understand the “not available” warning, it really only talks about
> > the “not available *any more*” case, whereas lexical subs present a “not
> > available *yet*” situation.
> 
> It can mean ‘not available ever’:
> 
> sub {
>     my $x;
>     sub foo {
>         $x
>     }
> };
> 
> ‘Is not available’ means ‘we are creating a sub right now, but we can’t get the value from that outside variable.’  (Though, come to think of it, ‘will not stay shared’ means the same thing.

'not available' and 'not stay shared' are two different things. This:

    sub bar {
        my ($x) = @_;
        sub foo { $x }
        printf "x=%s foo=%s\n", $x, foo();
    }

    bar(1);
    bar(2);

gives:

    Variable "$x" will not stay shared at /home/davem/tmp/p line 7.
    x=1 foo=1
    x=2 foo=1

i.e. the $x captured by foo() always remains as the first instance of
bar's $x - the one that exists from bar being created through to the
first exit from a call to bar().

Prepending this to the above example:

    my sub foo;

gives:

    Variable "$x" is not available.
    x=1 foo=
    x=2 foo=

I don't know enough of the details of lexical vars to know whether that's
expected behaviour, but its certainly *different* behaviour and is thus
deserving of a different warning message.







-- 
More than any other time in history, mankind faces a crossroads. One path
leads to despair and utter hopelessness. The other, to total extinction.
Let us pray we have the wisdom to choose correctly.
    -- Woody Allen

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