#!/usr/bin/perl -w use strict; # delacre our vars my($file,$title,$bgcolor,$text,$head,$body,$mail); my @colors = qw( white black red green blue purple yellow aqua gray silver fuchsia lime maroon navy olive); push @colors, ''; # add no color option # define a prompting subroutine that takes two arguments # the first argument is the prompt text, the second is a # a message to print if the user does not supply any input # the second argument is optional so we can allow no input sub prompt { # get the first argument to the sub from @_ my $prompt = shift @_; # get the second argument if this is not supplied then # the shift is false so the default value assigned is '' # note that although the shift does not specify @_ # this is the 'default argument' so it is exactly # the same as the shift @_ above, just shorter! my $blank_msg = shift || ''; PROMPT: print $prompt; chomp (my $input = <>); unless ($input) { if ($blank_msg) { print "$blank_msg\n"; goto PROMPT; } } return $input; } # look for a command line argument $file = shift @ARGV || ''; # prompt for a file if no command line argument supplied $file = prompt('Please enter a text file to process: ', 'Please enter a filename!') unless $file; # check that file exists and is a text file until (-e $file and -T $file) { print "File '$file' does not exist, or is not a text file!\n"; $file = prompt('Please enter a file to process: ', 'Please enter a filename!'); } # now get the user input and validate it $title = prompt('Enter the title to use for your web page: ','Please enter a title!'); $bgcolor = prompt('Enter the background color: '); # now we use grep to ensure a valid color (as defined in our # @colors array has been entered. the '' we added to @colors # via the push @color,'' means a null string is OK unless (grep{$_ eq $bgcolor}@colors) { print "The color options are @colors or enter for none!\n"; $bgcolor = prompt('Enter the background color: '); } $text = prompt('Enter the text color: '); unless (grep{$_ eq $text}@colors) { print "The color options are @colors or enter for none!\n"; $text = prompt('Enter the text color: '); } $head = prompt('Enter a heading (enter for none): '); $mail = prompt('Enter your email address (enter for none): '); # now assemble the body and heading tags $body = ". the var $last_line is used # as a flag so we know what to do my $last_line = ''; open (TEXT, "<$file") or die "Unable to open $file: $!"; # iterate over the lines in our textfile while () { chomp; $_ = '' if m/^\s*$/; # now escape the special chars and convert spaces and tabs s/&/&/g; s//>/g; s/( {2,})/' ' x length $1/eg; s/(\t)/' ' x 4/eg; # now add the line to $body with the required HTML formatting if ($_ eq '') { $body .= "
\n"; } elsif ($last_line) { $body .= "
$_\n"; } else { $body .= "

$_\n"; } $last_line = $_; } close TEXT; # print out our HTML using a herepage. In this herepage we # do not need to escape " but variables like $title etc are # correctly interpolated. talk about saving headaches # we print everything until we find the token END_HTML on # a line by itself print < $title $body


Email: $mail END_HTML