Path: utzoo!attcan!uunet!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: new menu.pl Message-ID: <8923@jpl-devvax.JPL.NASA.GOV> Date: 30 Jul 90 18:10:35 GMT References: <1990Jul29.195437.7608@uvaarpa.Virginia.EDU> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 80 In article <1990Jul29.195437.7608@uvaarpa.Virginia.EDU> marc@mit.edu writes: : Of particular interest are the following lines: : : sub sigint { : $foo = "@_[0..]"; # always generates a perl error at runtime. : } : : # ... : : # exception handling! : : $oldsigint = $main'SIG{'INT'}; : $main'SIG{'INT'} = "menu'sigint"; : : eval("&$fct(\$arg);"); : : $main'SIG{'INT'} = $oldsigint; : : I've basically implemented exception handling. Since eval returns at : any fatal error, the menu function can be aborted at any time by a : perl error. In this case, I install a signal handler which causes a : perl error. (Larry, is there any better way to generate a guaranteed : perl error?) It would be pretty trivial to implement throw- and : catch-style semantics given this technique: (Following untested) You don't need to get so fancy to generate a fatal error. Just use the die operator: sub sigint { die 'Here is a parameter that will be passed back in $@' . "\n"; } : With some clever code, you could probably implement real catch and : throw, with different kinds of throws and data. Seems fairly easy to me. Here's an implementation of typed exception handlers: sub catch { local($typelist, $code) = @_; local($retval); eval $code; if ($@ =~ /^(\w*):/) { $retval = $1; die $@ unless $typelist =~ /\b$retval\b/; # propagate exception $@ =~ s/^\w*://; } elsif ($@ ne '') { die $@; } $retval; } sub throw { local($type, $message) = @_; die "$type:$message\n"; } $except = &catch('INT MATH SYNTAX', '&calc'); if ($except eq 'MATH') {...} sub calc { ... &throw('MATH', "Can't divide by zero") if $denom == 0; ... } Or something like that--I haven't tested it. : I think this one can go under "functionality Larry never even dreamed : about." Don't teach your grandmother to suck eggs. :-) : Need another chapter in the book? :-) Short chapter. :-) Larry