On Nov 20, 2:15 pm, [EMAIL PROTECTED] (Irfan Sayed) wrote:
> Hi All,
>
> Can somebody please help me for this block of code.
>
> if ("$1" ne "-log"){
>
> `$0 -log "$@" 2>&1 | tee the_log_file.$$.log`;
>
> exit 0;
>
> }
>
> I know this code redirects the output of entire perl script to file but
> it is not executing as expected.

That statement doesn't make sense.

> Can somebody please explain / help

Could you break down which bits of the code you are having trouble
understanding?

There are many many things wrong with this code but the most obvious
reason why it doesn't do what (I'm guessing) the author intended is
that $1 should read $ARGV[0] and $@ should read @ARGV..

The code will probably break if there's more than one  argument passed
to the original script or if that argument contains any shell meta-
characters.

See FAQ: What's wrong with using backticks in a void context?

Fixing some of the obvious errors you'd get.

exec("$^X $0 -log ". join( ' ', map { quotemeta } @ARGV ). " 2>&1 |
tee the_log_file.$$.log") unless $ARGV[0] eq "-log";

But this really isn't that good - better to do the redirection in Perl
rather than calling out to shell and back in.

open STDOUT, '|-',  'tee', "the_log_file.$$.log" or die $!;
open STDERR, '>&', *STDOUT or die $!;

You could even use IO::Tee and get rid of the need for an external tee
program altogether if you are not worried about capturing output from
child processes.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to