in reply to title sub

I assume that your problem lies within the regular expression and not in the CGI-handling, although your question dosen't really make that clear.

I'm taking this regular expression ("RE") from an old shell script called sitemap which did a site map in bash and some GNU tools - you might want to read more on regular expressions in perlre...

# for offline conversion, set this to 1 $offline = 0; undef $/; open( FILE, "< $filename" ) or die "Can't open $filename : $!\n"; $HTML = <FILE>; close FILE; # Note that you can use \2 (or $2) in the new title part # to reference the old title. $HTML =~ s!(.*?<TITLE>)(.*?)(</TITLE>.*)!... new title here ...!ims; ($Left, $Title, $Right) = ($1,$2,$3); # Here, $Left contains everything left of the page title # $Right contains everything right of the page title # and $Title contains the page title # here you could do some more magic to the page title # before assigning it back to $HTML ... if ($offline) { rename $filename, "$filename.backup"; open( FILE, "> $filename" ) or die "Can't create $filename : $!\n" +; print FILE $HTML; close FILE; } else { print FILE; };

As you might (or might not) notice, this code will have problems with malformed HTML or a document that contains two TITLE tags, only the first such tag will get handled. Also, if you have a commented-out TITLE tag, this will also confuse the RE. But as you say that you want to use this in a template, you can craft your template around that, I hope :)

Replies are listed 'Best First'.
RE: HTML title substitution
by KM (Priest) on Jun 05, 2000 at 23:00 UTC
    And, if you are to use this code, you will want to make sure you have warning on, as well as be using the strict pragma, which this code does not have (And yes Corion, I think not showing a newbie to use strict is a reason for --, so don't whine when you see it).

    The RE from above can also just be made into something like:

    $HTML =~ s!(<TITLE>)(.*?)(</TITLE>)!$1 New Title $2!gism;

    There are also various template modules and HTML modules on CPAN you may want to look at (like HTML::Parser, and HTML::Entities). But, it all depends on the scope of what you are trying to do here.

    Cheers,
    KM

      To be honest, I haven't ever used the strict pragma - I know this lets me slip even lower with my reputation ... But I have a strong Pascal background to show for an excuse :) - I guess it's time to read perlman:lib:strict for me ...

      But I think you are right with the warnings :)

        Admission is the first step to recovery.

        Cheers,
        KM