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

Bug: DESTROY gets called at the wrong time

Thread Next
From:
David Cantrell
Date:
February 5, 2013 14:03
Subject:
Bug: DESTROY gets called at the wrong time
Message ID:
20130205140342.GA19941@bytemark.barnyard.co.uk
While having Evil Thoughts*, I ran into a weird little bug.  A short
discussion on the london.pm mailing list convinced me that this is a
perl bug:

  $ cat foo
  sub Sub::DESTROY {
      print "I'm destroying a Sub\n";
  }
  
  *main::wibble = bless sub { print "called the object\n" }, 'Sub';
  wibble();
  *main::wibble = sub { print "not an object\n"; };
  wibble();
  $ perl-5.16.2/bin/perl foo
  called the object
  not an object
  I'm destroying a Sub

Note that DESTROY is called too late.

There is a similar problem with blessed scalars (this example from
http://london.pm.org/pipermail/london.pm/Week-of-Mon-20130128/023376.html):

$ perl -Mstrict -wE 'sub Foo::DESTROY { say "Goodbye, cruel world" };
*foo = do { bless \(my $msg = "Message one"), "Foo" }; say $::foo; *foo
= do { bless \(my $msg = "Message two"), "Foo" }; say $::foo;'
Message one
Message two
Goodbye, cruel world
Goodbye, cruel world

but blessed hashes and arrays behave as expected.

Nicholas Clark says later in that thread that it's a dodgy optimisation,
and Matt Lawrence says that this may also cause problems with subroutine
attributes.

I wonder if this ties in with the Devel::Cover problem I reported a few
weeks ago, which prompted PJCJ to post this to p5p:
  http://www.nntp.perl.org/group/perl.perl5.porters/2012/12/msg196269.html

* oh, and the Evil Thought? I wondered if it would be practical to have
  a blessed subroutine that, when someone tried to replace it, would,
  through a magic DESTROY method, put itself back in the symbol table.

-- 
David Cantrell | top google result for "topless karaoke murders"

    There are many different types of sausages.  The best are
    from the north of England.  The wurst are from Germany.
      -- seen in alt.2eggs...

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