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

Hi, I am writing a perl CGI at the minute but am having problems adding some html tags in the .cgi script. For example i want to out put some text in courier font which i thought would be done like this;
print STDOUT "<font face ="courier"> blah blah </font>";
I also want to add links to the .cgi scripts but the normal html way isn't working for me. Any ideas???

Replies are listed 'Best First'.
Re: HTML tags in perl CGI scripts
by LTjake (Prior) on Nov 14, 2002 at 12:28 UTC
    The short: you're having a problem with your quotes.
    print "<font face=\"courier\">xyzzy</font>";
    The other short: There are better ways to print HTML. CGI.pm comes in handy, or maybe even HTML::Template. Check 'em out.

    HTH

    Update: Oh, and incase you haven't seen it. use CGI or die;.

    --
    Rock is dead. Long live paper and scissors!
Re: HTML tags in perl CGI scripts
by ColtsFoot (Chaplain) on Nov 14, 2002 at 12:29 UTC
    Could I suggest that you take a look and the CGI module when
    writing cgi scripts
    Update:Not fast enough on the 'Submit' button
Re: HTML tags in perl CGI scripts
by rruiz (Monk) on Nov 14, 2002 at 23:33 UTC

    Appart from the comments already made on this question, you may also use q and qq, for example:

    print qq{<font face = "courier"> blah blah </font>};

    You should use qq if you want the variables on the script to expand, like in:

    $name = 'Roberto'; print qq{<font face = "courier"> name = $name </font>};

    And q if you don't need variable expansion.

    <classical see also> perldoc perlop and search for "Regexp Quote-Like Operators". </classical see also>

    God bless you
    rruiz

Re: HTML tags in perl CGI scripts
by Maclir (Curate) on Nov 16, 2002 at 14:03 UTC
    Others have comments on the need to use the CGI.pm module. Several others have discussed the need (or lack of a need) to quote HTML attributes. Those two horses are well and truly deceased.

    Wht did you enclose your print string in double quotes? Are there values you want interpolated? If not, you are wasting resources. Try: print STDOUT '<font face ="courier"> blah blah </font>'; instead. Except, by now you are no doubt using the CGI.pm methods.

Re: HTML tags in perl CGI scripts
by mojobozo (Monk) on Nov 14, 2002 at 13:01 UTC
    One thing I have found in HTML, if you want to do it that way you can and the QUOTES DON'T MATTER. I've been doing this a lot recently with ASP pages and have started leaving the quotes out entirely and have had no problems. TMTOWTDI!
    _____________________________________________________
    mojobozo
    word (wûrd)
    interj. Slang. Used to express approval or an affirmative response to
    something. Sometimes used with up. Source
      It is true that, in general, the parsers won't care if your values are quoted or not. But, I'm going to have to recommend against this.

      If you want to be forward thinking in the least, you should try hard to conform to W3C standards.
      By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa. Authors may also use numeric character references to represent double quotes (&#34;) and single quotes (&#39;). For double quotes authors can also use the character entity reference &quot;. (source)


      --
      Rock is dead. Long live paper and scissors!
        HTML (which is supposed to be an SGML application) doesn't require quotation marks around attribute values. The next paragraph of the specification says:

        In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them.

        It also recommends including the quotes. For one thing, XML requires quotation marks and always including quotes will make code easier to transition to XHTML.

        It is dangerous to leave out the quotes when interpolating a variable. Someone in the future might try to put a space (or a quote) in the variable. Browsers try to deal with broken markup but it is wise not to abuse the browser. Anyone leaving out the quotes because they haven't had a problem yet is going to be scratching their head in the future wondering why their HTML isn't working on some other browser or made an unrelated change in the code.

        Sorry, I never read it.
        _____________________________________________________
        mojobozo
        word (wûrd)
        interj. Slang. Used to express approval or an affirmative response to
        something. Sometimes used with up. Source
      The quotes only matter if you use whitespace or possibly [^a-zA-Z0-9] type chars, such as '=', quotes and so forth. Most of the html parsers i've seen treat whitespace as the attribute delimiter for html tags. Example: <tag attr=thisworks attr2=this does not work>, although some browsers my parse that in different ways.