in reply to Re: Using number "0" as an input argument
in thread Using number "0" as an input argument

Thanks for this. I just noticed I put "||" in my post instead of "//" which is what I have in my script. I know how they differ but thanks for pointing it out.

I have now figured out it was a combination of using the // operator and also chomp. The code now looks something like:

my $in1 = $ARGV[0]; chomp(my $in2 = $ARGV[1] // ''); if (defined $in1) { $in1 =~ s/\s+//g; # Remove all whitespace instead of chomp if ($in1 =~ /\D+/) { print "Input invalid. Only numbers are allowed\n"; } } else { print "No valid input given\n"; }

Replies are listed 'Best First'.
Re^3: Using number "0" as an input argument
by RonW (Parson) on Sep 22, 2015 at 17:37 UTC
    $in1 =~ s/\s+//g;

    Although your comment suggests you are aware, I will still point out that s/\s+//g will remove all spaces, including spaces between digits/other characters, not just leading and trailing spaces.

    While unlikely to be a problem with your program, it is still possible to feed an unexpected value:

    yourprog "1 2"

    would result in $in1 being 12 and $in2 being empty

Re^3: Using number "0" as an input argument
by SuicideJunkie (Vicar) on Sep 22, 2015 at 14:54 UTC

    This is why you should copy/paste code instead of retyping it in your post.

    Both more accurate and faster as well! :)