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

I have a subroutine that takes a chunk of html that is going to be in a menu with a different background than the main one and returns it with font color tags around links in one color and then with font color tags around those things which do not already have font tags around them (yes I know font tags are depricated. However I am trying to make my site compatable with things like netscape 3.01, in which that is the only way to change the color of the text.)

I haven't figured out a good way to do the latter part, and after staring at it for longer than I wanted to decided I'd ask other people for ideas.

Anyway, here's the thing at the moment (I don't seem to be terribly awake and perhaps code would be a little more coherent than:)

sub menucolor { my $stuff = join '', @_; my ($ls, $le, $ts, $te) = ($Q->start_font({-color => $Page{'owner' +}{'MenuLink'}}), $Q->end_font, $Q->start_font({-color => $Page{'owner +'}{'MenuText'}}), $Q->end_font); $stuff = "<>$stuff<>" if $stuff !~ /^<.*>$/; #color links link color $stuff =~ s/(<a\s[^>]*?>)([^<]+)/$1$ls$2$le/ig; #color everything currently uncolored text color $stuff =~ s/(>)([^<]+)/$1$ts$2$te/ig; #doesn't yet do what it's su +pposed to return $stuff; }
P.S. bonus points of you recognize the reference in the title.

Replies are listed 'Best First'.
Re: This is a job for... someone else.
by zengargoyle (Deacon) on Feb 24, 2002 at 10:03 UTC

    This is a job for... a module, one of the HTML parser modules.

    It'll be easy, the link handler will wrap the link in font tags, the font handler will pass it's data untouched, the default handler will wrap it's data in font tags.

    type "html parser" in the search box at the top of the page.

Re: This is a job for... someone else.
by jonjacobmoon (Pilgrim) on Feb 24, 2002 at 14:55 UTC
    Sounds like a job for Template Toolkit.....


    I admit it, I am Paco.
Re: This is a job for... CGI.pm and HTML::Template
by dragonchild (Archbishop) on Feb 25, 2002 at 14:24 UTC
    use CGI. There is absolutely no good reason to roll your own HTML. There are modules out there that will do it better, faster, and cleaner than you could ever do. (If you got it as good as them, you just reimplemented what they have already done.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.