Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > ReadKey/ ReadLine query

Reply
Thread Tools

ReadKey/ ReadLine query

 
 
V S Rawat
Guest
Posts: n/a
 
      07-04-2006
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";
----------------

What did I do wrongly?

TIA.
--
Rawat
 
Reply With Quote
 
 
 
 
V S Rawat
Guest
Posts: n/a
 
      07-04-2006
V S Rawat wrote:

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


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

But, it is not working with eq either.

--
Rawat
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      07-04-2006
V S Rawat wrote:
> V S Rawat wrote:
>
>> if ($InFile == "0") {
>> exit;
>> }

>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
anno4000@zrz.tu-berlin.de
Guest
Posts: n/a
 
      07-04-2006
V S Rawat <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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.


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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-04-2006
John W. Krahn wrote:
> V S Rawat wrote:
>> V S Rawat wrote:
>>
>>> if ($InFile == "0") {
>>> exit;
>>> }

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


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


 
Reply With Quote
 
V S Rawat
Guest
Posts: n/a
 
      07-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:

> V S Rawat <(E-Mail Removed)> wrote in comp.lang.perl.misc:


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


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.
--
Rawat
 
Reply With Quote
 
V S Rawat
Guest
Posts: n/a
 
      07-04-2006
V S Rawat wrote:

> (E-Mail Removed)-berlin.de wrote:
>
> > V S Rawat <(E-Mail Removed)> wrote in comp.lang.perl.misc:

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

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

--
Rawat
 
Reply With Quote
 
V S Rawat
Guest
Posts: n/a
 
      07-04-2006
V S Rawat wrote:

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

--
Rawat
 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      07-04-2006
V S Rawat wrote:
> 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)


use IO:rompt rather than Term::ReadKey.

-jp

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      07-05-2006
V S Rawat wrote:

>> while ( defined ($_ = $term->readline($prompt)) ) {
>> $InFile = eval($_);


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling python 3.1.2 with local readline fails to get readline - help! gavino Python 4 09-20-2010 05:17 AM
Getting application ReadLine and Perl debugger ReadLine to cooperate Andrew DeFaria Perl Misc 1 01-30-2008 11:46 PM
Readline::readline() blocking all other threads Jean-Michel Ruby 0 12-22-2007 01:00 AM
Trying to query the Address table data of AdventureWorks database from Query Analyzer - need help! Learner ASP .Net 1 01-30-2006 08:58 PM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM



Advertisments