Redirecting STDOUT to a file

N

nickelstat

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
 
B

Brian McCauley

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.
 
N

nickelstat

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?
 
A

Anno Siegel

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
 
N

nickelstat

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.
 
U

usenet

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.
 
N

nickelstat

Thanks David

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

Ben Morrow

Quoth (e-mail address 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
 
A

Anno Siegel

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
 
R

Ray

So how would you effectively use tee from inside Perl - thatis, write
to stdout AND a file?
 
B

Ben Morrow

Quoth "Ray said:
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
 
B

Brian McCauley

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top