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

How can i substitute the titles between the title tags for a file and make changes to the file-verses-screen, without using the cgi, or any outside modules. I am a new perl programmer and have attempted to substitute via reg. express, etc, but to no avail. I preapologize if this is a silly question. I have searched many sites on the web, including perl monks and CPAN, and i have found very little that i can use without CGI. I have found a few scripts to print out just title tags, substitute title tags, etc, but not in between. My web admin has refused to install the badly needed modules. Currently running perl 5.005_02 on sun.

Replies are listed 'Best First'.
Re: title sub
by jjhorner (Hermit) on Jun 05, 2000 at 22:37 UTC

    You can install modules in your home directories.

    perl Makefile.PL LIB=/home/joseph/perllib make make test make install

    Then you can call the module using the 'use lib' pragma.

    use lib '/home/joseph/perllib'

    Note:

    The above example comes from page 166 of merlyn's and Joseph N. Hall's book _Effect Perl Programming_.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
HTML title substitution
by Corion (Patriarch) on Jun 05, 2000 at 22:38 UTC

    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 :)

      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 :)

Re: title sub
by KM (Priest) on Jun 05, 2000 at 22:30 UTC
    I don't know about anyone else, but I have no clue what you are trying to do. Care to be more consise? Are you saying that when the HTML page has <TITLE>HELLO<TITLE> you want it to display to the client as <TITLE>GOODBYE<TITLE> ?

    Cheers,
    KM

      Tks for the speedy reply. Yes, i have an html file, which i'd like to use as a template of sorts. I'd like to be able to substitute just what's between the title tags; just as your example mentioned. I'd like to change from <TITLE>HELLO</TITLE> to <TITLE>GOODBYE</TITLE>, and be able to edit to the file directly, instead of printing to the screen.
      Tks for the speedy reply. Yes, i have an html file, which i'd like to use as a template of sorts. I'd like to be able to substitute just what's between the title tags; just as your example mentioned. I'd like to change from <TITLE>HELLO</TITLE> to <TITLE>GOODBYE</TITLE>, and be able to edit to the file directly, instead of printing to the screen.
        If you want to change the file directly, the proper technique is to:
        • open a Read/Write filehandle to the file
        • read the file into a scalar
        • use a supplied regex to modify it
        • print the scalar to the filehandle
        The node How do I write to a file? in tue Q&A section on files may be of assistance.