Philipp Traeder wrote:
On Tuesday 08 February 2005 18:57, Scott R. Godin wrote:

This is my first time attempting to use Term::ReadLine to get input from
the commandline while a script is running. I grok the basics of it, but
am stumbling over the implementation of it. Let me illustrate:

READLINE:
{
# closure, so I don't have to either
#   -  call Term::ReadLine->new() every time thru the sub, or
#   -  pass the object to the sub every time thru
# this way it gets called _once_, period, and I don't have to think
# about it later
    my $term = Term::ReadLine->new();

sub get_input ($;$) {

[..]

    }
}


[..]

while ( get_input "What is the full path to the directory containing\
your website .html files", $default->() ) {
    print && next unless $_; #DEBUG
    print "nonexistent: $_" && next unless -d ;
    print "not readable: $_", next unless -r;
    die  "directory not writable by this process. check permissions on:
$_" unless -w;
    $input{htdocs} = $_;
}


[..]

My brain seems frozen today. How do I ensure that the input loop will
continue if the value is not defined, and test the values if they are ?
I'm guessing a continue block but I can't seem to wrap my brain around
the problem today. I should KNOW this, dammit! It's killing me that I
can't figure it out, and I'm sure it'll be obvious in retrospect.

should I even be using 'while' here?


I would say 'no' to this one ;-)
Why do you need a loop here? The way you've written your get_input() method, you'll always get a return value - either the one from the user, or a default value. Even if you implement a query without return value, your directory checks will catch it because "-d undef" returns 0, so the user would get the message "nonexistent: " if he enters no value for a question without default value, which is more or less correc.t


Maybe I didn't get the point, but I would probably write it like this:
[...]
$term = Term::ReadLine->new();

$_ = get_input "What is the full path to the directory containing your \ website .html files", $default->();
die "nonexistent: $_" unless -d;
die "not readable: $_" unless -r;
die "directory not writable by this process. check permissions on: $_" unless -w;
print "ok - got $_\n";
$input{htdocs} = $_;

and that's sorta fine except I'll have other different circumstances as well..


there will be instances where I cannot provide a default (Guess I should have made this clearer earlier... *smacks forehead*), such as username, password, etc.

and, on the off chance that my 'default' sub turns up returning undef (I'm unable to guess what their correct dir location might be), there won't be a default, in which case if they mistakenly press return, I get an undef value, which the sub converts to '' ..

that's all fine and dandy but I don't want to make the user re-run the script several times whenever he supplies bad input. I would prefer that it ask the question again until it gets a value it can use. (perhaps providing a 'sorry I didn't understand that.." message if they provide a null value, or the default doesn't exist for some reason.

--
Scott R. Godin
Laughing Dragon Services
www.webdragon.net

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to