#!/usr/bin/perl use strict; use warnings; use open qw< :encoding(UTF-8) >; my $BASEDIR = "/htdocs/pastanoggin.com"; my $base_filename = "$BASEDIR/docs/base"; my $status_filename = "$BASEDIR/docs/status"; sub print_file { my $filename = shift; my $file = undef; open($file, "<", $filename) or die "$0: can't open $filename for reading: $!"; print while (<$file>); die "unexpected error while reading from $filename: $!" if $! } sub print_header { my $code = shift; print <<~END; Status: $code Content-Type: text/html END } sub render_page { my ($docname, $doctitle) = @_; my $doc_filename = "$BASEDIR/docs/$docname"; my $base_file = undef; open($base_file, "<", $base_filename) or die "$0: can't open $base_filename for reading: $!"; # print header and contents print_header 200; while (<$base_file>) { if (/\[%title%\]/) { print "$doctitle"; } elsif (/\[%content%\]/) { print_file $doc_filename; } elsif (/\[%status%\]/) { print_file "$status_filename"; } else { print; } } die "unexpected error while reading from $base_filename: $!" if $! } sub render_error { my $errcode = shift; print_header $errcode; print_file "$BASEDIR/docs/${errcode}.html"; } my $path = $ENV{'PATH_INFO'} || '/'; if ($path =~ /^\/?$/) { render_page 'root', 'pastanoggin!!'; } elsif ($path =~ /^\/contact\/?$/) { render_page 'contact', 'hello, is it me you\'re looking for?'; } elsif ($path =~ /^\/blog\/?$/) { render_page 'blog', 'yapyapyapyap :D'; } elsif ($path =~ /^\/links\/?$/) { render_page 'links', 'the interwebs'; } else { render_error '404'; }