Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exiting script 3

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a part of my script where I want it to quit only after I type the '<Ctrl> c' three times instead of just once. In this part of the script if I type '<Ctrl> c' once it exits the scripts but I want it to exit after three times of '<Ctrl> c'.

How would I make a global variable to store the number of times '<Ctrl> c' is used in this part of my script??


Here is my input part:


while (@a = <STDIN>)
{
print @a;
}
 
Perl does not receive the ctrl-c from the input stream and stop when it receives it. Your terminal intercepts crtl-c and then sends an interrupt signal to the process. So what you have to do is install a signal handler.

At the most basic, for your purposes, add the following lines to your code:

$SIG{INT} = \&interrupt_handler;

sub interrupt_handler
{
$SIG{INT} = \&interrupt_handler;

#do something interesting here
}


Inside your handler, you could increment a global variable unless it had reached a given value, in which case it would exit.
 
Here's one way to do it.
Code:
$SIG{INT} = count_int;
$NumInt = 0;
while (@a = <STDIN>)
{
    print @a;
}

sub count_int () {
    $NumInt++;
    exit if ($NumInt >= 3);
}
 
# &quot;Perl does not receive the ctrl-c from the input stream # and stop when it receives it&quot;
# i think sleipnir214 is right sorry ...
# here a example of stoping only after 3 &quot;quit&quot;

my $byebye = 0;
while ($byebye < 3) {
$_ = <STDIN>;
if (/^quit/) {
$byebye++;
print &quot;exit commands pressed $byebye time&quot;;
if ($byebye > 1) {print &quot;s&quot;;}
print &quot;.\n&quot;
}
}
print &quot;Done&quot;;
exit; ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Your right Perl doesn't stop on ctrl-c.
But it does stop on ctrl-z (at least in windoze).

Question? -
Why would you want to stop on ctrl-c?
Isn't that a little like doing the reboot sequence (ctrl-alt-del) to login?
Why not just watch for something like an &quot;F&quot; key like F12 (nobody ever uses F12?

O.K that was three questions!

tgus
 
That's because Ctrl-z causes your terminal to send a different signal than Ctrl-c. Nobody mentioned wantint to intercept Ctrl-z.

Ctrl-z sends a KILL signal. You want to catch that one, set $SIG{KILL}.


You might want to interrupt Ctrl-c to stop a user from terminating the program. I guess catching it three times is a failsafe.
 
>You might want to interrupt Ctrl-c to stop a user from terminating the
>program. I guess catching it three times is a failsafe.

I guess that makes sense. But it must only work in the *nix world.
I can't get it to work in win.

 
Sorry, I was wrong. Crtl-Z sends TSTP, which you would catch with

$SIG{TSTP} = \&tstp_handler;

like sleipnir214 posted above for INT.

jaa
 
Is it possible to read/and write LPT and serial ports by using Perl? if it is, may i have some example please? I try to build up WinNT domain and i want to make users and etc. by using Nokia mobile phone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top