Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > In a Perl script 'exit 1' returns exit status 0!

Reply
Thread Tools

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

 
 
kaleem
Guest
Posts: n/a
 
      12-08-2006
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?

 
Reply With Quote
 
 
 
 
Billy N. Patton
Guest
Posts: n/a
 
      12-08-2006
kaleem wrote:
> 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;
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      12-09-2006
Michele Dondi <(E-Mail Removed)> wrote:
> On 8 Dec 2006 10:07:27 -0800, "kaleem" <(E-Mail Removed)> wrote:
>
>>Subject: In a Perl script 'exit 1' returns exit status 0!

>
> 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'

:=)


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
kaleem
Guest
Posts: n/a
 
      12-09-2006
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 Dondi wrote:
> On 8 Dec 2006 10:07:27 -0800, "kaleem" <(E-Mail Removed)> wrote:
>
> >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,


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      12-09-2006
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().

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      12-09-2006
On Fri, 8 Dec 2006 18:07:23 -0600, Tad McClellan
<(E-Mail Removed)> wrote:

>> 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'


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
--
{$_=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,
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      12-10-2006
Billy N. Patton wrote:

> 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
 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      12-13-2006
On 8 Dec 2006 10:07:27 -0800, kaleem <(E-Mail Removed)> wrote:

> #!/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.


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-14-2006
Dave Weaver <(E-Mail Removed)> wrote:
> On 8 Dec 2006 10:07:27 -0800, kaleem <(E-Mail Removed)> wrote:
>
>> #!/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



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...


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Any exit status without explicitely using return /exit Keith Thompson C Programming 10 03-03-2010 12:41 AM
Re: Any exit status without explicitely using return /exit jacob navia C Programming 3 02-24-2010 02:39 PM
Can perl start another perl script and then exit? Bill H Perl Misc 3 09-18-2007 12:07 AM
Exit status from perl script Adam-the-Kiwi Perl Misc 2 05-04-2005 11:11 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments