printing problems

They have: 5,633 posts

Joined: Jan 1970

how do you print from a html file

They have: 16 posts

Joined: Jan 2001

How long is a piece of string?

Can you refine the question somewhat? If you want to take a HTML page, and include it for output in your script, then something like:

open FH, '</path/to/file.html' or die $!;
flock FH, 1 or die $!;
while (<FH>) { print $_ }
close FH;
'

Would do it.

my $friends = qq[=:Jilly , Andrew , Peter & Harry:=];
print &welcome($friends), "2"; my @home;
sub welcome{my $s=shift; $s=~s{^(=\Smiling(.+?)(\:=)$}
{$_=$2;@home=split/[&,]/;}esgx;$friends= join'me',@home;
$friends=~s{(\A|\S|\s+)([A-Z]).+?(\s|\Z)}{$2}sge;my$c=-1;
$friends=~s{(me|\Z)}{++$c;@_=(qw|ust nother erl acker|);qq!$_[$c] !;}eg; $friends}

They have: 850 posts

Joined: Jul 1999

Matt, sorry for not mentioning it before, but welcome to TWF, and thanks for your contributions so far!

Just a simple thing with your code, if he was planning on printing any output to the browser, he will need:

print "Content-type:text/html\n\n";
.

Here is my simple solution

#!/usr/bin/perl -w
my $file = 'filename.html';
print "Content-type:text/html\n\n";
open(IN,$file) or die "Cannot open $file: $!";
print while <IN>;
close(IN);
'

Just curious, is there a reason to flock the file when reading content? I always thought that flock was only required when writing to a file?

They have: 16 posts

Joined: Jan 2001

Hi Rob, thanks for the welcome Smiling

Quote: Just curious, is there a reason to flock the file when reading content? I always thought that flock was only required when writing to a file?

I always lock to read and write out of habit. A file lock is conditional, so if it's locked to write (flock FH, 2 or better flock FH, LOCK_EX) and a non-locked request to read is made, the write lock is dropped and the data *can* be flushed before it's printed back.

I learnt that the hard way. Smiling

my $friends = qq[=:Jilly , Andrew , Peter & Harry:=];
print &welcome($friends), "2"; my @home;
sub welcome{my $s=shift; $s=~s{^(=\Smiling(.+?)(\:=)$}
{$_=$2;@home=split/[&,]/;}esgx;$friends= join'me',@home;
$friends=~s{(\A|\S|\s+)([A-Z]).+?(\s|\Z)}{$2}sge;my$c=-1;
$friends=~s{(me|\Z)}{++$c;@_=(qw|ust nother erl acker|);qq!$_[$c] !;}eg; $friends}

They have: 16 posts

Joined: Jan 2001

Oh, and a good habit to get into is to be explicit about the file read/write request.

Use either '<$file' to read, '>$file' to write and '>>$file' to append. Using '$file' on it's own can lead to exploitation of the file while the data is being processed.

I got my ass burned at comp.lang.perl.misc for just using open FH, $File;

my $friends = qq[=:Jilly , Andrew , Peter & Harry:=];
print &welcome($friends), "2"; my @home;
sub welcome{my $s=shift; $s=~s{^(=\Smiling(.+?)(\:=)$}
{$_=$2;@home=split/[&,]/;}esgx;$friends= join'me',@home;
$friends=~s{(\A|\S|\s+)([A-Z]).+?(\s|\Z)}{$2}sge;my$c=-1;
$friends=~s{(me|\Z)}{++$c;@_=(qw|ust nother erl acker|);qq!$_[$c] !;}eg; $friends}

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.