|
Posted by comp.llang.perl.moderated on April 23, 2008, 12:45 am
Please log in for more thread options
> Hi,
> I have a program that generates some output but I need it to dump them
> to a file if ran in a background and print on screen if ran in
> foreground. This is a sample program that need some help with:
> #!/bin/perl
> #
> use warnings;
> use strict;
> #
> my $today=`date +%Y%m%d`; chomp $today;
> my $LOG="/tmp/output.$today.log";
> my $foreground="$LOG";
> if ( -t STDOUT) {
> $foreground=STDOUT;}
>
> open (LOGfh, "> $foreground") or die "ERROR: $!";
>
> print LOGfh "The foreground is $foreground\n";
> close LOGfh;
>
> Of course this will give me error for using STDOUT in open.
Another possibility:
if ( -t STDOUT ) {
print ...
} else {
open ( local *STDOUT, '>', $LOG ) or die $!;
print STDOUT ...
}
--
Charles DeRykus
|