Term::ReadLine behaviour on a blank line
Anonymous Monk
created: 2006-04-03 11:50:42
Monks, I'm trying to write a snippet of code that gets the user to enter a filename until they enter one that's present. Here's the code:
$|++;
use Term::ReadLine;
my $term = new Term::ReadLine 'test';
while ($filename = $term->readline('>')) {
        print "checking for $filename...\n";
        last if (-e $filename);
        print "file not found, try again\n";
}
print "filename is ~~$filename~~";

This works fine except when I just press enter at the '>' prompt; at which point the program exits, without even getting to "checking for...". The same thing happens in the middle of my program. Obviously, I don't want this, as I then end up trying to open a file that isn't there. Is this behaviour intentional? Any ideas appreciated.
Re: Term::ReadLine behaviour on a blank line
created: 2006-04-03 12:15:31
I'm guessing readline returns "" when you just press enter. "" evaluates to false. Try
while (defined($filename = $term->readline('>'))) {
Re^2: Term::ReadLine behaviour on a blank line
created: 2006-04-03 12:41:40
Thanks, that nailed it.
Re: Term::ReadLine behaviour on a blank line
created: 2006-04-03 12:21:22
Comment:
last unless defined $filename;	# this doesn't work
last if $filename eq '';	# this doesn't work, either

perlmonks.org content © perlmonks.org and Anonymous Monk, ikegami

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03