Gossamer Forum
Home : General : Perl Programming :

Perl exit(); function

Quote Reply
Perl exit(); function
Hi

I'm trying to get my head around the perl exit(); function. I can't find any documentation on this anywhere?

I need to exit a sub and exit the parent sub at the same time. Would exit -1 do this?

I basically have an error sub-routine which is called at various stages within other sub routines. I need to exit, and flush the current sub and then process the error sub. At the moment, it appends the output of my error sub onto and upto the point of termination in the current sub.

Anyone else have any experience with this?

- wil
Quote Reply
Re: [Wil] Perl exit(); function In reply to
Hi Wil,

Actually, exit 1 to indicate perl execution that this sub has error. exit -1 is c style.

For further info, use command below

perldoc -f exit

regards,


Cheers,

Dat

Programming and creating plugins and templates
Blog
Post deleted by Wil In reply to
Quote Reply
Re: [tandat] Perl exit(); function In reply to
Hi

So the two arguments exit(); can take is 0 and 1. Binary operators for 0 exit success and 1 for exit error.

Hmm. I guess what I really want is die. That would exit the sub immediately and flush parent sub?

- wil
Quote Reply
Re: [Wil] Perl exit(); function In reply to
Yes Wil, the die function is common used in perl script to exit in the sub. In the previous message, I just let you know that the how exit's status is

regards,



Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] Perl exit(); function In reply to
But how can I customise the die sub? I need to output my own error message to the browser, but I need the error sub to do what die does and actually kill the script. Is there an exit call that would act like die?

- wil
Quote Reply
Re: [Wil] Perl exit(); function In reply to
>>But how can I customise the die sub? <<

RTF perldoc

$SIG{__DIE__}

Last edited by:

PaulW: Nov 19, 2001, 5:26 AM
Quote Reply
Re: [PaulW] Perl exit(); function In reply to
... elaborate ?

- wil
Quote Reply
Re: [Wil] Perl exit(); function In reply to
You know what rtf perldoc means right? Tongue

Here's how you do it from your browser....

Type www.perldoc.com into your location bar and hit go
Type "die" into the search box in the middle of your screen when the page has loaded.
Hit the submit button
Click the top search result
Read


Quote Reply
Re: [PaulW] Perl exit(); function In reply to
Still doesn't help me in what I'm trying to do, Paul. I need to exit the current sub and the parent sub. die not exit does that unfortunately.

- wil
Quote Reply
Re: [Wil] Perl exit(); function In reply to
I'm not sure what you are talking about.

Using exit or die stops the execution
Quote Reply
Re: [PaulW] Perl exit(); function In reply to
OK. Using exit stops the execution but it still carries on until the parent sub is finished. Die just kills everything and exists, which is what I want. However, I want to be able to pass the error message I get with die onto a nice sub to display an error message to the browser, instead of being written to a log file.

I know about FataltoBrowser or whatever that module is, but I just want a nice little sub that will actually kill the script (the same as die) but product an error. Basicaly, show the error on screen rather than the error logs.

Cheers

- wil
Quote Reply
Re: [Wil] Perl exit(); function In reply to
Quote:
I want to be able to pass the error message I get with die onto a nice sub to display an error message to the browser, instead of being written to a log file.

I _told_ you. $SIG{__DIE__}. I guess I'm going to have to hold your hand and show you an example.....

local $SIG{__DIE__} = \&ugh;

die "You Suck";

sub ugh {

my $error = shift;
print "Content-type:text/html\n\n";
print $error;
}

Last edited by:

PaulW: Nov 19, 2001, 6:19 AM
Quote Reply
Re: [PaulW] Perl exit(); function In reply to
So, how would I incorporate what you're saying into..

open (FILE,"</home/fba/fbagroup.co.uk$file") || die("Could not open $file : $!");

??

Thanks! This is what I want :-)

- wil
Quote Reply
Re: [Wil] Perl exit(); function In reply to
Declare the sub you want to use for die() at the top....

sub main {

local $SIG{__DIE__} = \&error;

open (FILE,"</home/fba/fbagroup.co.uk$file") or die "Could not open $file : $!";


Quote Reply
Re: [PaulW] Perl exit(); function In reply to
Thanks, Paul! I never knew you could define what subs to use to handle program calls like that. I've now got the line:

Code:
local $SIG{__DIE__} = \&error_html;

at the top of my program which directs all die to my own error sub and exists the script correctly. Funny thing is, it prints it's own header or takes it from the sub it was in. Hmm.

Thanks


- wil