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

Bug: DESTROY gets called at the wrong time

Thread Next
David Cantrell
February 5, 2013 14:03
Bug: DESTROY gets called at the wrong time
Message ID:
While having Evil Thoughts*, I ran into a weird little bug.  A short
discussion on the 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';
  *main::wibble = sub { print "not an object\n"; };
  $ 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

$ 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

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:

* 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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About