in reply to Read two values from input

use strict; use warnings; my $inp = getInput('Enter two values'); $inp = [split /[\s,]+/, $inp]; print "Values entered are: $inp->[0], $inp->[1]\n"; sub getInput { my $inp; while (1) { print "$_[0]: "; chomp($inp = <STDIN>); return $inp if $inp; } }

Replies are listed 'Best First'.
Re^2: Read two values from input
by ww (Archbishop) on Nov 27, 2011 at 16:26 UTC
    TJP: Your split is highly commendable -- because you considered the possibility that a user, entering two numbers on a single line might separate them with a space; a comma; or both. ++.

    However, you may have heard the (boastful) observation "Perl makes hard things, easy." It seems to me you've done the reverse (or at least, unnecessarily complicated the code) by introducing the sub.

    But how about keeping the code simple:

    perl -e "my $inp = <>;@input = split /[\s,]+/, $inp; print \"$input[0] : $input[1]\";"
      Because the object is to make easily-readable and reusable code, not code this in the smallest number of characters? The getInput sub is a standard one I include in almost all of my command-line scripts, normally it would just be included via:

      use Library::General;

      Pasting it into the code was just for illustration.