|00:01||Welcome to the Spoken Tutorial on Exception and error handling in PERL.|
|00:06|| In this tutorial, we will learn to:
Catch errors and Handle exceptions.
|00:12|| For this tutorial, I am using:
Ubuntu Linux 12.04 operating system Perl 5.14.2 and the gedit Text Editor.
|00:23||You can use any text editor of your choice.|
|00:27||To follow this tutorial, you should have working knowledge of Perl programming.|
|00:32||If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website.|
|00:39||When an error occurs, Exception handling deviates the execution of a program from the normal execution path.|
|00:47||Error handling helps to recover the program, without terminating the application.|
|00:53||We can identify and trap an error in a number of ways. We will see few commonly used methods in Perl.|
|01:01||The warn function only raises a warning message without taking further action.|
|01:07||The die function immediately terminates the execution and displays the error message.|
|01:13||Let us understand the die function using a sample program which I have already saved.|
|01:20||Go to the terminal and type: gedit die dot pl ampersand and press Enter.|
|01:29||This is the code in 'die.pl' file. Let us understand the code now.|
|01:35|| Here, we have defined a function divide which takes two input parameters
i.e dollar numerator and dollar denominator.
|01:46||At the rate underscore (@_) is a special variable used to pass the parameter list to the function.|
|01:53||If the denominator is zero, the die function will quit the script.|
|01:57||It will also display the error message for the user to read. Else, it will print the output.|
|02:05||These are the function call statements.|
|02:08||The first two times, the function is executed because the second parameter is not zero.|
|02:15||The third time, the denominator value is zero. So, the die function is executed.|
|02:23||The last divide function will not be executed as the die function quits the script.|
|02:29||Press Ctrl + S to save the program.|
|02:32||Let us execute the program.|
|02:35||Switch back to the terminal and type: perl die dot pl and press Enter.|
|02:43|| The output is displayed as shown here.
"Can't divide by zero!"
|02:49||This is the error message we have given in the program, in the die statement.|
|02:54||Next, we will see how to use eval function in error handling.|
|03:00||eval function is used for handling run-time errors or exceptions.|
|03:06||For example, built-in errors such as out of memory, divide by zero or user defined errors.|
|03:14||The general syntax for eval function is shown here.|
|03:19||The dollar exclamation($!) special variable holds the error message, if any.|
|03:25||Otherwise, dollar exclamation( $!) holds an empty string. That means it is evaluated as false.|
|03:33|| Let us understand the eval function using a sample program.
Go to the terminal.
|03:40||Type: gedit eval dot pl ampersand and press Enter.|
|03:47||In the eval dot pl file, type the following code as displayed on the screen. Let me explain the code now.|
|03:54||Here, in our example,open FILE invokes the die statement, if it has trouble in opening a file “test.dat”.|
|04:05||Perl gives the system error message from the last eval block to the variable dollar exclamation( $!).|
|04:13||Press Ctrl + S to save the file.|
|04:17||Switch back to the terminal and type: perl eval dot pl and press Enter.|
|04:25||The system error message is displayed as shown here.|
|04:30||Let us see another example. This time, we will see an error message returned from eval function using '$@' (dollar at the rate).|
|04:40||Let us switch back to the eval dot pl file.|
|04:44||Type the code as shown on the screen.|
|04:48||We are passing $total, $count as input parameters to the function average.|
|04:56||We have a possibility of getting an error if the count is zero.|
|05:00||Here, that is handled with the die statement.|
|05:04||The error message returned from eval is displayed using $@ ( dollar at the rate).|
|05:11||If not, it will print the Average value.|
|05:15||Press Ctrl +S to save the file. Let us execute the program.|
|05:22||Switch back to the terminal and type: perl eval.pl and press Enter.|
|05:31||The output is as shown here.|
|05:35||This brings us to the end of this tutorial. Let us summarize.|
|05:41||In this tutorial, we have learnt how to:
Catch errors and Handle exceptions.
|05:47|| As an assignment do the following.
On your Linux machine, create a file 'emp.txt' with 5 employee names.
|05:57||Change permission of 'emp.txt' to READ only.|
|06:02||Note: Go through the relevant Linux spoken tutorials on the spoken tutorial website for change permission option.|
|06:10||Write a Perl program to open the 'emp.txt' file in WRITE mode and add few employee names to it.|
|06:19||Using "eval", print appropriate error message if open/write operation fails.|
|06:26||The video at the following link summarizes the Spoken Tutorial project. Please download and watch it.|
|06:33|| The Spoken Tutorial Project team:
conducts workshops using spoken tutorials and gives certificates on passing online tests.
|06:42||For more details, please write to us.|
|06:46||Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.|
|06:53||More information on this mission is available at this link.|
|06:58||This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.|