in reply to Re: Stdin for just numbers.
in thread Stdin for just numbers.

A module is definitely the way to go. I would prefer IO::Prompt::Hooked. It works on all operating systems and provides validation and retry.
Bill

Replies are listed 'Best First'.
Re^3: Stdin for just numbers.
by AppleFritter (Vicar) on Mar 16, 2015 at 17:10 UTC

    You could also use IO::Prompter, which I only recently learned about.

    #!/usr/bin/perl use strict; use warnings; use feature qw/say/; use IO::Prompter; my %responses = (); my @prompts = ( ["startamount", "the starting amount", ""], ["startage", "your current age", ""], ["endage", "the age you want to retire", sub { $_ >= $r +esponses{'startage'} } ], ["yearlydeposit", "amount deposited per year", ""], ["annualinterest", "the Annual interest rate", ""], ["expectedmoney", "Expected Retirement Money", ""], ); foreach my $prompt (@prompts) { $responses{$prompt->[0]} = prompt "Enter $prompt->[1]:", -num => $ +prompt->[2] } foreach (sort keys %responses) { say "$_: $responses{$_}"; }

    There's probably a better way of specifying empty constraints -- though the OP will likely some constraints that goes beyond "it's a number" imposed on most of the input.

Re^3: Stdin for just numbers.
by davido (Cardinal) on Mar 16, 2015 at 20:02 UTC

    ++ for recommending my module (IO::Prompt::Hooked). I actually implemeneted a solution to the OP's question using IO::Prompt::Hooked last night, and it looked (in my opinion) great. ...but it became late, and I was too tired to submit a post around it. :)

    Anyway, this is exactly the sort of thing that IO::Prompt::Hooked aims to simplify.


    Dave