CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
problem in error handling.

 



ajitnavasare
Novice

Nov 20, 2009, 4:15 AM

Post #1 of 5 (601 views)
problem in error handling. Can't Post

I have a problem in error handling.

I want to stop the script as soon as it get an error and stop further execution.
In my below “sample script”, step 1 throws an error, and still step 2 get executed.
Can any one help me out, I want to do it in the following manner.
As on step 1 get error I want to stop the execution of script.

i think i am not using(or not aware of ) proper perl error handling features Unimpressed

Sample Script :
### Script : Start
use strict;
use warnings;
use Sys::HostIP;
use Win32::GuiTest qw[:ALL];
eval
{
system("start c:\\windows\\system32\\cmd1212.exe"); ### step 1
SendKeys(" dir {ENTER}"); ### Step 2
sleep 3;
};
if($@)
{
print "$@"
}
### Script : End
Thanks in Advance.


toolic
User

Nov 20, 2009, 5:48 AM

Post #2 of 5 (598 views)
Re: [ajitnavasare] problem in error handling. [In reply to] Can't Post

If you want your program to die if the system call returns an error, you should not put the system inside the eval block. Also,
use die:


Code
system("start c:\\windows\\system32\\cmd1212.exe") and die "some useful message";



I updated my untested code above due to FishMonger's astute observation.



Code
perldoc -f system 
perldoc -f eval
perldoc -f die



(This post was edited by toolic on Nov 20, 2009, 6:01 AM)


FishMonger
Veteran / Moderator

Nov 20, 2009, 5:57 AM

Post #3 of 5 (594 views)
Re: [toolic] problem in error handling. [In reply to] Can't Post


In Reply To
If you want your program to die if the system call returns an error, you should not put the system inside the eval block. Also,
use die:


Code
system("start c:\\windows\\system32\\cmd1212.exe") or die "some useful message";



Code
perldoc -f system 
perldoc -f eval
perldoc -f die



The system function will return the exit code of the command, which is 0 on success. So the proper error handling would be along the lines of:

Code
system("start c:\\windows\\system32\\cmd1212.exe") == 0 or die "some useful message";



7stud
Enthusiast

Nov 20, 2009, 6:01 AM

Post #4 of 5 (591 views)
Re: [toolic] problem in error handling. [In reply to] Can't Post

Can system() cause an error?


FishMonger
Veteran / Moderator

Nov 20, 2009, 6:13 AM

Post #5 of 5 (587 views)
Re: [7stud] problem in error handling. [In reply to] Can't Post


In Reply To
Can system() cause an error?


Since system is a built-in function, if it "causes" an error, then your Perl installation is corrupt.

The adjustment I made compares the return code of the system call to 0 and if they are not equal, then the command being executed failed, not Perl's system function.

See `perldoc -f system`
http://perldoc.perl.org/functions/system.html


(This post was edited by FishMonger on Nov 20, 2009, 6:14 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives