Hi Alex,
What you're asking is possible, especially since you only asked for quick and
easy
and said nothing about elegant ;)
The situation you describe is known as a 'condition' in Lisp, which allows
you to define the catch and handle exceptional conditions in your program,
including *restarting* the offending piece of code (presumably after
changing program state to prevent looping). Most other languages use a
catch-throw construct, which is spelled die-eval in Perl.
Basically, you need to place an eval in Plort and die in Error with a
specific error string (or other unique value) that Plort can use to
distinguish your particular situation from some other error. As you can see,
both routines have to cooperate to make this work: one to throw the error and
the other to catch it.
Here's an example.
#!/usr/bin/env perl
use strict;
$\ = "\n";
#note the all important newline, which prevents perl from helpfully appending
#more text to our error string.
our $ERROR_CONDITION = "Error-Condition\n";
sub Plort {
print "in Plort, calling Foo()";
eval {
Foo();
};
if ($@){
if ($@ eq $ERROR_CONDITION) {
print "caught error condition. All is well";
}
else {
#some other error
die;
}
}
print "in Plort, back from Foo()";
}
sub Foo {
print "in Foo, calling Error()";
Error();
print "in Foo, back from Error, but you'll never see this line";
}
sub Error {
print "in Error";
die $ERROR_CONDITION;
}
__END__
Plort();
On Thu, May 31, 2007 at 02:11:44PM -0400, Alex Brelsfoard wrote:
> Hi All,
>
> I'm looking for a quick and easy way to have this situation happen:
>
> sub Plort {
> ...
> ...
> Foo();
> ...
> }
>
> sub Foo {
> ...
> ...
> Error();
> ...
> }
>
>
> I want it to happen that when Error() is called, when Error() finishes doing
> what it does it returns you to it's parent's parent (Plort() in this case).
> I don't want to have to specify anything.
> I want it to always return you to where Error()'s grandparent called it's
> parent.
>
> Any ideas of how to do this?
>
> Thanks.
> --Alex
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm