New to Perl

B

Brett Irving

Hi I am new to perl and am trying to do some test functions that will
let me substitute html from one file into another using markers ie.

<HTML>

<HEAD>
</HEAD>

<BODY>
<!-- ***INSERT NEW CONTENT HERE*** -->
</BODY>

</HTML>

Is there any easy way I can go about doing this??
any help would be appreciated.
 
G

Gunnar Hjalmarsson

Brett said:
I am new to perl

What has that to do with your problem?
and am trying to do some test functions that will let me substitute
html from one file into another using markers

Let us see what you have done so far. Doing so will increase your
chances to get help.
 
G

Garry Short

Brett said:
Hi I am new to perl and am trying to do some test functions that will
let me substitute html from one file into another using markers ie.

<HTML>

<HEAD>
</HEAD>

<BODY>
<!-- ***INSERT NEW CONTENT HERE*** -->
</BODY>

</HTML>

Is there any easy way I can go about doing this??
any help would be appreciated.

Assuming the above file is in template.html, and the new content is in
content.html, something like this would work:

open TEMPLATE, "template.html" or die "Can't open template.html: $!\n";
open CONTENT, "content.html" or die "Can't open content.html: $!\n";
open OUTFILE, ">outfile.html" or die "Can't write to $outfile: $!\n";
while ($t = <TEMPLATE>) {
if ($t =~ /<!-- ***INSERT NEW CONTENT HERE*** --->/) {
while ($c = <CONTENT>) {
print OUTFILE "$c";
}
} else {
print OUTFILE "$t";
}
}
close TEMPLATE;
close CONTENT;
close OUTFILE;


HTH,

Garry
 
T

Tad McClellan

Garry Short said:
Brett Irving wrote:

something like this would work:
^^^^^^^^^^

No it won't...

if ($t =~ /<!-- ***INSERT NEW CONTENT HERE*** --->/) {
^^^ ^^^ ^^^

if ($t =~ /<!-- \*\*\*INSERT NEW CONTENT HERE\*\*\* -->/) {


But pattern matching is not the Right Tool, since we are not
looking for a pattern.

We are looking for a constant string, so this would be better:

print OUTFILE "$c";


A useless use of double quotes.

perldoc -q quot

What's wrong with always quoting "$vars"?
 
A

A. Sinan Unur

(e-mail address removed) (Brett Irving) wrote in
Hi I am new to perl and am trying to do some test functions that will
let me substitute html from one file into another using markers ie.

<HTML>

<HEAD>
</HEAD>

<BODY>
<!-- ***INSERT NEW CONTENT HERE*** -->
</BODY>

</HTML>

This can get way out of hand rather quickly. You might want to take a
look at HTML::Template at search.cpan.org.
 
T

Tad McClellan

[ snip code with mistakes ]
That's what happens when you post code without really thinking about
it


No, that's what happens when you post code without testing it first. :)

If you merely test your code before offering it to thousands of people
around the world, then such a problem can be avoided without the
added effort of thinking.
 
G

Garry Short

Tad said:
^^^ ^^^ ^^^

if ($t =~ /<!-- \*\*\*INSERT NEW CONTENT HERE\*\*\* -->/) {
<SIGH> That's what happens when you post code without really thinking about
it - cheers for correcting that, Tad, should have picked that up myself :)
 
S

Steve Grazzini

This is great (and funny) advice -->

Tad McClellan said:
If you merely test your code before offering it to thousands of
people around the world, then such a problem can be avoided without
the added effort of thinking.

Of course you need somebody to say "Right.. I wasn't thinking"
as a set-up. (There never seems to be a shortage.) Anyway, I
think some similar advice for Answerers would be helpful in the
Posting Guidelines.
 
T

Tad McClellan

Steve Grazzini said:
I
think some similar advice for Answerers would be helpful in the
Posting Guidelines.


That occurred to me too when I typed the above for the 2nd time in 2 days.

:)
 
G

Garry Short

Tad said:
That occurred to me too when I typed the above for the 2nd time in 2 days.

:)

Sounds like a repetitive task to me ... have you considered writing a perl
script to automate it?

;-)
 

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,121
Messages
2,570,712
Members
47,283
Latest member
hopkins1988

Latest Threads

Top