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

Hello Monks I am running an application from script and then as the application progresses it keeps on displaying various things on the command prompt which I am trying to capture to a log file. For experimentation I used a batch file and opened it using pipe (I plan to do the same with exe). the code looks like
use Log::Log4perl qw(); Log::Log4perl->init("process.conf"); my $log = Log::Log4perl->get_logger(); open APP, "Exception.bat |" or die "File not found\n"; while(<APP>){ print; #log->info; } close APP; print "Out of batch file\n";
Now when I capture everything through print then it works fine...but as soon as I try to use logging it fails. Please advise. Thanks

Replies are listed 'Best First'.
Re: Running an application and capturing output
by mifflin (Curate) on Jun 02, 2005 at 02:21 UTC
    Like thunderkgnat says, you're probably missing the $ infront of log->info;
    deparsing that code
    C:\Documents and Settings\erickn\Desktop\invexp>perl -MO=Deparse -e"lo +g->info" log($_)->info; -e syntax OK
    shows that you are executing log, a builtin function of perl. Since $_ is not set it tries to do a log of 0 which is not defined.
    Try putting use strict; and use warnings; at the top of your script to help track down bugs like these.

      Why would $_ not be set in a while loop? What would either strict or warnings do in a case like this?

        Strict because, well, it's just a good idea.
        Warnings because, if he had it on, would probably have seen something like
        C:\Documents and Settings\erickn\Desktop\invexp>e.pl line 1 Can't take log of 0 at C:\Documents and Settings\erickn\Desktop\invexp +\e.pl line 9, <DATA> line 1.
        Note that I am guessing what his batch file would return to stdout. My test code to produce the above output...
        #use warnings; use strict; use Log::Log4perl qw(); Log::Log4perl::init("process.conf"); my $log = Log::Log4perl->get_logger(); #open APP, "Exception.bat |" or die "File not found\n"; while(<DATA>){ print; log->info; } print "Out of batch file\n"; __DATA__ line 1 line 2
        update: I suppose that's not alot of extra info but it is more that he had and might have made it easier for him to figure it out on his own. *shrugs*
Re: Running an application and capturing output
by moot (Chaplain) on Jun 02, 2005 at 01:33 UTC
    You don't specify what the failure is, but it looks from your commented out line that you are not actually passing any data to $log->info. Try:
    # rest of code $log->info($_); # rest of code
Re: Running an application and capturing output
by thundergnat (Deacon) on Jun 02, 2005 at 01:35 UTC

    It doesn't appear that $log->info will act on $_ by default. Try explicitly passing $_ to it.

    while(<APP>){ print; $log->info($_); }
      I tried that also...but that was also giving the same error. The error looks like
      C:\Neshat\Perl-Scripts>perl captureexeoutput.pl Can't take log of 0 at captureexeoutput.pl line 8, <APP> line 1. FINDSTR: Write error The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent pipe. ^C^C C:\Neshat\Perl-Scripts>

        Do you have the $ sigil on your variable? It isn't in your example. log is a function. $log is the name of your Logger.