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

Twisted CvOUTSIDE and lexical/anon sub issues ([perl #113930] Lexical subs )

Thread Next
From:
Father Chrysostomos via RT
Date:
August 12, 2012 18:22
Subject:
Twisted CvOUTSIDE and lexical/anon sub issues ([perl #113930] Lexical subs )
Message ID:
rt-3.6.HEAD-11172-1344820933-1830.113930-14-0@perl.org
What should this do?

sub foo {
  my $x = 23;
  my sub bar;
  sub {
    sub bar { $x } # my sub bar is in scope, so this defines its body
  };
  warn bar();
}
foo();

My first thought was that, since bar is cloned when foo is called, it
should see foo’s $x.

I could make it work that way, but implementing it is quite complicated.

bar’s CvOUTSIDE pointer points to the prototype CV for the anonymous sub
containing its definition.  Usually, when a sub is cloned, any pad entry
marked FAKE (meaning it belongs to the outer sub) just ends up
referencing the same SV that is in the outer sub’s pad, unless the outer
pad is inactive.  In this case, the anonymous prototype has an entry for
$x, but it is empty.

So the cloning code would have to follow the CvOUTSIDE chain until it
finds an active sub.

That might make the above example work, but this slightly different one
would require more complicated checks:

sub {
  my $x = 23;
  my sub bar;
  sub {
    sub bar { $x } # my sub bar is in scope, so this defines its body
  };
  warn bar();
}->();

The outermost sub is an anonymous sub.  Therefore the inner anonymous
prototype’s CvOUTSIDE pointer points to the outer prototype CV, not its
currently-running clone.  So, in addition to following CvOUTSIDE
pointers, we would also have to search the context stack for the
currently-running clone.

I don’t think it is worth it.  Both examples above should warn with
‘Variable "$x" is not available’.  Opinions?

-- 

Father Chrysostomos


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