#!/usr/bin/perl use strict; use warnings; use POSIX qw/strftime/; use Getopt::Long; use List::Util qw/min max/; use LWP::Simple; use Text::Wrap; use lib "$ENV{HOME}/lib/site_perl/5.6.1/"; use Mail::Send; use Regexp::Common qw/number URI Email::Address/; use Roman; my ($url, $file, $email, $debug); #A statement saying how this program should be used my $usage = "Usage: --url --file [--email
] [--debug]"; GetOptions( "url=s" => \$url, "file=s" => \$file, "email=s" => \$email, "debug" => \$debug, ) or die "$usage\n"; die "$usage\n" unless defined $url and defined $file; die "'$url' does not appear to be a valid URL" unless $url =~ /$RE{URI}{HTTP}/; print "Now getting '$url'\n" if $debug; my $contents = get($url); die "Failed getting $url\n" unless $contents; print "'$url' retrieved\n" if $debug; print "Now opening $file for writing\n" if $debug; open my $ofh, '>', $file or die "Cannot open $file for writing: $!\n"; print "$file opened for writing\n" if $debug; my @numbers; print "About to begin substituting\n" if $debug; #Search for all real numbers, replace with Roman Numeral version #of integer portion of the number while ($contents =~ s/$RE{num}{real}{-keep}/Roman($4)/es){ #store the number found push @numbers, $1; } print "substitutions complete, found " . @numbers . " numbers\n" if $debug; print "Numbers: @numbers\n" if $debug; print $ofh $contents; close $ofh or die "Could not close $file: $!\n"; print "File closed\n" if $debug; if ($email){ print "Now verifying '$email'\n" if $debug; die "'$email' does not appear to be a valid email address\n" unless $email =~ /^$RE{Email}{Address}$/; print "Now sending summary to '$email'\n" if $debug; my $mail = Mail::Send->new(); $mail->to($email); $mail->subject('HW5 results'); my $fh = $mail->open; my $msg = "We successfully parsed '$url'. "; $msg .= "We found and replaced " . @numbers . " numbers, "; $msg .= "the largest of which was " . max(@numbers); $msg .= ", and the least of which was " . min(@numbers) . ". "; $msg .= "The substitution was performed on " . strftime('%B %e, %Y %l:%M%p', localtime) . '.'; { no warnings 'once'; $Text::Wrap::Columns = 70; } print $fh wrap("\t", '', $msg); print "Message composed, now sending\n" if $debug; $fh->close(); print "Email sent to '$email'\n" if $debug; } else { print "No --email supplied, no email will be sent\n" if $debug; }