Check Error Code In Perl
Contents |
CoolUsesForPerl PerlNews Q&A Tutorials Poetry RecentThreads NewestNodes Donate What'sNew on Aug 24, 2005 at 14:25UTC ( #486200=perlquestion: print w/replies, xml ) Need Help?? bwelch has asked for the wisdom of the Perl perl error code 9 Monks concerning the following question: Using perl 5.8.0 on Solaris, I perl error code 255 sometimes use system calls to manipulate a file or directory. This has worked well in the past, perl error code 256 but this week a script failed trying to copy a file. The error message was "Illegal seek at ./script.pl line XXX", and the system call causing the error is
Perl System Error Code
here: use strict; system( "cp /analysis/fasta1.fa /analysis2/fasta1.fa" ) or die print "Can't copy fasta file: $! \n"; [download] A friend at work said that in his experience, the return code from "system" isn't reliable when used that way. He said I should capture the actual return code from the system call and evaluate it. If it's not perl return error code zero, there's an error and to print $! at that point. I followed his recommendation and the problem went away. Here's the code I used. use strict; &doSystemCommand( "cp /analysis/fasta1.fa /analysis2/fasta1.fa" ); sub doSystemCommand { my $systemCommand = $_[0]; print LOG "$0: Executing [$systemCommand] \n"; my $returnCode = system( $systemCommand ); if ( $returnCode != 0 ) { die "Failed executing [$systemCommand]\n"; } } [download] Could you tell me more about what is happening here and why this eliminated the errors? Also, could you offer improvements in this function for handling system calls?Comment on Best method to capture return code from system calls?Select or Download Code Replies are listed 'Best First'. Re: Best method to capture return code from system calls? by dave_the_m (Prior) on Aug 24, 2005 at 14:52UTC Note that system() returns a false value on success, so system(...) or ... is the wrong idiom. Your code was succeeding, then printing out a suprious error. perlfunc demonstrates the most comprehensive checking code: system(...); if ($? ==
A • B • C • D • E F • G • H • I • L M • N • O • P • S T • U perl die error code • X die Perl 5 version 24.0 documentation Go to top Show
Html Code Perl
recent pages Home > Language reference > Functions > die Please note: Many features of this site
Perl System Variables
require JavaScript. You appear to have JavaScript disabled, or are running a non-JavaScript capable web browser. To get the best experience, please enable JavaScript or download a modern web http://www.perlmonks.org/?node_id=486200 browser such as Internet Explorer 8, Firefox, Safari, or Google Chrome. Recently read die Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage die LIST die raises an exception. Inside an eval the error message is stuffed into $@ and the eval is terminated with the undefined value. If the exception is outside of all http://perldoc.perl.org/functions/die.html enclosing evals, then the uncaught exception prints LIST to STDERR and exits with a non-zero value. If you need to exit the process with a specific exit code, see exit. Equivalent examples: die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news'; chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied. Note that the "input line number" (also known as "chunk") is subject to whatever notion of "line" happens to be currently in effect, and is also available as the special variable $. . See $/ in perlvar and $. in perlvar. Hint: sometimes appending ", stopped" to your message will cause it to make better sense when the string "at foo line 123" is appended. Suppose you are running script "canasta". die "/etc/games is no good"; die "/etc/games is no good, stopped";produce, respectively /etc/games is no good at canasta l
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn http://stackoverflow.com/questions/18440315/how-to-capture-the-exit-code-of-a-shell-script-in-a-perl-script more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to capture the exit code of a shell script error code in a perl script? up vote 3 down vote favorite i want to pop up an alert box using perl script. I am using exit 0 to terminate the shell script successfully and exit 1 to terminate the shell script when an error occurs. I want to capture this exit code in the perl script. And depending on the value 0 or 1, I want to pop up an perl error code alert box with success or failure message respectively. perl shell share|improve this question edited Aug 26 '13 at 9:11 TLP 54.9k663118 asked Aug 26 '13 at 8:58 Rahul 701613 add a comment| 2 Answers 2 active oldest votes up vote 7 down vote You can check the exit code of another process with the child error variable $?. For example: system("perl foo.pl"); my $exit_val = $? >> 8; # now contains the exit value of the perl script Read the documentation for more info. share|improve this answer answered Aug 26 '13 at 9:06 TLP 54.9k663118 i used your method, but its giving 127. but i am using exit 0 in my shell script. so, i need a 0 instead of 127. –Rahul Aug 26 '13 at 10:25 @Rahul : $? & 127 will give you what you want, as explained in the linked documentation. –Zaid Aug 26 '13 at 10:55 ok i got. now how can i pop up a alert box in the perl script or using javascript inside perl. –Rahul Aug 26 '13 at 11:11 add a comment| up vote 5 down vote In case of exit 0:- shell script returns 0 to perl sc