develooper Front page | perl.perl5.porters | Postings from February 2015

Setting PL_savebegin ( done by Devel::Cover ) breaks scope destruction

Thread Next
From:
Kent Fredric
Date:
February 22, 2015 02:35
Subject:
Setting PL_savebegin ( done by Devel::Cover ) breaks scope destruction
Message ID:
CAATnKFBHuYb+T1GYGA_LTdmM8WgoFitt7vdNVZH0=zjG=QgXsA@mail.gmail.com
After much analysis, I've discovered an unfortunate problem.


The following code creates a BEGIN sub that persists until global
destruction.

{
  my $foo;
  use package $foo;
}
# $foo is still alive here

This appears to be because the above code transforms as:

{
   my $foo;
   BEGIN {
      package->import($foo);
  }
}
# $foo is still alive here because BEGIN is still alive.


And BEGIN being a sub, holds $foo alive until BEGIN itself is destroyed.

But this behaviour only happens when PL_savebegin is set to TRUE,
which only appears to occur in Devel::Cover  ( related bug
https://github.com/pjcj/Devel--Cover/issues/118  )


I'm not sure how to avoid this or what the right fix should be.

A more specific example that shows why I am trying to do this is as
follows, and it behaves as expected in all
other situations, it just fails when run under perl -MDevel::Cover , or
when dynaloading any XS code that sets PL_savebegin ( like
https://github.com/pjcj/Devel--Cover/issues/118#issuecomment-75195330 )


---
use strict;
use warnings;

use Scope::Guard;

BEGIN {
  package noop;
  sub import {
      $_[1] = Scope::Guard->new(sub {
       print "1. Finish Old Scope\n"
    });
  };
  $INC{'noop.pm'}=1;
}
{
  my $value;
  use noop $value;
}
{
    print "2. Start New Scope\n";
}
---


Expected output is

**always* *
1. Finish Old Scope
2. Start New Scope

But under PL_savebegin=true, it outputs


2. Start New Scope
1. Finish Old Scope

Because "1" happens in global destruction.

-- 
Kent

*KENTNL* - https://metacpan.org/author/KENTNL

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