newkij1 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
Let say I call a subroutine inside a script as

callingsubroutine();
**************************************
and subroutine is defined as

sub callingsubroutine
{
Something...
exit -1
}

My Question: What's the meaning of exit -1 here ?

Thanks :D

  • Comment on "Subroutine basics" What does -1 mean ?

Replies are listed 'Best First'.
Re: "Subroutine basics" What does -1 mean ?
by toolic (Bishop) on May 29, 2014 at 15:00 UTC
    exit stops your script and returns -1 in your case. Conventionally, a non-zero exit status would mean that your script was unsuccessful.

      Conventionally, a non-zero exit status would mean that your script was unsuccessful.

      This is true under Unix and Linux. Not necessarily under other OS's.

Re: "Subroutine basics" What does -1 mean ? (not subroutine)
by LanX (Saint) on May 29, 2014 at 15:04 UTC
    You already got the right explanation concerning exit, but you seem to be victim of a misunderstanding.

    exit has no special meaning within subroutines like e.g. return does, you can use it everywhere.

    It just terminates the whole program, if you want a "super-return" on a meta level.

    The '-1' is just the "exit code" given to the calling process.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: "Subroutine basics" What does -1 mean ?
by Corion (Patriarch) on May 29, 2014 at 15:01 UTC

    Have you looked at exit?

    Basically, this ends the script and returns the exit code -1 to the calling process.

Re: "Subroutine basics" What does -1 mean ?
by jeffa (Bishop) on May 29, 2014 at 15:28 UTC

    "What's the meaning of exit -1 here ?"

    No one has explicitly stated this yet, but the only way to know the answer is to ask the person who designed the code. Convention would say that return value should signal that the subroutine call failed in some way, but who is to say that the person who created this code was following convention? Your best hint is to inspect all the lines of code that call that subroutine and find a common pattern. Once you figure that out, remove the silly return codes and add some REAL exceptions instead.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Add some real exceptions to the process handling of the OS? What shell do you run? Can I try it?

Re: "Subroutine basics" What does -1 mean ?
by Anonymous Monk on May 29, 2014 at 22:04 UTC
    exit -1 means that the author is lazy. What you are "supposed" to do is return a meaningful error code to the operating system. No OS that I'm aware actually does anything very useful with that exit code so we all get lazy and just use -1. Not to mention that error codes are generally non-portable and therefore pretty useless.