Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

how to use angle operator with timeout?

by Mark_Galeck (Novice)
on Feb 03, 2013 at 22:02 UTC ( #1016870=perlquestion: print w/ replies, xml ) Need Help??
Mark_Galeck has asked for the wisdom of the Perl Monks concerning the following question:

Hello, (I am not sure if this is a Perl question, or is more a general programming question. If the latter, then I am sorry to post here, please say so and I will begone. )

Every child in kindergarten knows what this does:

while (<>) { process $_ }
What I want, is to get into the loop not only when a new line arrives, but also, after 1 second of waiting for it, (in which case $_ should be empty). I am thinking along the lines of fork or something, but it's going to be somewhat ugly. Is there a nice "monk" way to do this? Thank you, Mark

Comment on how to use angle operator with timeout?
Download Code
Re: how to use angle operator with timeout?
by tobyink (Abbot) on Feb 03, 2013 at 23:35 UTC

    Try Prompt::Timeout or IO::Prompter.

    The basic technique just involves the alarm function...

    use 5.010; use strict; use warnings; use Try::Tiny; my $line; try { local $SIG{ALRM} = sub { die "ALARM\n" }; alarm 10; say "Please enter some text, but quick!"; $line = <>; chomp $line; alarm 0; } catch { my $e = shift; $e =~ /^ALARM/ ? warn("timeout\n") : die($e); }; say "Got: '$line'";
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Does that work on *nix? Cos it never times out on Windows.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        "Does that work on *nix? Cos it never times out on Windows."

        Works for me (on Linux).

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Thank you Monks very much, I appreciate your help! Yes this does work for me. Thank you!
Re: how to use angle operator with timeout?
by Mr. Muskrat (Abbot) on Feb 04, 2013 at 18:55 UTC

    Every child in kindergarten knows what this does:

    while (<>) { process $_ }

    Don't I wish!

Re: how to use angle operator with timeout?
by Khen1950fx (Canon) on Feb 05, 2013 at 07:23 UTC
    I couldn't get alarm to work for me in a consistent manner, so I tried Tie::Scalar::Timeout.
    #!/usr/bin/perl -l use strict; use warnings; use Tie::Scalar::Timeout; tie my $line, 'Tie::Scalar::Timeout', EXPIRES => '+1s', POLICY => undef; $line = system("whoami"); while (defined($_ = $line)) { foreach $line(my @lines) { print "$_" or die $@; } }

      couldn't get alarm ....

      What has that nonsense got to do with readline, ie the OPs question?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016870]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2015-07-05 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls