#!perl -T # mtv.pl - Merlyn Technique Viewer by epoptai # 1. Interleaves source code with description. # 2. Code listed in textarea for editing and saving. use strict; use CGI qw(:standard); use LWP::Simple qw(get getstore); use HTML::Entities; use vars qw(%code $code $init $tmp $src $oop $wtc); use HTML::TableExtract; use CGI::Carp 'fatalsToBrowser'; my@oops=qw(10 13 28 38 48 55); # columns with double code listings my$url = url(); my$dat = 'index.dat'; unless(-e $dat){$init=1} &import_names('IN'); my$merlyn = 'http://web.stonehenge.com/merlyn/WebTechniques'; if($IN::action eq 'bindex'){&indexer} if(($IN::action eq 'save as')or($IN::codename)){&savecode} unless($IN::action eq 'view'){ print header,start_html(-title=>'MTV',-bgcolor=>'white',-text=>'black'),blockquote,h1('MTV'); print< Web Techniques Perl Columns of Randal L. Schwartz. On his home page he says of these articles,
"Because I'm generally busy (or lazy), I simply ran the source files through the pod2html translator so that I could put them online here. It makes them ugly sometimes, but you'll just have to live with that for now until someone comes up with a better solution."

A better solution

  1. Code description interleaved with source code.
  2. Code listed in a form for easy editing and saving.
This script fetches selected articles and code listings from their source locations, modifies the contents, and displays the results. An index data file of all the current web technique articles must be built and refreshed once in a while. HTML if($init==1){ print qq~

Datafile not detected. Build an index.~; } if($init!=1){ my%dex = (); my$md = (stat($dat))[9]; open(FILE,"< $dat") or die "$!"; my@dat = ; close(FILE) or die "$!"; for(@dat){ my($num,$desc)=split(/\t/); $dex{$num}=$desc } my$c=keys(%dex); my$cc=($c/4); print qq~

Index of $c Web Techniques columns
~; printf "%s updated: %s\n", $dat, scalar localtime($md); print qq~


Note: '00ps' marks double code listings (@oops) which choke MTV.
They are diplayed without enhancement.


coded by epoptai~; } print end_html; exit; } if($IN::action eq 'view'){ $src = 'col' . $IN::getdoc . '.listing.txt'; $tmp = $^T . '.tmp'; my$col = 'col' . $IN::getdoc . '.html'; $wtc = $merlyn.'/'.$col; my$wts = $merlyn.'/'.$src; for(@oops){ if($IN::getdoc=~$_){$oop=1}} if($oop==1){ my$MTV=get($wtc); print header,blockquote; print $MTV; print end_html; exit(0); } my$res; getstore($wtc,$tmp); # had problems doing this with get $code = get($wts) or die "$!"; unless(-e $tmp){ print header,start_html(-title=>"Column $IN::getdoc source code listing",-bgcolor=>'white',-text=>'black'),blockquote,p("The newest columns are not available online until after they appear in print in Web Techniques magazine. Until then only the source code is published at Randal's site. Try again soon! Back to index."); encode_entities($code); print "

$code
"; print end_html; exit(0); } } open(F,"< $tmp") or die "$!"; my@col = ; close(F) or die "$!"; my$MTV=(); { open(F,"< $tmp") or die "$!"; local $/ = undef; $MTV = ; close(F) or die "$!"; } if($IN::action eq 'view'){ unlink $tmp} my@c=@col; for(@col){ if($_=~/=(\d+)=/){ # find code listing lines my$m=$&; # define hash key if($_=~/=1=/){$_=~s/<(.*?)>//g} # fix =1= $m=~s/=(\d+)=/$1/; # strip = = from key $_=~s/=(\d+)=/$1\./; # and value $_=~s/^\s+//g; # strip leading whitespace $code{$m}=$_; # build code hash } } my$h=(keys(%code)+3); my$form = "