Hello Perl experts. Before I begin just want to point out that I am new to programming so keep that in mind before killing me :)

I am trying to figure out why the following code that I wrote is not working correctly. I am going through some examples in the book which don't seem to cover SIGNAL catching very well so any tips/help would be helpful.

With that being said not sure why what I am doing wrong. From what I can see the script recognizes the CTRL-C key combo but then skips right over test1 and test2 subroutines depending on when the CTRL-C is detected. My question is how/what is the best way with the code below to have it catch the ctrl-c key combo and have it wait until all processing has been completed then once it's done with all the code then exit.

So to recap trying to figure out how to catch ctrl-c no matter where the script is at once it sees that key combo then wait until all the code has run then exit. Thanks for all the help in advanced.
#!/usr/bin/perl use strict; use warnings; use Fcntl qw(:flock); my $testFile1 = "/tmp/test.txt"; my $lck = "/tmp/l.lck"; my $wantToQuit = 0; while (1) { while ( !$wantToQuit ) { $SIG{INT} = 'wait'; my $pid = fork(); if ( !defined( $pid ) ) { die("can't fork sub proc\n"); } if ( $pid == 0 ) { if ( ! -e $lck ) { print "\n\ntestFile1 does not exist, f +ile READY for reading\n"; open(LCK, ">$lck") || die "Could not o +pen $lck $!\n"; flock(LCK, LOCK_EX); open(FH, "+<$testFile1") || die "Could + not open $testFile1 $!\n"; close(FH); close(LCK); print "$testFile1 file CREATED\n"; &test1; &test2; } else { print "$testFile1 exits, must wait til + it's gone\n"; print "Sleeping for 10 seconds, then c +hecking for lock file\n"; countDown(10); } $SIG{INT} = 'DEFAULT'; } waitpid($pid,0); } exit if $wantToQuit; } sub wait { print "\n\n\n===============WAIT SUB=================\n"; print "CTRL-C signal was been detected please hold while we fi +nish running the script\n"; $wantToQuit = 1; print "\n\n\n===============END WAIT SUB=================\n"; } sub test1 { print "\n\n\n===============TEST1 SUB=================\n"; print "Sleeping for 3 seconds In sub test1\n"; countDown(3); print "===============END TEST1 SUB=================\n"; } sub test2 { print "\n\n\n===============TEST2 SUB=================\n"; print "Sleeping for 3 secs In sub Test2\n"; sleep 3; print "Not done yet gotta sleep for another 15 seconds then we + can quit\n"; countDown(15); print "DONE WITH ALL CODE PROGRAM SHOULD NOW EXIT\n"; print "===============END TEST2 SUB=================\n"; exit 0; } sub countDown { my $delay = shift; my $count = 0; while ( $count < $delay ) { print "$count\n"; sleep 1; $count++; } }

In reply to Call Intercepts by perlnewbie9292

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.