ReadKey/ ReadLine query

V

V S Rawat

1. I am currently doing it like this:

--------------
do {
print "Font Name (1: Arjun, 2: Shree709, 3: Shusha, 0: Exit)? \n";
use Term::ReadKey;
ReadMode 4; # Turn off controls keys
while (not defined ($C1_map_opt = ReadKey(-1))) { # No key yet
}
if ( $Debug_flag == 1 ) { # $Debug_flag has already been defined
print "Get key $C1_map_opt\n";
}
ReadMode 0; # Reset tty mode before exiting
} until ( $C1_map_opt >= 0 && $C1_map_opt <= 3 );
--------------

Is there any method of giving a prompt (e.g. "Font Name (1: Arjun, 2:
Shree709, 3: Shusha, 0: Exit)? \n") with ReadKey? I mean, the way we
can do with ReadLine as in:
---------
my $prompt = "Input file name (0: Exit)? ";
$term->readline($prompt)
-------------------

That would avoid me writing a separate line some three lines before the
actual readkey command.

---------------------------------------------

2. The following is not reaching "entered file name: " line, nor is
exiting at 0.
----------------
use Term::ReadLine;
my $term = new Term::ReadLine 'Simple Perl calc';
my $prompt = "Input file name (0: Exit)? ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
$InFile = eval($_);
warn $@ if $@;
print $OUT $InFile, "\n" unless $@;
$term->addhistory($_) if /\S/;
}

if ($InFile == "0") {
exit;
}
print "entered file name: ", $InFile, "\n";
 
J

John W. Krahn

V said:
Oops! It should be
if ($InFile eq "0") {
because it is a string.

No, it should be:

if ($InFile == 0) {
exit;
}

because 0 is a number even when it is written as "0".


John
 
A

anno4000

V S Rawat said:
1. I am currently doing it like this:

--------------
do {
print "Font Name (1: Arjun, 2: Shree709, 3: Shusha, 0: Exit)? \n";
use Term::ReadKey;
ReadMode 4; # Turn off controls keys
while (not defined ($C1_map_opt = ReadKey(-1))) { # No key yet
}
if ( $Debug_flag == 1 ) { # $Debug_flag has already been defined
print "Get key $C1_map_opt\n";
}
ReadMode 0; # Reset tty mode before exiting
} until ( $C1_map_opt >= 0 && $C1_map_opt <= 3 );
--------------

Is there any method of giving a prompt (e.g. "Font Name (1: Arjun, 2:
Shree709, 3: Shusha, 0: Exit)? \n") with ReadKey? I mean, the way we
can do with ReadLine as in:

There is only one way to find out: read the doc. Why should anyone
do that for you?
---------------------------------------------

2. The following is not reaching "entered file name: " line, nor is
exiting at 0.
----------------
use Term::ReadLine;
my $term = new Term::ReadLine 'Simple Perl calc';
my $prompt = "Input file name (0: Exit)? ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
$InFile = eval($_);
warn $@ if $@;
print $OUT $InFile, "\n" unless $@;
$term->addhistory($_) if /\S/;
}

if ($InFile == "0") {
exit;
}
print "entered file name: ", $InFile, "\n";

Your while loop won't end except when you enter an EOF (^D under Unix).
So the subsequent statements are never reached.

Put the query for the loop end inside the loop.

Anno
 
J

Jürgen Exner

John said:
No, it should be:

if ($InFile == 0) {
exit;
}

because 0 is a number even when it is written as "0".

But what about "00" or "000"? If those are considered as equal, then yes,
the OP should use numerical comparison. If they are different then the OP
should use textual comparison.

jue
 
V

V S Rawat

Your while loop won't end except when you enter an EOF (^D under
Unix). So the subsequent statements are never reached.

Put the query for the loop end inside the loop.

Anno

Hmm! not becoming clear to me.

Does the line "while ( defined ($_ = $term->readline($prompt)) ) {" has
some problem? I have copied it as such from the doc.

file:///C:/Program%20Files/Perl/html/lib/Term/ReadLine.html
---------------------start
SYNOPSIS

use Term::ReadLine;
my $term = new Term::ReadLine 'Simple Perl calc';
my $prompt = "Enter your arithmetic expression: ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
my $res = eval($_);
warn $@ if $@;
print $OUT $res, "\n" unless $@;
$term->addhistory($_) if /\S/;
}
---------------------end

or, is it general behaviour that a ReadLine terminates only with an
EOF, and not with a Enter key?

thanks.
 
V

V S Rawat

V said:
Hmm! not becoming clear to me.

Does the line "while ( defined ($_ = $term->readline($prompt)) ) {"
has some problem? I have copied it as such from the doc.

file:///C:/Program%20Files/Perl/html/lib/Term/ReadLine.html
---------------------start
SYNOPSIS

use Term::ReadLine;
my $term = new Term::ReadLine 'Simple Perl calc';
my $prompt = "Enter your arithmetic expression: ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
my $res = eval($_);
warn $@ if $@;
print $OUT $res, "\n" unless $@;
$term->addhistory($_) if /\S/;
}
---------------------end

or, is it general behaviour that a ReadLine terminates only with an
EOF, and not with a Enter key?

thanks.

Adding:

doc says:
------------start
readline

gets an input line, possibly with actual readline support. Trailing
newline is removed. Returns undef on EOF.
----------end

wouldn't that mean that EOF should not be used.
 
V

V S Rawat

V said:
2. The following is not reaching "entered file name: " line, nor is
exiting at 0.
----------------
use Term::ReadLine;
my $term = new Term::ReadLine 'Simple Perl calc';
my $prompt = "Input file name (0: Exit)? ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
$InFile = eval($_);
warn $@ if $@;
print $OUT $InFile, "\n" unless $@;
$term->addhistory($_) if /\S/;
}

if ($InFile == "0") {
exit;
}
print "entered file name: ", $InFile, "\n";
----------------

I also noticed that when I enter some filename like "temp.txt", it is
diplaying "temptxt" after removing ".".

Also, ending the input with ^Z (EOF for command mode in windows)
doesn't help.
 
D

DJ Stunks

V said:
1. I am currently doing it like this:

--------------
do {
print "Font Name (1: Arjun, 2: Shree709, 3: Shusha, 0: Exit)? \n";
use Term::ReadKey;
ReadMode 4; # Turn off controls keys
while (not defined ($C1_map_opt = ReadKey(-1))) { # No key yet
}
if ( $Debug_flag == 1 ) { # $Debug_flag has already been defined
print "Get key $C1_map_opt\n";
}
ReadMode 0; # Reset tty mode before exiting
} until ( $C1_map_opt >= 0 && $C1_map_opt <= 3 );
--------------

Is there any method of giving a prompt (e.g. "Font Name (1: Arjun, 2:
Shree709, 3: Shusha, 0: Exit)? \n") with ReadKey? I mean, the way we
can do with ReadLine as in:

use IO::prompt rather than Term::ReadKey.

-jp
 
J

Joe Smith

Why in the heck are you doing that? If you enter "test.txt", then
eval("test.txt") is the same as {"test"."txt"} which concatenates
two barewords together. Likewise, entering "2+3*4" results in 14.

Get rid of that eval().

unless (-e $_) {
print "Error: $_ does not exist\n";
next;
}
if (-f $_) {
$InFile = $_;
} else {
print "Error: $_ exists but is not a file\n";
next;
}

-Joe
 
A

anno4000

As the name implies, readline terminates when an end-of-line is
recognized. Pressing the Enter key does that. Then it returns
the line it has seen, which is *never* an undefined value. So
your loop goes on and on, never reaching the place where you ask
about the value of $InFile.
Adding:

doc says:
------------start
readline

gets an input line, possibly with actual readline support. Trailing
newline is removed. Returns undef on EOF.
----------end

wouldn't that mean that EOF should not be used.

How do you deduce that? Entering EOF is the only way make readline()
return undef, and that is the only way to make your while-loop
terminate.

As noted, your must query the value of $InFile inside the loop, not
after it.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top