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

Discussion in 'Perl Misc' started by kaleem, Dec 8, 2006.

  1. kaleem

    kaleem Guest

    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?
    kaleem, Dec 8, 2006
    #1
    1. Advertising

  2. 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;
    Billy N. Patton, Dec 8, 2006
    #2
    1. Advertising

  3. Michele Dondi <> wrote:
    > On 8 Dec 2006 10:07:27 -0800, "kaleem" <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 9, 2006
    #3
  4. kaleem

    kaleem Guest

    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" <> 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,
    kaleem, Dec 9, 2006
    #4
  5. kaleem

    Dr.Ruud Guest

    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."
    Dr.Ruud, Dec 9, 2006
    #5
  6. On Fri, 8 Dec 2006 18:07:23 -0600, Tad McClellan
    <> 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,
    Michele Dondi, Dec 9, 2006
    #6
  7. kaleem

    Joe Smith Guest

    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
    Joe Smith, Dec 10, 2006
    #7
  8. kaleem

    Dave Weaver Guest

    On 8 Dec 2006 10:07:27 -0800, kaleem <> 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.
    Dave Weaver, Dec 13, 2006
    #8
  9. Dave Weaver <> wrote:
    > On 8 Dec 2006 10:07:27 -0800, kaleem <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 14, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,773
  2. jacob navia
    Replies:
    3
    Views:
    543
    Nick Keighley
    Feb 24, 2010
  3. Keith Thompson
    Replies:
    10
    Views:
    677
    Tim Rentsch
    Mar 3, 2010
  4. Adam-the-Kiwi

    Exit status from perl script

    Adam-the-Kiwi, May 3, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    406
  5. Bill H
    Replies:
    3
    Views:
    131
    Bill H
    Sep 18, 2007
Loading...

Share This Page