Manfred Lotz wrote:
> Hi there,
> I have a script where I log stuff to a file and the same time displays
> it to stdout using Log4perl.
>
> Here is a minimal example where I log a command which fails.
>
> <--------------------snip------------------------->
> #! /usr/bin/perl
>
> use strict;
> use warnings;
>
> #use autodie;
> use Log::Log4perl qw(:easy);
>
> Log::Log4perl->easy_init(
>       { level => $DEBUG, file => "> test.log", },
>       { level => $DEBUG, file => 'STDOUT', }
> );
>
> my $cmd = 'uname';
> my $parms = '-f'; # invalid parm
>
> INFO( "Issuing [$cmd $parms]" );
> open my $fh, '-|', "$cmd $parms 2>&1" or die "open: $!";
> INFO( "$_" ) while <$fh>;
> close $fh;
>
> <--------------------snap------------------------->
>
> This works great. Output is like this:
> 2013/05/01 16:16:40 Issuing [uname -f]
> 2013/05/01 16:16:40 uname: invalid option -- 'f'
> 2013/05/01 16:16:40 Try 'uname --help' for more information.
>
>
> However, if I add autodie then I get:
>
> 2013/05/01 16:16:33 Issuing [uname -f]
> 2013/05/01 16:16:33 uname: invalid option -- 'f'
> 2013/05/01 16:16:33 Try 'uname --help' for more information.
> Can't close(GLOB(0x2554c80)) filehandle: '' at ./test02.pl line 20
>
>
> close does not fail if the command is ok, e.g. 'uname -a'. Can anybody
> explain to me why close fails in the example above?
>
>
>
> --
> Thanks,
> Manfred

If you test the return code of the close call, you'll see that it fails in
both cases.

I'm not sure why it's failing, but personally I'd use IPC::Open3 instead
of opening the pipe as you are currently doing.  That way, you could
capture the stdout and stderr separately and log the stderr as WARN or
ERROR.

Ron Bergin


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to