Break loop by trapping INT

T

Todd A. Jacobs

I'm trying to break a loop whenever CTRL-C is pressed, and find that
this doesn't work:

trap 'INT', proc {break}; count=0; while count < 10;
puts count += 1; sleep 1; end

I'm guessing it's because the loop is somehow out of scope, but I'm not
sure why.

Some minor syntax changes to trap makes it work, sort of. If I use:

trap('INT') {break}

instead, then I get a LocalJumpError. So, why does the syntax only work
one way, and what is the right way to trap the interrupt?
 
M

Morton Goldberg

I'm trying to break a loop whenever CTRL-C is pressed, and find that
this doesn't work:

trap 'INT', proc {break}; count=0; while count < 10;
puts count += 1; sleep 1; end

I'm guessing it's because the loop is somehow out of scope, but I'm
not
sure why.

Some minor syntax changes to trap makes it work, sort of. If I use:

trap('INT') {break}

instead, then I get a LocalJumpError. So, why does the syntax only
work
one way, and what is the right way to trap the interrupt?

When the trap is triggered, the trap's proc object receives a 'call'
message. This happens in the scope of top level, not in the scope of
the while block. To get the result you want, you need to invoke a non-
local jump mechanism. This can be done with catch and throw.

<code>
trap('INT') { puts "done"; throw :quit }

count = 0
catch :quit do
while count < 10;
puts count += 1
sleep 1
end
end
</code>

Regards, Morton
 
7

7stud --

Morton said:
When the trap is triggered, the trap's proc object receives a 'call'
message. This happens in the scope of top level, not in the scope of
the while block. To get the result you want, you need to invoke a non-
local jump mechanism. This can be done with catch and throw.

trap('INT') { puts "done"; throw :quit }

count = 0
catch :quit do
while count < 10;
puts count += 1
sleep 1
end
end

Yikes! Isn't that using catch/throw as a plain vanilla goto?
 
M

Morton Goldberg

Yikes! Isn't that using catch/throw as a plain vanilla goto?

Not quite. Catches have scope. Consider the following:

<code>
trap('INT') { puts "done"; throw :quit }

count = 0
catch :quit do
while count < 10;
catch :next do ; end
puts count += 1
sleep 1
end
end
puts "But I don't want to quit!"
throw :next
</code>

That doesn't work because catch :next is invisible to throw :next.
Throws only search upward in the stack frames, not downward. Now this
could be made to work with continuations. With continuations you can
do truly shocking things.

But even if were just like a goto, so what? Using trap already
injects non-local behavior into the script. A little catch and throw
hardly matters afters that. And it gets the job done. In this
particular case, because there is nothing to do after the while
block, the OP could use:

<code>
trap('INT') { puts "done"; exit }

count = 0
while count < 10;
puts count += 1
sleep 1
end
</code>

But that's not really very different, only less general.

Regards, Morton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top