Rhodium has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks
Why won't the following code work? Basically I want to reopen a file (cheesy man's tail) and look for errors and warnings. The problem is I kept getting this in the output
readline() on closed filehandle main::LOG at ./netlist.pl line 510.

which used this code
my $log = "$cell".".$$"."/si.log"; my $pwd = cwd(); my $netlist = "$pwd"."/"."$cell".".$$"."/"."$cell".".cdl"; my ($error, $done) = 0; print " Checking log files for success on $netlist\n"; while (( ! $error ) and ( ! -f $netlist)) { open ( LOG, $log ) || warn " Couldn't open log\n" if ($debug); while (<LOG> ){ print "$_"; if ( ($_ =~ m/error/i) or ($_ =~ m/warning/i) ){ print "$_" ; print "$_" if ($debug) ; if ($_ =~ m/error/i) { $error = 1; print "$_"; $icfb->hard_close(); } } close LOG; } sleep 2; }
Ok fine, I thought it was re-opening the thing so many times, it barfed. That's ok, just check the file everyonce in a while. So I started looking to see if fileno was defined. It NEVER get's defined. Can someone please point me in the right direction..
my $log = "$cell".".$$"."/si.log"; my $pwd = cwd(); my $netlist = "$pwd"."/"."$cell".".$$"."/"."$cell".".cdl"; my ($error, $done) = 0; print " Checking log files for success on $netlist\n"; while (( ! $error ) and ( ! -f $netlist)) { open ( LOG, $log ) || warn " Couldn't open log\n" if ($debug); if ( defined (fileno LOG )) { print "Defined \n"; while (<LOG> ){ print "$_"; if ( ($_ =~ m/error/i) or ($_ =~ m/warning/i) ){ print "$_" ; print "$_" if ($debug) ; if ($_ =~ m/error/i) { $error = 1; print "$_"; $icfb->hard_close(); } } } close LOG; } else { print "Not Defined\n"; } sleep 2; }
FWIW, I also tried to use File::Tail but I couldn't figure out how to do a conditional if no errors and no netlist keep going..

Rhodium

The seeker of perl wisdom.

Replies are listed 'Best First'.
Re: Filehandle open or not - WTF? :(
by Ovid (Cardinal) on Mar 11, 2003 at 22:42 UTC

    You're trying to do too much on one line:

    open ( LOG, $log ) || warn " Couldn't open log\n" if ($debug);

    Here's how it deparses:

    $ perl -MO=Deparse,-p -e 'open F, shift || warn $! if $debug' ($debug and open(F, (shift(@ARGV) || warn($!))));

    In other words, you only try to open the filehandle if $debug is true.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

      Hey Thanks a lot!!

      Would you mind telling me where you found that little nugget of gold called Deparse? I would love to know and is there more info on it?

      Thanks again

      Rhodium

      The <it>seeker</it> of perl wisdom.

        See perldoc perlmodlib to find out which modules are included with your installation of Perl and perldoc perllocal to see which modules have been installed in addition to the core ones.

        Makeshifts last the longest.

Re: Filehandle open or not - WTF? :(
by chromatic (Archbishop) on Mar 11, 2003 at 22:44 UTC

    This line is tripping you up:

    open ( LOG, $log ) || warn " Couldn't open log\n" if ($debug);

    It'll only open the file if the $debug flag is true. The if is much higher in precedence than ||. This will work instead:

    open( LOG, $log ) or do { warn " Couldn't open log: $!\n" if $debug; };
Re: Filehandle open or not - WTF? :(
by Nkuvu (Priest) on Mar 11, 2003 at 23:56 UTC

    Your question seems to have been answered by other notes, but I was curious about this: if ( ($_ =~ m/error/i) or ($_ =~ m/warning/i) ) -- wouldn't it be clearer to say something like if (/error/i or /warning/i) ? If you aren't going to name the variable, you might as well use more concise code.

    You're also adding quotes where you don't need them, on each call to print "$_". And even more, print defaults to printing $_ so you could change print "$_" to print

    Personally I use more variables, but I like maximum verbosity. So I'd have things like while ($line = <LOG>) and then print $line but that's just my personal style...

    Oh, and the main reason I ask about all of this is that I've seen a lot of other monks make these suggestions -- so I'm partially curious as to why they weren't suggesting these things here... OK, I'll be quiet now. :)

          Well, yes, things could be reduced more. Notice that I said more concise code, not just concise code.

          I also read (in the Camel, maybe?) that the in-match alternation (such as /error|warning/) isn't as quick or efficient as two matches or'ed together. So I tend not to do that. :)

    Re: Filehandle open or not - WTF? :(
    by data64 (Chaplain) on Mar 11, 2003 at 22:22 UTC