Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Devious destructor

by bluto (Curate)
on Mar 12, 2002 at 18:52 UTC ( #151190=perlmeditation: print w/replies, xml ) Need Help??

I must confess that I recently got burned by doing something stupid that is probably obvious to many folks that have written DESTROY methods for their objects.

A DESTROY I wrote was using backticks to execute a command to perform some additional cleanup (not something that I could do within perl unfortunately since somebody else wrote the binary). It looked something like this...

sub DESTROY { my $output = `some_additional_cleanup 2>&1`; ... }
The side effect of this is that executing a command like this alters $! and $? (usually by setting them to zero when the command works).

The problem occured in some code kind of like this...

my $obj = My::Object->new(); ... if ($!) { die "something bad happened: $!"; }
The die would work ok, and print ok, but just before the script exitted, the values of $! and $?, which die uses to determine the exit code, were being set to zero, so the exit code turned out to be zero as well. The calling script of course was examining the exit code and thought the child sucessfully completed, and broke (silently) as a result.

It turns out if I localize these in the DESTROY everything works ok...

sub DESTROY { local ($!, $?); my $output = `some_additional_cleanup 2>&1`; ... }
This may be mentioned in the perl docs, but I didn't seen anything after a quick look. Also, I'm not sure both of these need to be localized, perhaps someone can comment about this, but wanted to cover other possible error cases, so YMMV.


Replies are listed 'Best First'.
Re: Devious destructor
by dragonchild (Archbishop) on Mar 12, 2002 at 19:13 UTC
    That's good defensive programming. If it didn't look so stupid, I'd put local $_; as the first line of every one of my subs. (For some moonlighting I might do, I will do that.) I never thought of doing that for $! and $?, neither of which I use very much. *shrugs*

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      That's good defensive programming

      It sure is!
      Defensive programming can bite you, though. Kane, one of the Rotterdam Perl Mongers once told the story about what happens if you localize $@ in a BEGIN block. By programming defensively, he ran into a bug in perl :)

      perl -e'BEGIN { local $@; print "a"; die "b"; print "c"; } print "d";'
      It turns out that the program doesn't die, and it doesn't display "b" (and not "at -e line 1"). The snippet above prints "ad". If it didn't localize $@, it would print:
      b at -e line 1. BEGIN failed--compilation aborted at -e line 1.
      Scary, huh?

      If you localize special variables, look out for bugs, and if you find any, report them!


Re: Devious destructor
by dmmiller2k (Chaplain) on Mar 13, 2002 at 18:22 UTC

    That's not the first time I've heard of negative side effects of using DESTROY (or END, for that matter).

    ++bluto for bringing this to our collective attention.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://151190]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2021-12-06 22:57 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (33 votes). Check out past polls.