Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Efficient code to auto-generate web pages

by Anonymous Monk
on Jan 09, 2002 at 02:04 UTC ( [id://137274]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

The following code will take an active newsgroups file as input, and output a web page containing some basic page elements, a blurb about each hierarchy, each newsgroup that we carry with a link to it on our news server, and a brief description of the group. It will generate one web page for each of the news hierarchies that we carry. It seems as though I'm writing alot of uneccessary code to accomplish this. Any thoughts?


#!/usr/local/bin/perl -w use strict; my $file = "/export/home/limo/newsgroups"; my $newfile = "/export/home/news/newfile.html"; my $altfile = "/export/home/news/altfile.html"; my $biofile = "/export/home/news/biofile.html"; my $bizfile = "/export/home/news/bizfile.html"; my $comfile = "/export/home/news/comfile.html"; my $gnufile = "/export/home/news/gnufile.html"; my $govfile = "/export/home/news/govfile.html"; my $k12file = "/export/home/news/k12file.html"; my $micfile = "/export/home/news/micfile.html"; my $newfile = "/export/home/news/newfile.html"; my $govfile = "/export/home/news/govfile.html"; my $otherfile = "/export/home/news/otherfile.html"; my $ng = "newsgroups"; use vars qw ($line $dsc $grp $title $blurb $header $footer $head); use vars qw (%alt %bio %bit %biz %com %gnu %gov %k12 %mic %new %other +%rec %sci %soc %tal %vms); ############################################## $header = <<HEADER bunch of html here. HEADER ; ############################################## $footer = <<FOOTER more html FOOTER ; ############################################## open FILE, $file || die "$!\n"; open ALTFILE, ">>$altfile" || die "$!\n"; while ($line = <FILE>) { chomp $line; if ($line =~ /(.*?)\s+(.*$)/) { $grp = $1; $dsc = $2; if ($grp =~ /^alt/) { $alt{$grp} = $dsc; $title = "alt.* $ng";} elsif ($grp =~ /^bio/) { $bio{$grp} = $dsc; $title = "bionet.* $ng" ;} elsif ($grp =~ /^bit/) { $bit{$grp} = $dsc; $title = "bit.* $ng" ;} elsif ($grp =~ /^biz/) { $biz{$grp} = $dsc; $title = "biz.* $ng" ;} elsif ($grp =~ /^com/) { $com{$grp} = $dsc; $title = "comp.* $ng" ;} elsif ($grp =~ /^gnu/) { $gnu{$grp} = $dsc; $title = "gnu.* $ng" ;} elsif ($grp =~ /^gov/) { $gov{$grp} = $dsc; $title = "gov.* $ng" ;} elsif ($grp =~ /^k12/) { $k12{$grp} = $dsc; $title = "k12.* $ng" ;} elsif ($grp =~ /^mic/) { $mic{$grp} = $dsc; $title = "microsoft.* .$ng +" ;} elsif ($grp =~ /^new/) { $new{$grp} = $dsc; $title = "news.* $ng" ;} elsif ($grp =~ /^rec/) { $rec{$grp} = $dsc; $title = "rec.* $ng" ;} elsif ($grp =~ /^sci/) { $sci{$grp} = $dsc; $title = "sci.* $ng" ;} elsif ($grp =~ /^soc/) { $soc{$grp} = $dsc; $title = "soc.* $ng" ;} elsif ($grp =~ /^tal/) { $tal{$grp} = $dsc; $title = "talk.* $ng" ;} elsif ($grp =~ /^vms/) { $vms{$grp} = $dsc; $title = "vmsnet.* $ng" ;} else { $other{$grp} = $dsc; }}} ##################################################### $head = <<HEAD <span class="centerTopTitle">$title</span> <hr align=left width="40%"><br> HEAD ; ##################################################### $blurb = <<BLURB <p>Name a topic, and it's in here. The rules for creating an <strong>alt.*</strong> newsgroup are easier than the proced +ures in the Usenet hierarchies, so new groups appear all the time. <p>At myco, we generally don't take every new <strong>alt.*</strong> group that is created out on the net, s +o <a href="http://srv.myco.org/helpdesk/f_feedback.html">submit +a request</a> if you'd like access to an alt.* group you've heard about. <br><br> BLURB ; print ALTFILE $header; print ALTFILE $head; print ALTFILE $blurb; foreach my $key (sort keys %alt) { print ALTFILE <<ALT \<a href\=\"news\:\/\/news\.myco\.org\/$key\"\>$key\<\/a\><br> +$alt{$key}<br> ALT ; } print ALTFILE $footer;
I know that to finish this, I will need to include a blurb for each hierarchy, but there's got to be a more efficient way to code the rest of this besides the above 16 or so times. For those of you who wish to see what the final pages will look like, check them out here

Replies are listed 'Best First'.
Re: Efficient code to auto-generate web pages
by kwoff (Friar) on Jan 09, 2002 at 02:13 UTC
    `perldoc perldsc`
      Thanks for your response. I realize that a data structure such as a LoH might help, but as a relatively inexperienced perl hacker, I was hoping for something a little more concrete. Mind you, I don't need this coded for me, but a bit more direction would help.
        References quick reference might be helpful in figuring out how to get ideas down into code.

        A concrete idea is that your long list of hashes should all be one hash of hashes. That reduces the insert lines down to something like:

        my $base = substr($grp, 0, 3); if (exists $is_hierarchy{$base}) { $dsc{$base}{$grp} = $dsc; $title = "$base$ng"; } else { $dsc{other}{$grp} = $dsc; }
        (And then the information about all of the different hierarchies you have has to be popualted in a hash %is_hierarchy.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://137274]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found