Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Redirecting STDOUT to a FileHandle ?

by chb (Deacon)
on Apr 04, 2001 at 12:04 UTC ( [id://69576]=perlquestion: print w/replies, xml ) Need Help??

chb has asked for the wisdom of the Perl Monks concerning the following question:

I want to redirect STDOUT to a FileHandle and I tried this:
#! /usr/bin/perl -w use FileHandle; $fh=FileHandle->new(">blarg"); open(STDOUT, ">&$fh") or die "Argh: $!"; print "foo bar\nbaz\n"; $fh->close();
It gives me
Argh: Invalid argument at chb.pl line 6.
After intense manual study I came across this solution:
#! /usr/bin/perl -w use FileHandle; $fh=FileHandle->new(">blarg"); open(STDOUT, sprintf(">&=%d", $fh->fileno())); print "foo bar\nbaz\n"; $fh->close();
It works, but it looks really ugly. My Question is: is there a cleaner way to do this ? Have I missed some manpage ?
- chb

Replies are listed 'Best First'.
Re: Redirecting STDOUT to a FileHandle ?
by jeroenes (Priest) on Apr 04, 2001 at 12:41 UTC
    Read perlopentut and FileHandle.
    #! /usr/bin/perl -w use FileHandle; $fh=FileHandle->new(">blarg"); die 'Not open' unless defined $fh; open(STDOUT, ">&".$fh->fileno) or die "Argh: $!"; print "foo bar\nbaz\n"; $fh->close();
    Hope this helps,

    Jeroen
    "We are not alone"(FZ)

      Hey, thanks, that looks better
      - chb
(using local typeglob) Re: Redirecting STDOUT to a FileHandle ?
by arhuman (Vicar) on Apr 04, 2001 at 13:06 UTC
    or even :
    use FileHandle; $fh=FileHandle->new(">blarg")or die "error on fh creation"; local *TOTO=$fh; open(STDOUT, ">&TOTO") or die "Argh: $!"; print "foo bar\nbaz\n"; close(TOTO);
    If ou want to go back in a known field...

    "Only Bad Coders Badly Code In Perl" (OBC2IP)
Re: Redirecting STDOUT to a FileHandle ?
by physi (Friar) on Apr 04, 2001 at 12:24 UTC
    I'm not sure if you mean that:
    open STDOUT ">whatever.dat"; print "Hello World"; close STDOUT;
    Now have a look in whatever.dat :^)
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
      No, I didn't mean that, I need a FileHandle variable which can be passed to a sub, and the sub does the redirect...
      But you are right, that didn't show up in my example code...
      -chb
Re: Redirecting STDOUT to a FileHandle ?
by jbert (Priest) on Apr 04, 2001 at 14:44 UTC
    And for TIMTOWTDI fans:
    open( FOO, "> foo" ) or die( "End of foo is nigh : $!" ); select FOO; # Hang on, isn't 'select' for networking? call_legacy_code(); # Hehe, output goes to foo exit 0; sub legacy_code { # Heh...print to the screen print "I 0wn j000!\n"; }
    Caveats:
    1. printing explicitly to STDOUT will still go to STDOUT
    2. This isn't what you asked
    3. You can probably do this with 'FileHandle' objects but this monk is too lazy to check
    4. the reputation of comments may go down as well as up, past performance is no guarantee of future success.
Re: Redirecting STDOUT to a FileHandle ?
by Anonymous Monk on Apr 04, 2001 at 19:21 UTC
    How about something like this (it is much cleaner).
    use POSIX qw(dup2); use FileHandle (); my $fh = FileHandle->new(">blarg"); dup2(STDOUT->fileno,$fh->fileno); print $fh "Um... Yah\n";
    I think this will be much easier to follow.
      (Maybe next time I will log in first)
Re: Redirecting STDOUT to a FileHandle ?
by Anonymous Monk on Apr 04, 2001 at 19:25 UTC
    Oh .. and if you want really short...
    local $fh = \*STDOUT; print $fh "Um... Yah\n";
    I'm not sure you can get any shorter than that (with out taking out the whitespace).
      (Maybe next time I will log in first )
Re: Redirecting STDOUT to a FileHandle ?
by Rhandom (Curate) on Apr 04, 2001 at 20:37 UTC
    Or yet another. This might be better than my first example because you don't have to open a file on some bad filehandle.
    use POSIX qw(dup); use FileHandle (); my $fd = dup(STDOUT->fileno); my $fh = FileHandle->new(); $fh->fdopen($fd,'w'); print "Um... Yah\n";
    If you want it shorter...
    my $fh - FileHandle->new(); $fh->fdopen(dup(fileno(STDOUT)),'w');
    The first is more readable.
Re: Redirecting STDOUT to a FileHandle ?
by isotope (Deacon) on Apr 04, 2001 at 21:02 UTC
Re: Redirecting STDOUT to a FileHandle ?
by araqnid (Beadle) on Apr 04, 2001 at 22:03 UTC
    use IO::Wrap; my $stdout = wraphandle(\*STDOUT);
    that sort of thing? (iirc IO::Wrap is not core, but on CPAN)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://69576]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2024-04-23 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found