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

Hello Perlmonks, I'm in need of some help. I am trying to figure out how to replace a tag in a HTML file with a list pulled from a flat-file db. Here's what I've got so far...the following will open the header.htm file and change any LIST tags with the word My_List. I would like to learn how to open an existing flat-file, which has one entry per line, and display it as a list.
#!/usr/bin/perl { &header; } sub header { $header="header.htm"; open (HEAD,$header) || die "can't open $header"; $header=""; while (<HEAD>){$header .=$_;} close(HEAD); $header=~s/\<LIST\>/My_List/gie; print "Content-type: text/html\n\n"; print "$header"; } 1;

Replies are listed 'Best First'.
Re: Replacing a Tag With a List
by jdporter (Paladin) on Nov 30, 2006 at 21:24 UTC

    You don't need that /g, in this case, and you really don't need that /e.

    use CGI qw/:standard/; open F, "< $flatfile_db" or die "read $flatfile_db: $!"; my $List = ul(li([<F>])); $header =~ s/<LIST>/$List/i;
    We're building the house of the future together.
Re: Replacing a Tag With a List
by Joost (Canon) on Nov 30, 2006 at 21:14 UTC
Re: Replacing a Tag With a List
by swampyankee (Parson) on Nov 30, 2006 at 20:56 UTC

    I'd suggest you do a supersearch. HTML processing is frought with peril, and is best done with a module. One obvious issue is that your code will break if <LIST> is, for example, <list>.


    <pr>Jdporter pointed out that I misread the regexp with <LIST>, so I struck out the third sentence. I still think that the OP would be better off using a module for this sort of processing.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
      if ( "/gie" =~ /i/ ) { print "Are you blind?"; }
      We're building the house of the future together.