Printing to a file Or STDOUT

Discussion in 'Perl Misc' started by Hal Vaughan, Aug 21, 2006.

  1. Hal Vaughan

    Hal Vaughan Guest

    I thought this was a FAQ, but I can't find it, which means it either isn't
    as obvious as I think or I'm just using the wrong search terms.

    I want to be able to print to either STDOUT or to a file, so if I have a
    series of print statements like:

    print $fh "This is a line of text\n";

    I'd like to be able to have $fh be able to refer to STDOUT so a command line
    option for my program could specify to either save the output to a file or
    print it to the console. I know I can just open the file, but how to I set
    $fh equal to the actual STDOUT stream?

    Thanks!

    Hal
     
    Hal Vaughan, Aug 21, 2006
    #1
    1. Advertising

  2. Hal Vaughan wrote:
    > I thought this was a FAQ, but I can't find it, which means it either isn't
    > as obvious as I think or I'm just using the wrong search terms.
    >
    > I want to be able to print to either STDOUT or to a file, so if I have a
    > series of print statements like:
    >
    > print $fh "This is a line of text\n";
    >
    > I'd like to be able to have $fh be able to refer to STDOUT so a commandline
    > option for my program could specify to either save the output to a fileor
    > print it to the console. I know I can just open the file, but how to Iset
    > $fh equal to the actual STDOUT stream?


    my $fh = \*STDOUT;
    print $fh "Hello, world\n";

    --
    Josef M├Âllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Aug 21, 2006
    #2
    1. Advertising

  3. Hal Vaughan

    Guest

    Hal Vaughan <> wrote:
    > I thought this was a FAQ, but I can't find it, which means it either
    > isn't as obvious as I think or I'm just using the wrong search terms.
    >
    > I want to be able to print to either STDOUT or to a file, so if I have a
    > series of print statements like:
    >
    > print $fh "This is a line of text\n";
    >
    > I'd like to be able to have $fh be able to refer to STDOUT so a command
    > line option for my program could specify to either save the output to a
    > file or print it to the console. I know I can just open the file, but
    > how to I set $fh equal to the actual STDOUT stream?



    $ perl -wle 'use strict; my $fh=*STDOUT; print $fh "foo"'
    foo

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 21, 2006
    #3
  4. Hal Vaughan

    Big and Blue Guest

    Hal Vaughan wrote:
    >
    > I want to be able to print to either STDOUT or to a file, so if I have a
    > series of print statements like:
    >
    > print $fh "This is a line of text\n";


    You could just print to stdout (ie: just print) and do the redirection
    to a file on the command line.


    --
    Just because I've written it doesn't mean that
    either you or I have to believe it.
     
    Big and Blue, Aug 22, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jody Burgess
    Replies:
    2
    Views:
    551
    George Kinney
    Jul 17, 2004
  2. Elad
    Replies:
    0
    Views:
    416
  3. Pappy
    Replies:
    3
    Views:
    385
  4. Andreas S
    Replies:
    3
    Views:
    269
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    348
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page