Redirecting STDOUT to a file

Discussion in 'Perl Misc' started by nickelstat@sbcglobal.net, Apr 25, 2006.

  1. Guest

    But using a file handle.

    This works:

    open(FH, ">>$file");
    close STDOUT;
    open STDOUT, ">&FH"; # stdout = dup()
    print "this goes to the file\n";


    This does NOT work, using a file handle variable

    my $fh = new FileHandle;
    open($fh, ">>$file");
    close STDOUT;
    open STDOUT, ">&$fh"; # stdout = dup()
    print "this does not go anywhere\n";

    How can I make the code with the filehandle variable work?


    tia
    , Apr 25, 2006
    #1
    1. Advertising

  2. wrote:
    > But using a file handle.
    >
    > This works:
    >
    > open(FH, ">>$file");
    > close STDOUT;
    > open STDOUT, ">&FH"; # stdout = dup()
    > print "this goes to the file\n";
    >
    >
    > This does NOT work, using a file handle variable
    >
    > my $fh = new FileHandle;
    > open($fh, ">>$file");
    > close STDOUT;
    > open STDOUT, ">&$fh"; # stdout = dup()
    > print "this does not go anywhere\n";
    >
    > How can I make the code with the filehandle variable work?


    Except in legacy code[1] the 2-argument form open() should only ever
    be used in a few _very_ rare[2] special odd cases.

    open my $fh, '>>', $file or die $!;
    open STDOUT, '>&', $fh or die $!;

    BTW are you sure you really want to dup(2) $fh to STDOUT and not simply
    select($fh) ?

    [1] and code to run on legacy perl[3]

    [2] most programmers are unlikely to encounter one in their careers

    [3] the fact that you use the legacy FileHandle module may indicate
    that you are indeed using a very old version of perl. If this is the
    case then let us know and I can did out the answer applicable to your
    version of perl.
    Brian McCauley, Apr 25, 2006
    #2
    1. Advertising

  3. Guest

    This is perl, v5.6.0 built for sun4-solaris

    Copyright 1987-2000, Larry Wall

    Am I sure I want to dup(2)? not sure. For my purpose it does not
    matter.
    What I want: when I do "print STDOUT' the output is sent to the file
    instead.

    I didn't know that FileHandle is old. It's in my book. What is used now
    instead?
    , Apr 25, 2006
    #3
  4. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > This is perl, v5.6.0 built for sun4-solaris
    >
    > Copyright 1987-2000, Larry Wall
    >
    > Am I sure I want to dup(2)? not sure. For my purpose it does not
    > matter.
    > What I want: when I do "print STDOUT' the output is sent to the file
    > instead.


    With explicit STDOUT? Then you need the dup. select(FH) determines
    the default file when none is given with print.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Apr 25, 2006
    #4
  5. Guest

    Ah!! Thanks
    The tiny little details I tend to forget

    Did you see anything wrong with my using the scalar variable $fh,
    because it does not work.
    , Apr 25, 2006
    #5
  6. Guest

    wrote:
    > Redirecting STDOUT to a file


    You can do something like this:
    open (FILEHANDLE, ">/path/to/my/file.txt");
    *STDOUT = *FILEHANDLE;

    Now if you do a plain print() statement it will go to the file instead
    of the terminal. Just comment that second line to make output go to the
    screen instead of the file.

    --
    http://DavidFilmer.com
    , Apr 25, 2006
    #6
  7. Guest

    Thanks David

    Because I'm writing a re-usable utility, I cannot use a global
    filehandle, I have to use a variable.
    , Apr 25, 2006
    #7
  8. Ben Morrow Guest

    Quoth :
    > I didn't know that FileHandle is old. It's in my book. What is used now
    > instead?


    IO::Handle and its subclasses, if you want OOish filehandles. I'd
    generally just use 5.6's lexical FHs instead; that is, to open a file
    I'd say

    open my $FH, '>', 'file' or die "can't write 'file': $!";

    Ben

    --
    BEGIN{*(=sub{$,=*,=sub{print@_};my($x,$w,$y)=@_;for(keys%$x
    ){/main/&&next;*;=$$x{$_};/(\w)::$/&&(&(($x.$_,$w.$1,$y),next);$y==\$;&&&,($w.$
    _)}};*_=sub{for(@_){$|=(!$|||$_||&,(q) )));&((q:\:\::,q,,,\$_);$_&&&,("\n")}}}_
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,
    Ben Morrow, Apr 26, 2006
    #8
  9. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > Ah!! Thanks
    > The tiny little details I tend to forget
    >
    > Did you see anything wrong with my using the scalar variable $fh,
    > because it does not work.


    I don't see you using anything right here, and I'm not going to
    review the thread to find out what you are talking about. Quote
    context when you reply.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Apr 26, 2006
    #9
  10. Ray Guest

    So how would you effectively use tee from inside Perl - thatis, write
    to stdout AND a file?
    Ray, Apr 26, 2006
    #10
  11. Ben Morrow Guest

    Quoth "Ray" <>:
    > So how would you effectively use tee from inside Perl - thatis, write
    > to stdout AND a file?


    Err... other than

    open my $FH, '|-', qw/tee file/ or die "can't fork tee: $!";

    ? :) You have to output everything twice; but I'm sure there is a module
    on CPAN to do it for you.

    Ben

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
    Ben Morrow, Apr 27, 2006
    #11
  12. wrote:
    > wrote:
    > > Redirecting STDOUT to a file

    >
    > You can do something like this:
    > open (FILEHANDLE, ">/path/to/my/file.txt");
    > *STDOUT = *FILEHANDLE;


    You can, but it's neither fish for fowl.

    > Now if you do a plain print() statement it will go to the file instead
    > of the terminal.


    But if that's all you want then select() if your friend.

    If you want more than that then David's solution will probably not
    suffice. David's solution is not redicting the "real" STDOUT (i.e.
    filedescriptor 1). Any C code or child processes that writes to STDOUT
    will not be redirected.
    Brian McCauley, Apr 29, 2006
    #12
    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. Douwe
    Replies:
    1
    Views:
    5,640
    Mike Schilling
    Jan 12, 2004
  2. Elad
    Replies:
    0
    Views:
    410
  3. SamG
    Replies:
    10
    Views:
    424
  4. =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z

    Issue of redirecting the stdout to both file and screen

    =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z, May 28, 2007, in forum: Python
    Replies:
    5
    Views:
    364
    =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z
    May 29, 2007
  5. comp.lang.ruby
    Replies:
    4
    Views:
    279
    Gary Wright
    Jan 11, 2010
Loading...

Share This Page