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:

##################################################################
#!/usr/bin/perl -w

use strict;
use Term::ReadLine;

my(%input, $default);

$default = sub { .. };

# declaring the console object globally
my $term;

sub get_input ($;$) {
    my($descr, $default) = @_;
    print "No description specified for input"
        unless $descr;
    $default ||= '';
    my $result = $term->readline("$descr [$default]: ") || $default;
    return $result;
}

### main

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

##################################################################

If you want to process a batch query, you can always build a while loop around 
it like this:

my @queries = (
  {'name' => 'htdocs',
   'description' => 'Full path to your htdocs',
   'default' => $default
  },
  {'name' => 'home',
   'description' => 'Your home directory, if you please:',
   'default' => sub { return $ENV{HOME}; }
  }
);

foreach my $query_ref (@queries) {
  $_ = get_input(
              $query_ref -> {'description'},
              $query_ref -> {'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 $_ for $query_ref->{'name'}\n";
  $input{$query_ref->{'name'}} = $_;
}

HTH,

Philipp

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