First off, a few style related points to ponder:

  1. Be consistent with white space usage. Perl::Tidy is great for cleaning up code
  2. Don't use & to call subs, it doesn't do what you think. Use subName(...) instead.
  3. Don't use prototypes. They really don't do what you think.
  4. Use early exits to clean up error handling logic and remove levels of indentation.

I'd put the code into a subroutine to make testing easier. I'd also simplify the time matching regex and perform the range checking outside the regex. Something like:

#!/usr/bin/perl use strict; use warnings; for my $test ('', '234-10:23', '0:0:1', '0:1', '24:60') { my $fail = parseTime($test); print "Failed parsing '$test': $fail\n" if $fail; } sub parseTime { my ($inputTxt) = @_; return "Bad input value" if !defined $inputTxt || !length $inputTx +t; $inputTxt =~ s/^\s+|\s+$//g; return "Bad time string" if $inputTxt !~ /^(?:(\d{1,3})-)?(?:(\d+):)?(\d+):(\d+)/; my ($days, $hours, $mins, $secs) = map {$_ || 0} $1, $2, $3, $4; return "Bad day count: $days" if $days > 366 && $days >= 0; return "Bad hour value: $hours" if $hours > 23 && $hours >= 0; return "Bad minutes value: $mins" if $mins > 59 && $mins >= 0; return "Bad seconds value: $secs" if $secs > 59 && $secs >= 0; my $totalSecs = ($days * 24 * 3600) + ($hours * 3600) + ($mins * 60) + $secs; print "$inputTxt - ${totalSecs}s\n"; return; }

Prints:

Failed parsing '': Bad input value 234-10:23 - 20218223s 0:0:1 - 1s 0:1 - 1s Failed parsing '24:60': Bad seconds value: 60
True laziness is hard work

In reply to Re: A regular expression that will work for (hh:)mm:ss [converting clock time to seconds] by GrandFather
in thread A regular expression that will work for (hh:)mm:ss [converting clock time to seconds] by aware74

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.