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

Can someone explain to me how to generate font tags such as <font -color="red">some text here</font>?

Here is a sample of what I'm doing. Let's say I want to make the word "current" red, how can I do that? I've tried many ways that did not work...

package mywebpages; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(servePage isPageValid); require 5.003; use strict; use CGI qw(:standard escapeHTML); ################ # commonHeader # ################ # Description: produces html header common to all pages # INPUT: webpage name (ex. = home.html) # OUPUT: scalar reference containing html header sub commonHeader($) { my ($page) = @_; my $html_string; $html_string = header("text/html"). start_html(-meta=>{"Content-Type" => "text/html", "charset" => "iso-8859-1"}, -title=>"$page", -bgcolor=>"white"). p. h1("myWebSite"). hr. p("The 'current' time according to this system is: "). b($timestamp); return (\$html_string); }

Replies are listed 'Best First'.
Re: How do I create font tags with cgi.pm?
by blakem (Monsignor) on Sep 13, 2001 at 23:05 UTC
    Have you tried something like:

    print font({-color=>'red', -face=>'Arial', -size=>'3'}, 'Some Text Her +e');

    -Blake

      Thanks Blake, that did it! I'm operating in "stupid" mode today without my html manual. I was forgetting that I had to put the text I wanted to be red 'inside' the font tag---no wonder it wasn't working!

      And, unfortunately, the only "font" info in cgi.pm doc is for in-line style sheets.

      Thanks again, Apprentice (much humbled by forgetfullness)
Re: How do I create font tags with cgi.pm?
by arturo (Vicar) on Sep 13, 2001 at 23:21 UTC

    You might want to rethink using CGI.pm for this, although it's not difficult. Just because it provides the methods doesn't mean you have to use them. That aside, CGI.pm can create any kind of HTML tag you want : if you use the OO interface, print $query->foo("hah!"); will output

    <foo>hah!</foo>

    To set attributes of such a tag, the first argument you give to the method or function must be a hash reference or anonymous hash that associates each attribute with its associated value. So, in this case,

    print font( { color => "red" }, "This text is in red");

    Will produce the following HTML :

    <font color="red">This text is in red</font>

    FWIW, though, being an up-to-date, let's-follow-w3c-recommendations kinda guy, I'd use

    span({style=>"color:red"}, "This text is in red");

    or style sheets to make text red.

    Incidentally, your problem could be solved fairly simply: just embed the font tag in the text you're outputting, like so:

    print p("I like <font color='red'>red</font> text"); # or print p("I like ", font({color=>"red"}, "red"), " text");

    Or use here documents :

    print <<END_OF_TEXT; <p>I like <font color="red">red</font> text.</p> END_OF_TEXT

    Keep in mind that you *don't need* to use CGI.pm to generate all your HTML.

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: How do I create font tags with cgi.pm?
by Chmrr (Vicar) on Sep 13, 2001 at 23:07 UTC
    Try this:
    use CGI qw/:standard/; print header, start_html, p("The ",font({color=>"red"},"current")," time is ",scalar local +time), end_html;
    The CGI docs are wonderful things -- all this, plus much, much more!

    perl -pe '"I love $^X$\"$]!$/"=~m%(.*)%s;$_=$1#$&V"+@( NO CARRIER'

Re: How do I create font tags with cgi.pm?
by Masem (Monsignor) on Sep 13, 2001 at 23:45 UTC
    <HTMLPurist IGNORE="ALLOWED">

    I'd strongly suggest that instead of FONT tags, you use CSS styles instead. It will be much more portable, and IIRC, font CSS stuff is nearly 100% complete across the browser board. To use w/ CGI.pm, you'd call it as such:

    print $cgi->p( -style=>'font-color: red', "My text here" );
    </HTMLPurist>

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important