I light of my brainfart yesterday I felt compelled to show a revised script including proper error handling:

I blame it on the meds!

#!/usr/bin/perl # read emails I sent myself containing just a link # and make a HTML-redirect based bookmark out of them # # SPEC # read given files as internet messages # use subject as title and filename, escape as needed # use one URL in body as href (if there a two they are both the same) # and output one HTML file for each bookmark use strict; use warnings; use Carp qw(cluck confess); use Encode; use Email::Simple; use HTML::Entities; use Path::Tiny; use URI::Find; use version; our $VERSION = qv('0.0.1'); if ( @ARGV == 0 ) { confess 'Invalid number of arguments' } foreach my $in_filepath (@ARGV) { if ( not path($in_filepath)->exists ) { cluck "$in_filepath: Input file '$in_filepath' does not exist" +; next; } my $in_file = Path::Tiny->new($in_filepath)->slurp or confess "$in_filepath: Fatal error in module"; my $message = Email::Simple->new($in_file) or confess "$in_filepath: Fatal error in module"; if ( not length $message->as_string or $message->as_string =~ /\A\s*\z/ ) { cluck "$in_filepath: Invalid message"; next; } if ( not length $message->header('Subject') ) { cluck "$in_filepath: Invalid subject"; next; } my $subject = decode( 'MIME-Header', $message->header('Subject') ) or confess "$in_filepath: Fatal error in module"; my $title = HTML::Entities::encode($subject) or confess "$in_filepath: Fatal error in module"; my $out_filepath = $subject =~ s/[%\/\?<>\\:\*\|":]/_/gr . '.URL.H +TML'; # TODO IMPR URL::Search might be nicer than URI::Find for some cas +es my @uris; my $finder = URI::Find->new( sub { push @uris, $_[0] } ); $finder->find( \$message->body ); if ( @uris == 0 ) { cluck "$in_filepath: Invalid number of URIs"; +next; } if ( path($out_filepath)->exists ) { cluck "$in_filepath: Output file '$out_filepath' exists"; next; } path($out_filepath)->spew( "<!DOCTYPE html><title>$title</title><meta http-equiv=\"refresh\" cont +ent=\"0; url=@uris\">", ) or confess "$in_filepath: Fatal error in module"; }

In reply to Re^4: Please critique this script -- Read emails and write URLs into bookmarks by Anonymous Monk
in thread Please critique this script -- Read emails and write URLs into bookmarks by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.