In a Perl script 'exit 1' returns exit status 0!

K

kaleem

Hello,

I'm calling 'exit 1' inside a Perl script. When the script terminates
and I check the exit status using 'echo $?', I get 0! Can someone
explain what exactly is happening here and whether it's a Perl bug. The
details follow:

Platform: Linx 2.6 on IA64

Here's the script (read between the lines :)):

----------------------------------------------------------------
#!/usr/bin/perl -w

BEGIN {
push(@INC, "/diag-tools/mmii-diags/perlmod"); # The path where
Expect.pm module is located
}
use Expect;

our $exp;
$exp = new Expect;
$exp->raw_pty(0);
our $debug_mode = 1;
$exp->log_stdout($debug_mode);
$exp->spawn("ksh");
#$exp->hard_close();
exit 1;
---------------------------------------------------------------------

If I comment '$exp->hard_close()', the status returned is 0! If I
uncomment '$exp->harc_close()', the status returned is 1. I check
return status using 'echo $?'

So what seems to be happening is that if I don't close the command
spawned by Expect, someone is changing $? to 0 and that's how my return
status becomes 0.

Can you tell me who exaclty is doing this and why?
 
B

Billy N. Patton

kaleem said:
Hello,

I'm calling 'exit 1' inside a Perl script. When the script terminates
and I check the exit status using 'echo $?', I get 0! Can someone
explain what exactly is happening here and whether it's a Perl bug. The
details follow:

Platform: Linx 2.6 on IA64

Here's the script (read between the lines :)):

----------------------------------------------------------------
#!/usr/bin/perl -w

BEGIN {
push(@INC, "/diag-tools/mmii-diags/perlmod"); # The path where
Expect.pm module is located
}
use Expect;

our $exp;
$exp = new Expect;
$exp->raw_pty(0);
our $debug_mode = 1;
$exp->log_stdout($debug_mode);
$exp->spawn("ksh");
#$exp->hard_close();
exit 1;
---------------------------------------------------------------------

If I comment '$exp->hard_close()', the status returned is 0! If I
uncomment '$exp->harc_close()', the status returned is 1. I check
return status using 'echo $?'

So what seems to be happening is that if I don't close the command
spawned by Expect, someone is changing $? to 0 and that's how my return
status becomes 0.

Can you tell me who exaclty is doing this and why?
Try putting a warn before and after $exp->hard_close()
What you'll probably see is that is never gets to the exit 1;
 
T

Tad McClellan

Michele Dondi said:
I thought 0! *is* 1...


No, you silly goose, 0! is a syntax error.

!0 however, *is* 1 (for now anyway):

perl -le 'print !0'

:=)
 
K

kaleem

I'm using '!' as exclamation mark. So it's just 0. Additional info:

Taken from Programming Perl:

"We said that the exit function exits immediately, but that was a
bald-faced lie. It exits as soon as possible, but first it calls any
defined END routines for at-exit handling. These routines cannot abort
the exit, although they can change the eventual exit
value by setting the $? variable. Likewise, any class that defines a
DESTROY method will invoke that method on behalf of all its
objects before the real program exits. If you really need to bypass
exit processing, you can call the POSIX module's _exit function
to avoid all END and destructor processing. And if POSIX isn't
available, you can exec "/bin/false" or some such."

I tried POSIX's _exit and it works fine; the retrun status is 1 after
the script terminates.

So, as the Programming Perl says someone seems to be changing $? to 0
so that I get exit
status '0' even if I call 'exit 1'. I'd like to know where exactly is
this happening and why.

Also, please note that if I don't spawn a process and just 'exit 1',
the exit status is 1. So only
after I spawn a process using the spawn method of $exp object and don't
call hard_close()
on it before exiting, I get the exit status as 0.

Thanks.


Michele said:
Subject: In a Perl script 'exit 1' returns exit status 0!

I thought 0! *is* 1...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
D

Dr.Ruud

kaleem schreef:
If I comment '$exp->hard_close()', the status returned is 0! If I
uncomment '$exp->harc_close()', the status returned is 1. I check
return status using 'echo $?'

So maybe the "hard_close()" results in an 'exec' that replaces your
process, for example.
Have you checked the source of Expect? Which version? Have you switched
on extensive logging?

The current DESTROY() looks at "do_soft_close", maybe just leave that at
(or if you have an old version, set that to) 0 and let DESTROY() do the
hard_close()?
See also what's mentioned on $? inside DESTROY().
 
M

Michele Dondi

No, you silly goose, 0! is a syntax error.

!0 however, *is* 1 (for now anyway):

perl -le 'print !0'

Weren't we talking Perl 6?!?

pugs> sub postfix:<!> (Int $n) { [*] 1..$n }
undef
pugs> say $_! for ^6
1
1
2
6
24
120
undef


Michele
 
J

Joe Smith

Billy said:
Try putting a warn before and after $exp->hard_close()
What you'll probably see is that is never gets to the exit 1;

Proof that exit(0) can return nonzero:

linux% cat test.pl
#!/usr/bin/perl
END { warn "Setting \$? to 3"; $? = 3; }
print "Doing an exit(0) here\n";
exit 0;
linux% perl test.pl; echo $status
Doing an exit(0) here
Setting $? to 3 at test.pl line 2.
3
 
D

Dave Weaver

#!/usr/bin/perl -w

BEGIN {
push(@INC, "/diag-tools/mmii-diags/perlmod"); # The path where
Expect.pm module is located
}
use Expect;

Nothing to do with your question, but you may wish to know that:

use lib '/diag-tools/mmii-diags/perlmod';
use Expect;

will do the same thing as your quoted code above and is a little
easier on the eye.
 
T

Tad McClellan

Dave Weaver said:
Nothing to do with your question, but you may wish to know that:

use lib '/diag-tools/mmii-diags/perlmod';
use Expect;

will do the same thing


will do _almost_ the same thing

as your quoted code above and is a little
easier on the eye.


Examine @INC after each of them...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top