> in the following code I have tried == and eq.  I'm thinking I should
> chomp the input before checking if it's
> an n or q (which is why I tried to kludge it with n\n and q\n, but
> I'm not sure how to slip the chomp in without affecting the case
> where I really need the newline.

Use eq instead of ==. Your code is logically correct, but you have to
realize that each time you test against <STDIN>, you're actually reading
another line off of STDIN.

To explain:

> sub page_control {
> print "would you like to see the next ten lines of $file_list[$openme]?";
> if (<STDIN> == "\n") {

This reads the first line from STDIN and (although == should be replaced
with eq) compares it to "\n" and throws away the line.

> & ten_line_file_reader;
> }
> elsif (<STDIN> == "n\n") {

This reads the _next_ line from STDIN (which is nothing, because the user
hasn't done anything after the initial input), compares it to "n\n", and
throws away the line.  So by this time, you no longer have the original
input from the user, just an empty string.

> & open_current_directory;
> }
> elsif (<STDIN> == "q\n") {

And this reads the third line from STDIN, which is by now an empty string,
compares it to "q\n", and throws it away.

> exit;
> }
> }

In this case, it's probably best to save chomp()ed input to a variable, and
then do an if-elsif-else statement:

sub page_control {
    print "What would you like to do?\n";
    print "  <return>   Read next 10 lines of current file\n";
    print "  n          Read current directory\n";
    print "  q          Exit program\n";
    chomp(my $answer = <STDIN>);
    if($answer eq "n") {
        open_current_directory();
    } elsif($answer eq "q") {
        exit;
    } else {
        ten_line_file_reader();
    }
}

The one caveat to doing it this way is that if the user enters anything
other than 'n' or 'q', s?he'll get the next ten lines of the file.

> I will modify the prompt later, and any other tests later too (like
> case insensitivity, or testing for garbage input)

You'll probably first want to define 'n', 'q', and <return>, so the user
knows what keys to hit at the prompt.

Hope that helps.

Regards,
David

Reply via email to