Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

HTML embedded in perl

by Anonymous Monk
on Dec 16, 2002 at 10:38 UTC ( [id://220155]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, Having a few problems... I want to add some pretty colours to a cgi that i'm writting but am getting a bit confused with back-slashing etc. I thought this would work..
print STDOUT "<FONT COLOR\="red"> $data </FONT><P>";
but was wrong.... is it the actual 'red' thats wrong. e.g do i need to use the hexidecimal number codes instead?? cheers

Replies are listed 'Best First'.
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

      "# 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

        ;-)

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Excuse me, but this won't work. $data and $some_content won't get interpolated. You have to use << "HTML" instead.

      update: forget this, look at the replies to this node, they're right. And the ancestor node is right too. The only node not being right is this one. :-}

      Bye
       PetaMem

        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

        Actually interpolated context is the default. You don't need the rabbit ears.

        $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

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.
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-

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
Re: HTML embedded in perl
by ph0enix (Friar) on Dec 16, 2002 at 10:55 UTC

    You must escape double quotes or use the single one. So you can use one of

    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
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.
    vox 269.226.9550 ext 24
    fax 269.349.9076
     http://www.nexcerpt.com
    ...Nexcerpt...Connecting People With Expertise

      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.

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

Re: HTML embedded in perl
by dga (Hermit) on Dec 16, 2002 at 16:28 UTC

    HTML will use either type of quote so you could do

    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.

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\".
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 :-)
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
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
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://220155]
Approved by wil
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-18 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found