[30224] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1467 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 22 17:12:44 2008

Date: Tue, 22 Apr 2008 14:12:36 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 22 Apr 2008     Volume: 11 Number: 1467

Today's topics:
        sending output to STDOUT or a file <vahid.moghaddasi@gmail.com>
    Re: sending output to STDOUT or a file xhoster@gmail.com
    Re: sending output to STDOUT or a file <someone@example.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 22 Apr 2008 11:26:28 -0700 (PDT)
From: Vahid <vahid.moghaddasi@gmail.com>
Subject: sending output to STDOUT or a file
Message-Id: <cef0bae1-ab84-4116-80cf-d7eb59f4bbeb@26g2000hsk.googlegroups.com>

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.
Thanks,


------------------------------

Date: 22 Apr 2008 19:03:25 GMT
From: xhoster@gmail.com
Subject: Re: sending output to STDOUT or a file
Message-Id: <20080422150327.437$SB@newsreader.com>

Vahid <vahid.moghaddasi@gmail.com> wrote:
> 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.
> Thanks,

Maybe I'm missing something, but I think this does it:

unless ( -t STDOUT) {
  my $today=`date +%Y%m%d`; chomp $today;
  open STDOUT, ">/tmp/output.$today.log" or die $!;
};

print "The foreground is $foreground\n";

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


------------------------------

Date: Tue, 22 Apr 2008 19:04:15 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: sending output to STDOUT or a file
Message-Id: <PEqPj.1279$PM5.794@edtnps92>

Vahid wrote:
> 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;

use POSIX 'strftime';

my $today = strftime '%Y%m%d', localtime;


> my $LOG="/tmp/output.$today.log";
> my $foreground="$LOG";

my $foreground = $LOG;

perldoc -q quoting


> 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.

perldoc -q "How can I use a filehandle indirectly"
perldoc -q "How do I make an array of filehandles"
perldoc -f open
perldoc perlopentut



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1467
***************************************


home help back first fref pref prev next nref lref last post