home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5677 < prev    next >
Encoding:
Text File  |  1992-09-02  |  2.1 KB  |  58 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: How do I make traps in perl?
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Sep2.141332.14728@news.eng.convex.com>
  8. Date: Wed, 2 Sep 1992 14:13:32 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <1992Aug26.135146.1759@linda.lidac.liu.se> <1992Aug28.190101.9961@netlabs.com> <1992Sep2.122722.22017@unipalm.co.uk>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 40
  17.  
  18. From the keyboard of ian@unipalm.co.uk (Ian Phillipps):
  19. :lwall@netlabs.com (Larry Wall) writes:
  20. :
  21. :>    sub CATCHINT {
  22. :>    print "OUCH! Don't DO that!!!\n";
  23. :>    }
  24. :
  25. :I've wanted the equivalent of a longjmp to allow a sig handler to yank
  26. :me out of nest of routines etc, and plonk me down somewhere standard
  27. :(e.g. a main STDIN-reading loop). The "goto" spec is so discouraging
  28. :that I've not even tried it, and nothing else seems to fit.
  29.  
  30. Have you not tried eval?
  31.  
  32. 27) How can I do an atexit() or setjmp()/longjmp() in Perl?
  33.  
  34.     Perl's exception-handling mechanism is its eval operator.  You 
  35.     can use eval as setjmp, and die as longjmp.  Here's an example
  36.     of Larry's for timed-out input, which in C is often implemented
  37.     using setjmp and longjmp:
  38.  
  39.           $SIG{'ALRM'} = 'TIMEOUT';
  40.           sub TIMEOUT { die "restart input\n"; }
  41.  
  42.           do {
  43.               eval '&realcode';
  44.           } while $@ =~ /^restart input/;
  45.  
  46.           sub realcode {
  47.               alarm 15;
  48.               $ans = <STDIN>;
  49.           }
  50.  
  51.  
  52. --tom
  53. -- 
  54.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  55.     > This made me wonder, suddenly: can telnet be written in perl?
  56.     Of course it can be written in Perl.  Now if you'd said nroff, 
  57.     that would be more challenging...   --Larry Wall
  58.