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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ending script execution

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
Hello

Is there any way to stop a script from completing in mid loop? I am using a linux server with perl 5.

Thanks Robert Carpenter
questions? comments? thanks? email me!
linkemapx@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
Sure,
depending on your situation, you can end the loop based on some condition from inside the loop or your can do a little Inter-Process Communication and send the process a 'kill'. Without more detail in the question, I'm not sure how to elaborate. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
first of all, thanks for the reply!

Here is my situation....I have a script that has several different tests that make the actual purpose of the script "do-able" if any of these tests fail, I need to be able to print out an error message and kill the script.

Kind of like javascript's return false statment.

I have seen the loop kill statment, but I am looking for a script kill statment. I guess what I meant by the "mid loop" part is that the script might be able to tell it won't be able to execute in the middle of a test loop. Robert Carpenter
questions? comments? thanks? email me!
linkemapx@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
Use the [tt]exit[/tt] function. It allows you to exit the script. //Daniel
 
There are several techniques for accomplishing your request. Here are a couple.

eval {..your perl code...};
if ($@){..if code fails do something..}

if (condition){..do something..}

For variable definfitions

$var ||="set variable to this if not already defined";

someperlcommand || die "some meaningful info about the failure";

Give us a code snippet so we can be more helpful
 
daniel! thanks! that hits the spot exactly!

but Haunter...I think that the die command would do the same thing except I couldn't get it to work...

I tried it like this:
open (SOMEFILE,"nonexistantfile.nxf") || die "bad file";

but all I get is a internal server error....whats up? Robert Carpenter
questions? comments? thanks? email me!
linkemapx@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
You must print/send a compliant http header before any text output. So, make sure you have something like,
Code:
print "Content-type: text/html\n\n";
before any text can make it out to the browser.

A good way to catch your errors in a piece of CGI is to use the CGI::Carp 'fatalsToBrowser' trick....
Code:
#!/usr/local/bin/perl
use CGI;
use CGI::Carp 'fatalsToBrowser';

open (SOMEFILE,"nonexistantfile.nxf") || die "bad file";

That will wrap the output of the die in just enough http stuff to make the web server happy and let the error message make it to the browser. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top