perlquestion
markseger
I have no idea if this is perl or ubuntu, as this works find on an Ubuntu/natty distro running perl 5.10 and does not do what I expect on Ubuntu/precise running perl 5.14.
<p>
The simple problem statement is I want to tail a file and count various types of entries, writing intermediate counters to a file every second. My solution, which has been running just fine for over a year is to simply set a timer with ularm to wake me every second at which time I write my counters and continue.
<p>
To reproduce the behavior, first you create the file xyz and then run the script which I've posted after the results. Then in a different window run the command: "echo test >> xyz" a couple of times.
<p>
On natty you see:
<p>
<code>
alarm
alarm
test
alarm
alarm
test
</code>
<p>
In other words it stays inside the loop processing the tail command. But the identical test on precise produces the following, noting which each new line written to the file it breaks out of the inner loop:
<p>
<code>
alarm
alarm
alarm
test
Fell through...
alarm
alarm
test
Fell through...
alarm
alarm
</code>
<p>
I've been trying a variety of things to get this to do what I want and am out of ideas. Here's the code:
<p>
<code>
#!/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>)
{
print $line;
}
print "Fell through...\n";
}
sub sigAlarm
{
print "alarm\n";
}
</code>
<p>
thoughts?
<p>
<b>Update...</b>
<br>
Now here's somethign really odd I also just discovered. If I run my second 'echo test' before the second consecutive alarm fires, it doesn't fall through the loop! Look at this, noting I waited a couple of second before doing the fourth echo command:
<p>
<code>
alarm
alarm
test
Fell through...
alarm
test <<< stayed in loop
alarm
test <<< stayed in loop
alarm
alarm
alarm
test
Fell through...
alarm
</code>
<p>
-mark