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:
# 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"; } |
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:
# 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; |

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.
Recommended Posts:
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Measuring script execution time in PHP
- How do you run JavaScript script through the Terminal?
- What purpose does a <script> tag serve inside of a <noscript> tag?
- Why split the <script> tag when writing it within document.write() method in JavaScript ?
- How to get the path of current script using Node.js ?
- How to import config.php file in a PHP script ?
- Why PHP script does not create a directory with 777 permissions ?
- Get Your System Information - Using Python Script
- Importing Data in R Script
- How to terminate a script in JavaScript ?
- How to check whether a script is running under Node.js or not ?
- Perl | split() Function
- Perl | chomp() Function
- Perl | Backtracking in Regular Expression
- Perl | Searching in a File using regex
- Perl | Operators | Set - 1
- Perl | lt operator
- Perl | chop() Function
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.

