Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Different perl behavior between V5.10 and V5.14

by vsespb (Chaplain)
on Apr 01, 2013 at 12:54 UTC ( #1026480=note: print w/replies, xml ) Need Help??


in reply to Re^2: Different perl behavior between V5.10 and V5.14
in thread Different perl behavior between V5.10 and V5.14

Hm. Weird. I've modified code like this:
#!/usr/bin/perl -w use Time::HiRes; $SIG{"ALRM"}=\&sigAlarm; my $interval=1; my $uInterval=$interval*10**6; Time::HiRes::ualarm($uInterval, $uInterval); open TAIL, "tail -f xyz|" or die 'couldnt open xyz'; while (1) { while ($line=<TAIL>) { $X = $!; print $line; } if (eof(TAIL)) { print "EOF TAIL!\n"; } print "Fell through... $!\n"; } sub sigAlarm { print "alarm\n"; }
After first EOF found, it never works normal again. It just prints EOF TAIL + Fell through... in infinite loop. So, I think filehandle never recovered after Perl finds EOF.

Replies are listed 'Best First'.
Re^4: Different perl behavior between V5.10 and V5.14
by markseger (Beadle) on Apr 01, 2013 at 13:29 UTC
    I think the problem is once you've done the first $line=<TAIL> it's too late. Here's what I wrote that DOES work correctly and will not exit the loop, but as I said I'm not proud of it ;):

    #!/usr/bin/perl -w use Time::HiRes; $SIG{"ALRM"}=\&sigAlarm; my $interval=1; my $uInterval=$interval*10**6; Time::HiRes::ualarm($uInterval, $uInterval); my $logname='xyz'; open TAIL, "tail -f $logname|" or die "couldn't open $logname"; while (1) { while(1) { if (eof(TAIL)) { printf "EOF: %d\n", eof(TAIL); $line=<TAIL>; next; } last if !($line=<TAIL>); print $line; } print "Fell through...\n"; } sub sigAlarm { print "alarm\n"; }

    I also wonder if doing that eof() test at the beginning go throw me into an infinite loop under some conditions, but as so far this seems to be ok for now.

    -mark

      I think I finally have a clean solution. After much reading and asking around in the office someone helped me with this snippet, assuming TAIL is the file handle you're doing the tail -f on. The answer is to simply redo the failed operation which previous responses had suggested. So if anyone is interested this is what I've settled on and particularly like it because it's short and straightforward:

      while (1) { while(1) { my $line = <TAIL>; if (!defined($line)) { redo if $! eq 'Interrupted system call'; last; } print $line; } printf "Fell through...\n"; }

      -mark

        Yep, looks like a good workaround. Also I belive you can use
        $!{EINTR}
        instead of
        $! eq 'Interrupted system call'
        (as per '%!' in perlvar)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026480]
help
Chatterbox?
[robby_dobby]: marto: yeah, I'd eventually visit Glasgow some day - just to visit the home town of GHC :-)
[robby_dobby]: Corion: Oh, that's awesome! Your timing is perfect enough to see all hell break loose when you get back at work :P
[marto]: well, let me know in advance, I'll buy you a pint :)
[Corion]: robby_dobby: No, I'm returning in the second workweek of January. The main hectic parts are in the first days after the start of the new year
[robby_dobby]: marto: and, I'm not sure I'd be around here long enough for YAPC::EU
[marto]: Corion nice, what are you plans for Christmas?
[robby_dobby]: Corion: Nice, you just won't be around to deal with the mess - enjoy your vacation :-)
[robby_dobby]: marto: Sure, will do. Thanks!
[Corion]: marto: No great plans - I'll meet with my sister, my brother and my mother, but that's all :)
[Corion]: ... and also a visit to my godson+family of course :)

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-12-15 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?