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 ($;$) {
        my($descr, $default) = @_;
        croak "No description specified for input"
            unless $descr;
        $default ||= '';
        my $result = $term->readline("$descr [$default]: ") || $default;
        return $result;
    }
}

my(%input, $default);

$default = sub {
foreach ("$ENV{HOME}/public_html", "$ENV{HOME}/html", "$ENV{HOME}/www") {
return $_ if -e && -d _;
}
};


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} = $_;
}


now, eventually what I'm aiming at is a way to sort of batch-process thru a bunch of queries, but I'll worry about that part later. Just trying to get this small bit working how I'd expect it to.

What's wrong with the way outlined above is that $_ isn't being set, so the DEBUG print gets an undefined value.

if I use
while (local $_ = get_input ... ) {
(or some variant of
while (my $var = get_input...) {
)
then the conditionals don't get checked if the user presses return on an empty default.


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?

--
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