[ back to toc ]

Printing files in perl

Date: 2002/05/21 11:19

Q:
I am trying to print the contents of a file (called header.html) using a
perl script. I just can't seem to get it working. I can open the file but
I just can't print the contents. I am using the following chunk of code.

$fh = new FileHandle;
if ($fh->open("> ./header.html")) {
print "in";#debug
print <$fh>;
$fh->close;
}

Any suggestions would be appreciated.
A:
print "in"

prints to the standard output.

print <$fh>;

prints a line read from the file (which is actually undef as the file was
opened for output) to the standard output.

To print something into the file you have to

print $fh "string"

(note there is no comma after $fh)

Regards,
Peter
Q:
Thank you for answering this. I did not make my question quite as clear as
I should have. I want to read the contents of a file into the perl script.
That is, I want to print the contents of header.html to the standard
output.

Thanks

A:
This is quite simple:

open(F,"<header.html") or die "Can not open the file";
undef $/;
$f = <F>;
close F;
print $f;

Regards,
Peter

[ back to toc ]