I was surprised to find that a DESTROY handler called when an object goes out of scope changes the exit status of the child process it's running in, even if that child exits with exit(exit_code).
This code:
package Foo;
use strict;
use warnings;
sub new {
bless {}, shift;
}
sub DESTROY {
warn "$$: destroy";
system("waaaah");
}
package main;
use strict;
use warnings;
use POSIX ":sys_wait_h";
my $foo = Foo->new();
my $pid = fork();
die "fork failed" if !defined $pid;
if( $pid ) {
# parent
print "$$: parent\n";
} else {
# child
print "$$: child\n";
# system("waaaah");
exit 5;
}
my $reaped = waitpid($pid, 0);
my $child_exit_status = POSIX::WEXITSTATUS($?);
print "pid=$reaped status: $child_exit_status\n";
shows
pid=4778 status: 255
although the child exits with exit(5). So the failing system() call in the DESTROY method of the object that goes out of scope overrides the explicitly set exit() value?
(If you comment out the system() call in the DESTROY handler and uncomment the one in the child code you'll get the expected exit code 5).
Surprised?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.