How about something like this:

$SIG{INT} = sub { die "Skip" }

LOOP: for $item (@BigList) {
     eval {
          print "Slowly processing $item..."; 
          # ...
     };

     if ($@ eq 'Skip') {
          # The user pressed ^C
          # If you need to clean up after it, do it here
          # Otherwise, this is a no-op
     }
     elsif ($@) {
          # Something else died
          # You may need some cleanup code here too.
     }
     else {
          # This item was processed w/o any interruptions.
     }
}

So, the concept here is you have wrapped your code in an eval block. If anything dies, it will stop the execution of the eval block at that spot. Outside of the eval block, you can inspect $@ to see if anything died (and what was thrown).

So far, the above is just common exception handling. What we do is setup SIGINT to die with a special message. The equivalent in Java might be throwing a Thread Interrupted Exception.

Please note that eval {} is not the same as eval "" and does not have the associated performance penalty.

Hope this helps.

Ted


In reply to Re: User interactivity in long loops by Anonymous Monk
in thread User interactivity in long loops by Solo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.