Re: HTML embedded in perl
by tachyon (Chancellor) on Dec 16, 2002 at 10:52 UTC
|
With back-slashing you need to \ the " chars within the string like so (BTW generally you don't need STDOUT as this is the default):
print STDOUT "<FONT COLOR=\"red\">$data</FONT>";
The \ lets Perl know you mean a literal " not a " as in the end of the string you are printing. Typically you avoid this problem using heredocs or the qq operator
# to print a bunch of HTML using a heredoc I would do:
print <<HTML;
<html>
<head>
<title>
$title
</title>
</head>
<body>
<P><FONT COLOR="red">$data</FONT>
<P>$some_content
<-- continue -->
</body>
</html>
HTML
# or you can do this with qq (non interpolated text must not contain !
+ char)
print qq!
<html>
<head>
<title>
$title
</title>
</head>
<body>
<P><FONT COLOR="red">$data</FONT>
<P>$some_content
<-- continue -->
</body>
</html>
!;
"red" is fine but if you want to use hex it is #RRGGBB where #FF0000 is red and #0000FF is blue.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] [select] |
|
"# or you can do this with qq (non interpolated text must not contain !)"
You've missed the exclamation marks from your HTML comments start tags.
For HTML I often find it better to use a character other than an exclamation mark to begin and end the qq string, to avoid having to frequently escape them. (Characters can be anything non-alphanumeric and non-whitespace).
cheers,
michael
| [reply] |
|
| [reply] |
|
| [reply] [d/l] |
|
Sorry, but you are incorrect. qq IS what you use to double quote text and have variables interpolated. q will single quote and not interpolate.
#!/usr/bin/perl -w
use strict;
my $text = "INSERTED TEXT";
print qq! There is tome $text here \n!; # THIS INTERPOLATES
print q! There is some $text here \n!; # THIS DOESN'T
| [reply] |
|
$why_dont_you_check_your_facts_before_you_embarass_yourself = 'Oops!';
print <<THIS;
I would have to say:
$why_dont_you_check_your_facts_before_you_embarass_yourself
THIS
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] |
Re: HTML embedded in perl
by benn (Vicar) on Dec 16, 2002 at 10:52 UTC
|
You either need to escape the " " around red, thus....
print STDOUT "<FONT COLOR=\"red\"> $data </FONT><P>";
or use a different quoting mechanism...
print STDOUT qq(<FONT COLOR="red"> $data </FONT><P>);
...but as an HTML purist, I would urge you to consider using styles rather than the font tag...
print STDOUT "<span style='color:red;'> $data <span>";
Good luck. | [reply] [d/l] [select] |
Re: HTML embedded in perl
by gjb (Vicar) on Dec 16, 2002 at 12:41 UTC
|
Consider using templates, that way the HTML can reside in a separate file and can be tweaked without changing the Perl code. A few template systems that come to mind are HTML::Template and Text::Template, but there are many more on CPAN.
Hope this helps, -gjb-
| [reply] |
Re: HTML embedded in perl
by zakzebrowski (Curate) on Dec 16, 2002 at 12:45 UTC
|
Just as a side comment, why are you embeding the color within the code itself? It's easier to use a cascading style sheet (see google) if you are going to set colors a lot, and thus can make a change to a file when someone says "I want that green to be blue"... Only side note is that (until recently) most browsers do not correctly support css, so you may get various results depending on the version of (insert browser here) you use...
See my home page as an example...
----
Zak
Pluralitas non est ponenda sine neccesitate - mysql's philosphy | [reply] |
Re: HTML embedded in perl
by ph0enix (Friar) on Dec 16, 2002 at 10:55 UTC
|
print STDOUT "<FONT COLOR=\"red\"> $data </FONT><P>";
print STDOUT "<FONT COLOR='red'> $data </FONT><P>";
print STDOUT << "EODATA";
<FONT COLOR="red"> $data </FONT><P>
EODATA
| [reply] [d/l] |
Re: HTML embedded in perl
by Mur (Pilgrim) on Dec 16, 2002 at 15:57 UTC
|
Everyone else is going with the "template" or "cascading style sheets" answers, but I'm old school:
use CGI::Pretty (:standard);
print headers;
print font({-color="red"}, q(This text is red));
-- |
Jeff Boes |
Database Engineer |
Nexcerpt, Inc. |
|
|
...Nexcerpt...Connecting People With Expertise
|
| [reply] [d/l] [select] |
|
Regarding cascading style sheets, the following is excerped from the HTML 4.01 spec on the World Wide Web Consortium's website:
2.4.1 Separate structure and presentation
HTML has its roots in SGML which has always been a language for the specification of structural markup. As HTML matures, more and more of its presentational elements and attributes are being replaced by other mechanisms, in particular style sheets. Experience has shown that separating the structure of a document from its presentational aspects reduces the cost of serving a wide range of platforms, media, etc., and facilitates document revisions.
Font (along with many other elements and attributes) has been depreciated because it is a style element and doesn't really belong in the structure of a document.
| [reply] |
Re: HTML embedded in perl
by Anonymous Monk on Dec 16, 2002 at 15:29 UTC
|
No it doesn't matter about the using the word red.
You might like to try escaping out the " characters though
try
print STDOUT "<FONT COLOR=\"red\">$data</FONT>";
I won't go into why you shouldn't use the font tag at all, and that using style sheets (CSS) is so much better.
Also you might like to try writing out bulk HTML statements like this:
print STDOUT <<_EOM_;
<p>This is my html which doesn't need to be escaped <font color=red>see</font></p>
_EOM_
Cheers, Tom | [reply] |
Re: HTML embedded in perl
by dga (Hermit) on Dec 16, 2002 at 16:28 UTC
|
print STDOUT "<font color='red'> $data </font>";
This avoids a lot of backslashes in your code. As mentioned previously, putting html in your perl code is a potential maintenence headache though and hard coding colors lacks flexibility.
| [reply] [d/l] |
Re: HTML embedded in perl
by Anonymous Monk on Dec 16, 2002 at 16:22 UTC
|
You should escape the quotes around "red", as in \"red\". | [reply] |
Re: HTML embedded in perl
by amrangaye (Friar) on Dec 16, 2002 at 17:00 UTC
|
Hi. I think you should be escaping the " not the = like this:
print STDOUT "<FONT COLOR=\"red\"> $data </FONT><P>";
either that, or you use single quotes:
print STDOUT "<FONT COLOR='red'> $data </FONT><P>";
cheers :-)
| [reply] [d/l] [select] |
Re: HTML embedded in perl
by chimni (Pilgrim) on Dec 17, 2002 at 06:12 UTC
|
you have to escape both the quotes (\" \")
this will work
<b>
<font size=\"\"color=\"#3299CC\">$Msg</font></b>\n
hope this helps
chimni | [reply] [d/l] |
Re: HTML embedded in perl
by chimni (Pilgrim) on Dec 17, 2002 at 06:08 UTC
|
you have to escape both the quotes (\" \")
this will work
"$Msg\n"
hopt this helps
chimni | [reply] |
Re: HTML embedded in perl
by spacewarp (Pilgrim) on Dec 19, 2002 at 03:36 UTC
|
I just have to congratulate everyone who responded, for not saying "Use CGI.pm and avoid this." It's gotten to be a real peeve of mine to see this answer used every time someone has a problem using Perl to produce HTML.
Hurrah for you, free-thinkers!!
Spacewarp
DISCLAIMER:
Use of this advanced computing technology does not imply an endorsement
of Western industrial civilization.
| [reply] |