LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2010, 05:10 PM   #1
tprayush
LQ Newbie
 
Registered: Apr 2010
Posts: 3

Rep: Reputation: 0
how to continue in the loop after traping signal in perl


hi all....

plz tel me how can i solve this....here's the situation (this is just a sample!!)..
Code:
$ cat sigtrap
#!/usr/bin/perl
$SIG{'INT'} = 'ABORT';
    sub ABORT {
    print "\nStop the loop?? (y/n) : ";
        chop($ch=<STDIN>);
       
 if ($ch =~ /[yY]/)
    {     
    
         
    exit(1);

     }
  else
   {
     redo;
   }
    }
$no=0;
while (1)
 {
   print "$no\n";
   sleep 2;
   $no++
 }
Code:
$ ./sigtrap
0
1
2
3
^C
Stop the loop?? (y/n) : n
Can't "redo" outside a loop block at ./sigtrap line 16, <STDIN> line 1.
by using 'trap' in bash...we can continue in loop....if we write 'trap' in the loop...

but how can i solve this in perl!! plz help!!


thanx.
 
Old 05-02-2010, 05:22 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tprayush View Post
hi all....

plz tel me how can i solve this....here's the situation (this is just a sample!!)..
Code:
$ cat sigtrap
#!/usr/bin/perl
$SIG{'INT'} = 'ABORT';
    sub ABORT {
    print "\nStop the loop?? (y/n) : ";
        chop($ch=<STDIN>);
       
 if ($ch =~ /[yY]/)
    {     
    
         
    exit(1);

     }
  else
   {
     redo;
   }
    }
$no=0;
while (1)
 {
   print "$no\n";
   sleep 2;
   $no++
 }
Code:
$ ./sigtrap
0
1
2
3
^C
Stop the loop?? (y/n) : n
Can't "redo" outside a loop block at ./sigtrap line 16, <STDIN> line 1.
by using 'trap' in bash...we can continue in loop....if we write 'trap' in the loop...

but how can i solve this in perl!! plz help!!


thanx.
You could use better indentation - the code looks disorderly.

Reading from STDIN in signal handler is a very bad idea - the handler should be as quick as possible.

Use signal handler to set a flag and analyze the flag value inside your loop. Depending on the flag value do or do not read from STDIN and continue or not depending on the value read from STDIN.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl - Press Any Key To Continue nobody123 Linux - Newbie 4 04-06-2009 06:52 PM
perl loop jazman Programming 3 07-05-2006 10:31 AM
kernel message traping ekdya Debian 2 10-06-2005 06:35 PM
Need help with perl loop! morbid_ru Programming 1 02-24-2004 01:14 PM
Need help with perl loop! morbid_ru Programming 2 02-17-2004 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration