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...
The side effect of this is that executing a command like this alters $! and $? (usually by setting them to zero when the command works).sub DESTROY { my $output = `some_additional_cleanup 2>&1`; ... }
The problem occured in some code kind of like this...
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.my $obj = My::Object->new(); ... if ($!) { die "something bad happened: $!"; }
It turns out if I localize these in the DESTROY everything works ok...
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.sub DESTROY { local ($!, $?); my $output = `some_additional_cleanup 2>&1`; ... }
bluto
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Devious destructor
by dragonchild (Archbishop) on Mar 12, 2002 at 19:13 UTC | |
by Juerd (Abbot) on Mar 13, 2002 at 15:29 UTC | |
|
Re: Devious destructor
by dmmiller2k (Chaplain) on Mar 13, 2002 at 18:22 UTC |