Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error handling

Status
Not open for further replies.

newtoperl

Technical User
Sep 5, 2003
5
US
Hi all,

In ksh, there is a "trap" command which can trap returning value from each shell scripts and invoke an error handling routine when return value != 0.

For Example :

trap `error_routine`.....
test1.sh
test2.sh

Is there anything serving the same function in Perl?
so that I don't need to write like the following :

test1.sh || die "error in test1.sh";
test2.sh || die "error in test2.sh";

thanks
 
I understand your question to mean you are running a perl program that runs some shell scripts and you want to trap the output?

try this:
[tt]
my $output1 = `/path/to/test1.sh`;
my $output2 = `/path/to/test2.sh`;
[/tt]
or:
[tt]
`/path/to/test1.sh` || &whinge($!);

sub whinge {
my $error = $_[0];
print "The error was $error\n";
}
[/tt]
fortytwo
will@hellacool.co.uk
 
thanks fortytwo

it is not quite what I'm looking for.

In ksh, there is a trap command to handle signals

For Example
===========
trap "echo Ctrl-C not supported; exit" 2
means
whenever user press Ctrl-C, this message will display

trap "echo error from program; exit" ERR
means
whenever a program within this shell exit with non-zero, this message will display

with this function, I don't need to check the return value from each step as my Perl script will involved over 100 steps.

I found something like sigtrap in Perl, I'm checking if it can do what I want.
 
Hi NewToPerl,

There are a number of error and signal trapping tools in Perl. Among them 'die' and 'warn', as well as 'carp', 'croak' and confess from 'carp.pm'.

But I think you especially might want to examine the use of 'eval'.

Here is a snippet of code that is pretty much ripped off from 'Perl by Example' by Ellie Quigley, 2nd Ed. (See sectiohn 12.5.3):

#!/usr/bin/perl

print "> ";
while (<STDIN>) {
$output=eval $_;
warn $@ if $@;
print &quot;$output\n&quot;;
print &quot;> &quot;;
}

When you run it, it makes a kind of baby Perl interpreter giving a &quot;> &quot; prompt. The user can enter anything on the command line, then the 'eval' function evaluates the user's input as though it were a Perl expression. If it processes without any errors, the output is printed. But if there is an error, and error message is passed back to the calling routine in the '$@' variable. In this case, the message is just printed, but you could call a subroutine, or take some other action, instead. BTW, to quit, just type exit. (It evaluates 'exit' just like any other Perl command).

Another thing you might want to look into is signal handling, which is implemented somewhat differently from exception handling, using the %SIG associative array.

Hope this gets you going in the right direction,
Grant.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top