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

My program highlights C/C++ code. On one page users enter the code, I process it, and print out on the other page. When I print to the browser, things like <stdlib.h> or <iostream.h> are interpreted as some misterious html tags and are not displayed.

Any way around it?

Edit Masem 2001-08-08 - Title change from 'html'

Replies are listed 'Best First'.
Re: html
by plaid (Chaplain) on May 23, 2000 at 08:45 UTC
    There are two basic ways around this. The first is to use the html special characters for this, &lt and &gt, for < and > respectively. The other solution is to enclose the entire block in a <xmp> block, the contents of which are displayed as-is.

    Updated: Brain fart. I meant xmp instead of pre. Really. I did. :)

      The second part is not correct. <PRE></PRE> will still interpret the <iostream.h>

      Use a <XMP> block or since this is permonks:
      $code =~ s/>/&gt;/g; $code =~ s/</&lt;/g;
        Or, just use HTML::Entities from the LWP library. You do have that installed, don't you?
      Make sure you use the semicolon after the &gt; and &lt; tags. Some older browsers will not parse them without them.
        Or some new ones =) The spec for those codes says that the ";" is needed unless the ampersand and codestring are followed by whitespace or other character illegal in a codestring. That means that <html> always works, <html> is wrong since <h may someday mean something and then which do you choose? IE tries to find a working match down the string which is nice but breaks in the future and shows the wrong tag. It has also taught a half million people to do it wrong and then yell at the NS and Opera people.
RE: html
by turnstep (Parson) on May 23, 2000 at 19:40 UTC
    Or, so it the reverse of what merlyn suggested: instead of knowing all the html tags, know all the C++ things likely to be included. For example, the regexp
    s/<([^> ]*\.h)>/&lt;$1&gt;/g;
    should take care of your example...