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


I have a PERL script that does some search operation and i keep writing stuff to a log file. I have used this pattern in a LOT of PERL scripts but unfortunately, this time it just won't work!!!! it's a very simple PERL statmenet - why won't it work?!!!

#!/usr/bin/perl use strict; # use warnings; use XML::LibXML; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use File::Copy; use File::Glob ':glob'; use Data::Dumper; my $paramcount = scalar @ARGV; if ( 0 == $paramcount || $paramcount < 3) { print "Usage: perl hunt.pl <excel sheet> <search folder> <output f +older>\n\n"; exit; } my $i_sheet = $ARGV[0]; my $search_folder = $ARGV[1]; my $out_folder = $ARGV[2]; # sanity check and preparation.... unless ( -d $search_folder ) { print "Input folder does not exist \n"; exit; } unless ( -f $i_sheet ) { print "Input excel does not exist \n"; exit; } unless ( -d $out_folder ) { print "Out folder does not exist \n"; exit; } # prepare log file... open( LOG, ">> huntlog.log") || die("Could not open log file!"); print LOG "Log start... \n";


The last line in this code snippet and any line that starts with "print LOG ..." would not work - the log file gets created but it remains empty forever!!

Any idea why?


Replies are listed 'Best First'.
Re: writing to a log file....
by toolic (Bishop) on Nov 04, 2009 at 22:27 UTC
    When I run your code, it works for me. The 1st time I ran it, it created a file named 'huntlog.log' and it contained the line 'Log start...'. Every subsequent time I ran it, it appended the same line to the file, as desired.

    Try checking if print fails:

    print LOG "Log start... \n" or die "wtf: $!";

    See also suffering from buffering

Re: writing to a log file....
by affc (Scribe) on Nov 04, 2009 at 22:34 UTC
    Check also if you get an error with $! when opening LOG:
    open( LOG, ">> huntlog.log") || die("Could not open log file: $!");
Re: writing to a log file....
by Argel (Prior) on Nov 05, 2009 at 00:14 UTC
    Sounds more like a permissions issue, or trying to write to a file-system mounted as read-only -- e.g. are you writing to a network share?

    Elda Taluta; Sarks Sark; Ark Arks

Re: writing to a log file....
by Bloodnok (Vicar) on Nov 04, 2009 at 23:17 UTC
    ... or, further to the previous pertinent replies, in the interests of far more readable code, use (pun, once again, intended:-) autodie

    A user level that continues to overstate my experience :-))
Re: writing to a log file....
by kikuchiyo (Hermit) on Nov 05, 2009 at 10:52 UTC
    Or possibly this is a buffering issue.

    The data gets written to the actual file only if a certain amount of it is accumulated in the buffer. (Or something to that effect.)

    Try select LOG; $|++; before the first </code>print LOG</code> statement or simply $|++; at the beginning of your file.