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:
Any way to exit all subroutines at once?

 



alexb
Novice

Dec 26, 2010, 1:18 AM

Post #1 of 7 (1669 views)
Any way to exit all subroutines at once? Can't Post

Hello,

I have searched all the web for an answer and couldn't find one.

I am trying to write a function like : &message_die("<some message>") that exists all the sub routines (even embedded ones) wherever I call this sub and I want it to return to the main.

is there any way to do so ? because die exits the program, and I don't want it to exit, I just want it to exit all the subroutines that were called till now.

* I know that I can do a return statement in all the subroutines, but I want this function to be standalone, so it wouldn't be dependent of how I write the subroutines.


shawnhcorey
Enthusiast


Dec 26, 2010, 6:26 AM

Post #2 of 7 (1659 views)
Re: [alexb] Any way to exit all subroutines at once? [In reply to] Can't Post

Use `eval`:

Code
# main 
eval { my_sub( @args ) };
if( $@ ){
print STDERR "error in my_sub: $@\n";
}


Then use `die` in my_sub (or the subs it calls) to jump back to main after the eval.

PS: This is called exception processing. You can search for `perl exceptions` for more information.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

(This post was edited by shawnhcorey on Dec 26, 2010, 6:28 AM)


rovf
Veteran

Dec 27, 2010, 6:57 AM

Post #3 of 7 (1632 views)
Re: [alexb] Any way to exit all subroutines at once? [In reply to] Can't Post

shawnhcorey's solution will work often, but is strictly speaking not a general solution to your problem, because it will fail if an intermediary sub catches the exception. This could be the case if, for example, you are using a 3rd party framework, where you write the main program and the call back functions, but where the framework itself sits in "middle ground".

I have to impression that you are using something like setjmp/longjmp in C. This is (fortunately) not possible in Perl.

If however you have control over your whole application, an exception policy like shawnhcorey described will be the best solution.


alexb
Novice

Dec 29, 2010, 12:19 AM

Post #4 of 7 (1621 views)
Re: [rovf] Any way to exit all subroutines at once? [In reply to] Can't Post

This is a great solution, thanks.

but, I encounter another problem.
I wanted to be able to give a message of my own and not like die does.
is there a way to just jump back to the main without printing anything (like die does to STDERR).

I would really appreciate you help.


rovf
Veteran

Dec 29, 2010, 3:30 AM

Post #5 of 7 (1620 views)
Re: [alexb] Any way to exit all subroutines at once? [In reply to] Can't Post

'die' does NOT print to stderr. 'die' just throws an exception, and only if the exception is not handled, the message sent by 'die' is printed to stderr. Technically speaking: The default exception handler (not 'die') prints the message and aborts the program.

For example:

perl -lwe "eval {die 'This will not be printed'}; "


alexb
Novice

Dec 29, 2010, 4:27 AM

Post #6 of 7 (1616 views)
Re: [rovf] Any way to exit all subroutines at once? [In reply to] Can't Post

ok,

so if the default handle prints to stderr, how can I write that line within my program and make it to NOT print to stderr?

I write a line like : eval{&do_something()}

and

sub do_something
{
....
....
die;
}

This program will print to stderr.


rovf
Veteran

Dec 30, 2010, 2:20 AM

Post #7 of 7 (1607 views)
Re: [alexb] Any way to exit all subroutines at once? [In reply to] Can't Post


Quote
This program will print to stderr.


If it does, please delete your Perl installation, because it means it is broken.

Here it how it works in a working Perl installation:


Code
$ perl -lwe 'use strict; eval{&do_something()}; sub do_something { die; }' 
$


I can't see anything printed here.


(This post was edited by rovf on Dec 30, 2010, 2:20 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