ReadKey/ ReadLine query

Discussion in 'Perl Misc' started by V S Rawat, Jul 4, 2006.

  1. V S Rawat

    V S Rawat Guest

    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
     
    V S Rawat, Jul 4, 2006
    #1
    1. Advertising

  2. V S Rawat

    V S Rawat Guest

    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
     
    V S Rawat, Jul 4, 2006
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Jul 4, 2006
    #3
  4. V S Rawat

    -berlin.de Guest

    V S Rawat <> 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
     
    -berlin.de, Jul 4, 2006
    #4
  5. 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
     
    Jürgen Exner, Jul 4, 2006
    #5
  6. V S Rawat

    V S Rawat Guest

    -berlin.de wrote:

    > V S Rawat <> 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
     
    V S Rawat, Jul 4, 2006
    #6
  7. V S Rawat

    V S Rawat Guest

    V S Rawat wrote:

    > -berlin.de wrote:
    >
    > > V S Rawat <> 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
     
    V S Rawat, Jul 4, 2006
    #7
  8. V S Rawat

    V S Rawat Guest

    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
     
    V S Rawat, Jul 4, 2006
    #8
  9. V S Rawat

    DJ Stunks Guest

    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::prompt rather than Term::ReadKey.

    -jp
     
    DJ Stunks, Jul 4, 2006
    #9
  10. V S Rawat

    Joe Smith Guest

    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
     
    Joe Smith, Jul 5, 2006
    #10
  11. V S Rawat

    -berlin.de Guest

    V S Rawat <> wrote in comp.lang.perl.misc:
    > V S Rawat wrote:
    > > -berlin.de wrote:
    > > > V S Rawat <> 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?


    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.

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


    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
     
    -berlin.de, Jul 5, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dustan

    Cross-Platform ReadKey

    Dustan, Nov 21, 2005, in forum: Python
    Replies:
    0
    Views:
    318
    Dustan
    Nov 21, 2005
  2. Dustan

    Modifying Unix ReadKey Code

    Dustan, Nov 25, 2005, in forum: Python
    Replies:
    0
    Views:
    416
    Dustan
    Nov 25, 2005
  3. Dustan
    Replies:
    0
    Views:
    354
    Dustan
    Nov 27, 2005
  4. lucas

    extra printout with Term::ReadKey

    lucas, Jan 8, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    128
    lucas
    Jan 9, 2004
  5. Martin Mohr

    Term::ReadKey not working on one linux box

    Martin Mohr, Mar 21, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    104
    Martin Mohr
    Mar 26, 2006
Loading...

Share This Page