From: Rudolf Polzer Date: 2003-09-10T06:26:43+09:00 Subject: Re: Emulate perls local Scripsit illa aut ille �Karsten Meier� : > Thank you for the feedback. > (Are only germans interested in this kind of stuff?) No idea. > I think most usages in perl just don't make sense in ruby, > normally it is better to use a local variable, you also have no trouble > to put file handles in a variable in ruby. Neither do you in current Perl versions: open my $fh, " The perl cookbook mentions also it is possible to use it for temporary > signal handlers, like this (perl code) > local $SIG(INT) = 'IGNORE'; > I never needed to install a signal handler at all, so I'm not sure > if this something intersting, and if it apply to ruby. In Ruby one would just use exceptions for this purpose. Hm... nearly, since there is no "ON ERROR RESUME NEXT". Seems like it's impossible to use a block for both the signal handler and the code which is to be executed. Hm... wouldn't something like this (but also supporting Proc#yield and Proc#[] which is trivial to add) be a nice extension? class Proc alias :origCall :call def trap(signal, &handler) (@traps ||= []) << [signal, handler] self end def do_trapping(&block) savedtraps = [] (@traps || []).each() do |t| savedtraps << [t[0], Kernel.trap(t[0], t[1])] end begin block.origCall() # do NOT call yield() here (endless recursion)! ensure savedtraps.each() do |t| Kernel.trap(t[0], t[1]) end end end def call(*args) do_trapping do origCall(*args) end end end trap('INT') do puts "Aiiieee!!!1" end proc do puts "AAA" sleep 5 puts "BBB" end.trap('INT') do puts "SIGINT caught" end.trap('QUIT') do puts "SIGQUIT caught" end.call() # <-- this MUST NOT be left out sleep 5