Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Printing to a file Or STDOUT

Reply
Thread Tools

Printing to a file Or STDOUT

 
 
Hal Vaughan
Guest
Posts: n/a
 
      08-21-2006
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
 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      08-21-2006
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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-21-2006
Hal Vaughan <(E-Mail Removed)> 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
 
Reply With Quote
 
Big and Blue
Guest
Posts: n/a
 
      08-21-2006
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.
 
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
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
Printing a file to a printer &/or changing the device that is referred to by stdout Jody Burgess Python 2 07-17-2004 04:59 AM



Advertisments