Code Style -- here-docs -- How do you make them look good?

M

MST

Usually I use perl for system admin type things, but recently I've
been writing a flurry of CGI scripts. In the process I find my self
using (abusing?) here-docs to print chunks of HTML code. I find that
no matter how hard I try I just can't find a style of writing a
here-doc that pleases me I've tried things like this:

if($test) {
print <<HTML
<p>Some html $Junk!</p>
<p>$More junk</p>
HTML
}

and that isn't so bad. What irks me is if the here-doc isn't at the
end of a block I need to throw a ; on a line all by its lonesome. This
leaves me trying to creatively position code chunks such that the
here-doc is at the end of a block (which usually results in less
readablility in the end). The readablity of my code is fairly
important as eventually I will be passing it off to a much less
seasoned perl programmer.

What do you guys think? Should I forget it and switch to using
multiple print qq()'s? Is there a better way to format the here-doc?
Maybe I just have a stick up my butt; however I wanted to see what
kind of feedback I'd get from the .misc crowd.
 
G

gnari

MST said:
if($test) {
print <<HTML
<p>Some html $Junk!</p>
<p>$More junk</p>
HTML
}

and that isn't so bad. What irks me is if the here-doc isn't at the
end of a block I need to throw a ; on a line all by its lonesome.

I think you suffer from a misconception. the ; belongs at the
end of the print line. it just happens that you can leave it out
in this case, but it is clearer if you don't.

gnari
 
P

Paul Lalli

Usually I use perl for system admin type things, but recently I've
been writing a flurry of CGI scripts. In the process I find my self
using (abusing?) here-docs to print chunks of HTML code. I find that
no matter how hard I try I just can't find a style of writing a
here-doc that pleases me I've tried things like this:

if($test) {
print <<HTML
<p>Some html $Junk!</p>
<p>$More junk</p>
HTML
}

and that isn't so bad. What irks me is if the here-doc isn't at the
end of a block I need to throw a ; on a line all by its lonesome.

No you don't. You need to put a semi-colon after the first heredoc
marker:

if ($test) {
print <<HTML;
<p>Some html here</p>
HTML
print "this prints!\n";
}

This
leaves me trying to creatively position code chunks such that the
here-doc is at the end of a block (which usually results in less
readablility in the end). The readablity of my code is fairly
important as eventually I will be passing it off to a much less
seasoned perl programmer.

What do you guys think? Should I forget it and switch to using
multiple print qq()'s?

Why multiple?

print qq
(this is one line
here's another.
and a third\n);

That will print the three lines as written, because qq obeys whitespace,
including newlines.
Is there a better way to format the here-doc?
Maybe I just have a stick up my butt; however I wanted to see what
kind of feedback I'd get from the .misc crowd.

The other thing you might want to consider is using one of the templating
modules available on CPAN. I've never used any of them extensively, so I
can't recommend one above the other. Others can, I'm sure.


Paul Lalli
 
A

Ala Qumsieh

Paul said:
No you don't. You need to put a semi-colon after the first heredoc
marker:

if ($test) {
print <<HTML;
<p>Some html here</p>
HTML
print "this prints!\n";
}

That is of course correct. But, as an Emacs user, I know for a fact that
heredocs confuse its cperl mode, and the auto indentation breaks after
the end marker. Putting a lone semicolon on a line by itself after the
end marker fixes the problem.

To the OP, I don't see how this affects readability.

--Ala
 
M

MST

Thanks for the advice, as I said I'm not usually treading in CGI land
so my CGI programming style is style being worked out. I didn't
realize that the quote like operators were ok with embedded newlines;
I guess you learn something new every day :)
 
T

Tintin

MST said:
Thanks for the advice, as I said I'm not usually treading in CGI land
so my CGI programming style is style being worked out. I didn't
realize that the quote like operators were ok with embedded newlines;
I guess you learn something new every day :)

CGI has no "programming style" nor "quote like operators". Perhaps you are
confusing CGI with Perl?
 
G

Gunnar Hjalmarsson

Tintin said:
CGI has no "programming style" nor "quote like operators". Perhaps
you are confusing CGI with Perl?

You should have read the whole thread before making such a comment. If
you had, you had realized that the OP is well aware of the distinction
between Perl and CGI. (And you wouldn't have posted.)
 
V

Vetle Roeim

[...]
That is of course correct. But, as an Emacs user, I know for a fact that
heredocs confuse its cperl mode, and the auto indentation breaks after
the end marker. Putting a lone semicolon on a line by itself after the
end marker fixes the problem.

I believe you are mistaken. It is the normal perl-mode that is confused
by the heredocs, and cperl-mode is not. At least that is my experience.


[...]
 

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
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top