Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How to set timer in perl

by Rahul Gupta (Sexton)
on Jun 13, 2013 at 12:26 UTC ( #1038733=perlquestion: print w/ replies, xml ) Need Help??
Rahul Gupta has asked for the wisdom of the Perl Monks concerning the following question:

Hi ,

I have a file with huge data, i read the same file and stored the content in an array

my @file_content = read(file); foreach(@file_content){ if ($_ =~ m/somestring/){ } }

i have to check the string for some specific time(i.e 5 secs..). if i don't get the string in defined time, then it should get terminate.

i used alarm but it didn't work for me.

can any one help me

thanks in advance

Comment on How to set timer in perl
Select or Download Code
Re: How to set timer in perl
by hbm (Hermit) on Jun 13, 2013 at 13:09 UTC

    I've only needed alarm once, and this worked:

    eval { local $SIG{__DIE__} = 'DEFAULT'; local $SIG{ALRM} = sub { die "timeout" }; alarm 2700; # do things alarm 0; }; if ($@) { if ($@ =~ "timeout" ) { ... } else { print "$@\n"; } alarm 0; }
      The documented example for alarm has a "\n" at the end of the die string, but also checks $@ with 'eq', not a regex like you are. Just thought I would point that out.
Re: How to set timer in perl
by Bloodnok (Vicar) on Jun 13, 2013 at 13:33 UTC
    In order to guide/help you without repeating steps you've already tried, whomsoever does reply would prefer to have a look see at what you've tried, by way of (skeletal) implementation, thus far i.e. in what way(s) did you try use alarm that didn't work.

    A user level that continues to overstate my experience :-))
      hi,

      Here is the code which i have tried.

      #!/usr/bin/perl -w use strict; my $FH; local $SIG{ALRM} = sub { die "Timeout no data received\n"; }; open ($FH, '<', "file1") or die "cant open file to read file$!.\n"; alarm 5; while (<$FH>){ if ($_ =~ m/any string/i){ print "$_\n"; alarm 0; } } close ($FH);
        I'd venture to say that you're hitting EOF and exiting the loop before the five seconds have passed. Perhaps you want File::Tail?
Re: How to set timer in perl
by DrHyde (Prior) on Jun 14, 2013 at 10:58 UTC
    No, we can't help you, not without you showing us exactly what you tried. What, precisely, did you do with alarm, what, exactly, did you expect to happen, and how did that differ from what happened?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038733]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-12-27 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls