Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Redirecting STDOUT to a file

Reply
Thread Tools

Redirecting STDOUT to a file

 
 
nickelstat@sbcglobal.net
Guest
Posts: n/a
 
      04-25-2006
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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      04-25-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
nickelstat@sbcglobal.net
Guest
Posts: n/a
 
      04-25-2006
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?

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-25-2006
<(E-Mail Removed)> 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.
 
Reply With Quote
 
nickelstat@sbcglobal.net
Guest
Posts: n/a
 
      04-25-2006
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.

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      04-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
nickelstat@sbcglobal.net
Guest
Posts: n/a
 
      04-25-2006
Thanks David

Because I'm writing a re-usable utility, I cannot use a global
filehandle, I have to use a variable.

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      04-26-2006

Quoth (E-Mail Removed):
> 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)=@_;fo r(keys%$x
){/main/&&next;*;=$$x{$_};/(\w)::$/&&(&(($x.$_,$w.$1,$y),next);$y==\$;&&&,($w.$
_)}};*_=sub{for(@_){$|=(!$|||$_||&,(q) )));&((q:\:\::,q,,,\$_);$_&&&,("\n")}}}_
$J::u::t, $a::n:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-26-2006
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Ray
Guest
Posts: n/a
 
      04-26-2006
So how would you effectively use tee from inside Perl - thatis, write
to stdout AND a file?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirecting output ruby and from stdout results in blank file comp.lang.ruby Ruby 4 01-11-2010 11:10 PM
Issue of redirecting the stdout to both file and screen =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7zS?= Python 5 05-29-2007 02:12 AM
redirecting stdout to a file as well as screen SamG Python 10 04-12-2007 11:57 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
Ant redirecting stdout to a file while in a spawned java program Douwe Java 1 01-12-2004 02:16 AM



Advertisments