Writint to a file

P

perlUSER

I am writing a small report and got a quick question. Consider the
follwoing example:

format STDOUT =
Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
$album, $artist, $price
..


$album = "Test album";
$artist = "Brit";
$price = "9.99";
write();

When I execute this snippet, I can see the results on the screen. Can
someone please tell me how to re-direct the report to a file? Any help
is greatly appreciated.

Regards,
Sri.
 
M

Mothra

perlUSER said:
I am writing a small report and got a quick question. Consider the
follwoing example:

use strict;
use warnings;
use vars qw($album $atrist $price);

open (LABEL, ">full_path_to_file/filename") or die "Can't create file:$!\n";
format STDOUT = format LABEL =
Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
$album, $artist, $price
.


$album = "Test album";
$artist = "Brit";
$price = "9.99";

write LABEL;

Hope this helps

Mothra
 
U

usenet

perlUSER said:
When I execute this snippet, I can see the results on the screen. Can
someone please tell me how to re-direct the report to a file?

Besides the "ordinary" approach that Mothra provided, you can also use
handy trick like this. Open a file, obviously:

open (OUT, ">/path/to/my/file.txt");

Now you can add a line like this:

*STDOUT = *OUT;

That will re-direct STDOUT to your file, so that whatever was printing
to your terminal now goes to the file (you can redirect ANY handle to
ANY other handle this way - which is powerful). You can switch between
terminal-viewing and file-writing by simply commenting or un-commenting
this line.

But I wouldn't leave it that way for "production" code. That is more of
a development technique, IMHO.
 
J

John W. Krahn

Besides the "ordinary" approach that Mothra provided, you can also use
handy trick like this. Open a file, obviously:

open (OUT, ">/path/to/my/file.txt");

Now you can add a line like this:

*STDOUT = *OUT;

That will re-direct STDOUT to your file, so that whatever was printing
to your terminal now goes to the file (you can redirect ANY handle to
ANY other handle this way - which is powerful). You can switch between
terminal-viewing and file-writing by simply commenting or un-commenting
this line.

Or you could use select:

select OUT;

Which will give the save effect without modifying the STDOUT typeglob.



John
 

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

Forum statistics

Threads
474,266
Messages
2,571,085
Members
48,773
Latest member
Kaybee

Latest Threads

Top