FAQ
I have a script that does all kinds stuff.
When its running it outputs all kinds of useful information to the
screen and exits.

What I want to do is have this also sent to a file.

Any thoughts?

Thanks

Search Discussions

  • JupiterHost.Net at Jan 11, 2005 at 11:49 pm

    Larry Guest wrote:
    I have a script that does all kinds stuff.
    When its running it outputs all kinds of useful information to the
    screen and exits.

    What I want to do is have this also sent to a file.
    perldoc -f open
  • Tim Johnson at Jan 11, 2005 at 11:50 pm
    One solution would be to use a custom subroutine instead of print()ing
    directly. If you need something more robust, there are modules out
    there that can do the heavy lifting for you.


    #####################

    use strict;
    use warnings;
    open(OUTFILE,">script.log") || die "Couldn't write to script.log!\n";

    PrintOut("This goes to a file as well as the screen.\n");

    sub PrintOut{
    print @_;
    print OUTFILE @_;
    }

    #####################





    -----Original Message-----
    From: Larry Guest
    Sent: Tuesday, January 11, 2005 3:40 PM
    To: 'Perl Beginners Mailing List'
    Subject: Write stdout to a file as well as stdout

    I have a script that does all kinds stuff.
    When its running it outputs all kinds of useful information to the
    screen and exits.

    What I want to do is have this also sent to a file.

    [Tim Johnson] <snip>
  • JupiterHost.Net at Jan 12, 2005 at 12:13 am

    Tim Johnson wrote:
    One solution would be to use a custom subroutine instead of print()ing
    directly. If you need something more robust, there are modules out
    there that can do the heavy lifting for you.


    #####################

    use strict;
    use warnings;
    open(OUTFILE,">script.log") || die "Couldn't write to script.log!\n";
    - or die is better than || die
    - why go to all the trouble of giving an error if you don't use the
    error? (IE $!) :)
    PrintOut("This goes to a file as well as the screen.\n");
    Why use a subroutine just to print to a filehandle?

    Also you didn't ever close it which could be bad :)

    use strict;
    use warning;
    open FH, '>./script.log' or die "Open script.log for writing failed: $!";
    print LOG "print to LOG like whenever you want to add something to it\n";
    close FH;

    You may also want to consider locking it either with flock if your
    system supports it or creating/deleting/checking for a lock file.
  • Chance Ervin at Jan 12, 2005 at 1:12 am
    why not just re-direct from the command line if all output is needed?

    perl.program > output.file

    --
    -----------------------------
    InteleNet Communications Inc. "Help me help you."
    Chance Ervin - SCSA -- Jerry Maguire
    Oracle Certified Professional
    Systems Engineer

    ________________________________

    From: Larry Guest
    Sent: Tue 1/11/2005 3:40 PM
    To: 'Perl Beginners Mailing List'
    Subject: Write stdout to a file as well as stdout



    I have a script that does all kinds stuff.
    When its running it outputs all kinds of useful information to the
    screen and exits.

    What I want to do is have this also sent to a file.

    Any thoughts?

    Thanks


    --
    To unsubscribe, e-mail: [email protected]
    For additional commands, e-mail: [email protected]
    <http://learn.perl.org/> <http://learn.perl.org/first-response>
  • Chris Devers at Jan 12, 2005 at 3:48 am

    On Tue, 11 Jan 2005, Larry Guest wrote:

    Any thoughts?
    You want to replicate the `tee` command. From its manpage:

    NAME
    tee - pipe fitting

    SYNOPSIS
    tee [-ai] [file ...]

    DESCRIPTION
    The tee utility copies standard input to standard output, making
    a copy in zero or more files. The output is unbuffered.

    The following options are available:

    -a Append the output to the files rather than overwriting them.

    -i Ignore the SIGINT signal.

    The following operands are available:

    file A pathname of an output file.

    The _Perl Cookbook_ has recipies showing how to replicate this
    behavior, e.g.:

    open(MANY, "| tee file1 file2 file3 > /dev/null") or die $!;
    print MANY "data\n" or die $!;
    close(MANY) or die $!;

    Or, to make the print statements simpler,

    # make STDOUT go to three files, plus original STDOUT
    open (STDOUT, "| tee file1 file2 file3") or die "Teeing off: $!\n";
    print "whatever\n" or die "Writing: $!\n";
    close(STDOUT) or die "Closing: $!\n";

    Or look on CPAN for modules like Tie::Tee and IO::Tee.



    --
    Chris Devers
  • Ing. Branislav Gerzo at Jan 12, 2005 at 8:21 am
    Larry Guest [LG], on Tuesday, January 11, 2005 at 15:40 (-0800)
    contributed this to our collective wisdom:

    LG> I have a script that does all kinds stuff.
    LG> When its running it outputs all kinds of useful information to the
    LG> screen and exits.
    LG> What I want to do is have this also sent to a file.
    LG> Any thoughts?

    We have module for that, and here is also small example:

    use IO::Tee;

    my $tee = new IO::Tee(\*STDOUT, ">log.txt");
    print join(' ', $tee->handles), "\n"; #could go away
    for (1..10) { print $tee $_, "\n" } #print to $tee handles
    $tee->flush;
    __END__

    helps ?


    --

    ...m8s, cu l8r, Brano.

    [Anybody can win, unless there is a second entry.]

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 11, '05 at 11:40p
activeJan 12, '05 at 8:21a
posts7
users6
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase