Here's an alternative idea to simplify your code: instead of making the user type "q" to quit, they could either use "^C" to halt execution when they're done, or use the appropriate "eof" control character ("^Z" on windows, "^D" on unix), which will cause perl to "close" STDIN.

With that, the basic structure of the application could be done like this -- and I'll add an "extra feature":

use strict; $| = 1; # turn off output buffering if ( @ARGV and $ARGV[0] =~ /^\d+$/ and ! -f $ARGV[0] ) { show_sec( @ARGV ); } else { while (<>) # read a line from STDIN (or from a named file) { chomp; show_sec( $_ ); } } sub show_sec { for ( @_ ) { if (/^\d+$/) { my $sec = $_ * 86400; my $s = ( $_ == 1 ) ? '':'s'; print "[$_] day$s = [$sec] seconds\n"; } else { warn "$_ is not a positive integer; I'll ignore that.\n"; } } }
Let's suppose I call this script "d2s" (for "days to seconds"). If it's written like that, I can use it in any of the following ways:

In all cases, each input string is always handled the same way: if it is a positive integer it is converted to seconds, otherwise it is ignored, with a message that says so. (0 days is converted to 0 seconds, which seems okay to me.)

The  while (<>) loop provides all that flexibility. Also, when you use "warn" instead of "print" to deliver error messages about unusable input, those messages go to STDERR, so you can then redirect valid output (i.e. STDOUT) to a file to save the results for later re-use (e.g. "d2s > my_d2s.txt") -- there are many reasons for having the "good output" and "bad output" going to different file handles.

(Special note: the way @ARGV is handled there, if you have a data file called "12345" and give that name as the first arg, the script will read from that file, rather than using "12345" as a number to be converted. Basically, if the first arg is a number and is not a file, then all args are expected to be numbers to convert, otherwise they're all treated as files to be read.)


In reply to Re: String/Numeric Manipulation by graff
in thread String/Numeric Manipulation by Ms. T.

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.