Perl | Exiting from a Script

exit() function evaluates the expression passed to it and exits from the Perl interpreter while returning the value as the exit value. The exit() function does not always exit immediately but calls the end routines before it terminates the program. If no expression is passed to the exit function then a default value 0 is returned. Use of exit() function is limited and should not be used to exit from a subroutine. To exit from a subroutine, die or return is used.

Syntax: exit(value)

Parameter: value which is to be returned on function call

Returns: the value passed to it or 0 if function is called without an argument

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Getting the user's bid for
# an online auction
print "Enter your bid";
$bid = <STDIN>;
  
# Exit function returns $bid
# if bid is less than 1000
if ($bid < 1000)
{
    exit $bid;
}
  
else
{
    # Prints this message if bid is 
    # greater than or equal to 1000
    print "\nThanks for Participating";
}

chevron_right


Here is how the above code works
Step 1: Get a bid value from the user.
Step 2: Exit returns bid value if the bid is less than 1000 and terminates the program.
Step 3: Prints this message if the bid is greater than or equal to 1000.

Passing Parameter to exit function
A parameter can be passed to the exit function that gets stored in the system’s variable.

Note: Value passed to the exit function can be any random value, it needs not to be any specific value.
The example below shows how to pass a value to the exit function:
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Opening a file
if(!open(fh,"<","Filename.txt"))
{
    # This block passing an error code 56 
    # to exit function indicating file 
    # could not be opened.
    print "Could not open file";
    exit 56;
}
  
# Passing a success code 1 to 
# the exit function
exit 1;

chevron_right


 

Here is how the above program works:-
Step 1: Opening a file in read mode.
Step 2: When it is unable to open a file, if block executes, that calls an exit function and passes an error code value 56 to the system.
Step 3: If opening the file is successful then it returns 1.

In order to view the values returned by an exit function on Linux/Unix $? is used. The command echo $? is executed on the terminal in order to view the value returned by an exit function.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.