[30237] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1480 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 25 16:56:37 2008

Date: Fri, 25 Apr 2008 13:56:30 -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           Fri, 25 Apr 2008     Volume: 11 Number: 1480

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

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

Date: Tue, 22 Apr 2008 21:45:01 -0700 (PDT)
From: "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: sending output to STDOUT or a file
Message-Id: <c9f4ef06-6e6d-4ca2-b409-cf8e48834b40@59g2000hsb.googlegroups.com>

On Apr 22, 11:26 am, Vahid <vahid.moghadd...@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.

Another possibility:

if ( -t STDOUT ) {
   print ...
} else {
   open ( local *STDOUT, '>', $LOG ) or die $!;
   print STDOUT ...
}


--
Charles DeRykus


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

Date: Wed, 23 Apr 2008 11:34:52 -0700 (PDT)
From: Vahid Moghaddasi <moghaddasi@gmail.com>
Subject: Re: sending output to STDOUT or a file
Message-Id: <6462df1b-79e5-4467-8827-78363f421291@59g2000hsb.googlegroups.com>

On Apr 22, 3:03 pm, xhos...@gmail.com wrote:
> Vahid <vahid.moghadd...@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
>
Yes that does the trick, thank you.
I don't understand how does the output of the last print statement
goes to STDOUT if the program is running in the background?



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

Date: 23 Apr 2008 21:39:58 GMT
From: xhoster@gmail.com
Subject: Re: sending output to STDOUT or a file
Message-Id: <20080423174000.656$D9@newsreader.com>

Vahid Moghaddasi <moghaddasi@gmail.com> wrote:

> >
> > 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
> >
> Yes that does the trick, thank you.
> I don't understand how does the output of the last print statement
> goes to STDOUT if the program is running in the background?

Sorry, I don't understand the question.  Maybe you are confusing
STDOUT with the terminal.  Often STDOUT is hooked up to the terminal,
but sometimes it is not.

The concept of "background" is somewhat fuzzy.  In Linux, if you start a
program "in the background" by adding a &, that doesn't change the STDOUT
of the program.  For example, the below prints "1" because the program's
STDOUT is still the terminal, despite being run in the background.

perl -le 'sleep 3; print -t STDOUT' &

Where as this doesn't print "1", because it's STDOUT is not the terminal,
even though it is running in the foreground;

perl -le 'warn -t STDOUT' > /dev/null

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: 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 1480
***************************************


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