Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

control-d out of loop?

by Marza (Vicar)
on Dec 03, 2001 at 07:20 UTC ( [id://129026]=perlquestion: print w/replies, xml ) Need Help??

Marza has asked for the wisdom of the Perl Monks concerning the following question:

Okay. This should be easy but I am just not getting it.

I have a routine with a menu that calls sub routines.
The subroutine is supposed to take input, do its thing, display, results, take input.

This process is to continue till a control-d is entered.

What i basically have is...

while (1) { chomp($picks = <STDIN>); if ($picks eq /\cD/ { last; }
Now the if does not work at all. Is there a way to check for a control-d being entered?

Thank you!

Edit kudra, 2001-12-03 Added code tags

Replies are listed 'Best First'.
Re: control-d out of loop?
by chipmunk (Parson) on Dec 03, 2001 at 07:29 UTC
    When you type a Control-D, it simply terminates the input; the Control-D character does not get passed in as input. Anyway, if you rewrite your loop, you don't need to check for Control-D:
    while($picks = <STDIN>) { chomp($picks); # do stuff }
    <STDIN> will return undef when there is no more input on STDIN.
      Amen. This seems to be a hangup for a lot of people. As I once said here:
      EOF (end of file) is not a thing. It's a condition, a state of being.

      In the same way that "dead" is not a thing. You cannot have a dead, you cannot hold a dead. You cannot give away a dead, and you can't write a dead to a file.

      Of course, if the tty is in raw mode we can nitpick this to death (hee!) but for the most part it holds.

      Roger that.

      In even more other words, you don't actually get a Ctrl-D character (the OS eats it); instead you will see EOF, which is a testable condition embodied by the <> operator returning undef. Here's the same example again using the implicit $_ variable:

      while (<>) { chomp; #do stuff (with $_) }

      dmm

      
      You can give a man a fish and feed him for a day ...
      Or, you can teach him to fish and feed him for a lifetime
      
      And for the uninitiated, remember that ^D does not equal EOF on all operating systems. With Windows, for example, you'd have to use ^Z for the same effect.
      <body>

      Update: There is a way to do this.

      However, it is a VI trick.

      To input a control-d, you press control-v then a control-d.
      This will put a control-d char in variable that you can use to trap for a control-d being pressed.

      This code does not have the special char. It is just an example. If you want to try it replace 
      the ^D with the sequence I described...

      $control_d = '^D';

      while (chomp($char = <STDIN>)) {
              print "control_d $control_d char = $char\n";
              if ($char eq '^D) { # control D
                  print "gotcha\n";
                  last;
              }
      }

       

      </body>
      <body>

      Thank you for helping me get my thinking straight.  I was going down a wierd path.

      There is one way to do it!

      while (1) {

          print "input your stuff : ";

          last unless defined(($picks = <STDIN>));
          chomp $picks;

          #stuff

      }

      This does allow you to do a control-d and return to the master loop while the CR will just

      error and return to the prompt!

      Thanks again!

      </body>
Re: control-d out of loop?
by Necos (Friar) on Dec 03, 2001 at 13:54 UTC
    If you are using Perl/Tk, there are built in functions that will allow you to specify keyboard events. If you wish to look further into Tk, get the Perl/Tk book from O'Reilly. Yes, I know, most of the time, GUI is bad. In this case however, A few text boxes in a GUI with keyboard events might be what you were looking for.

    Necos
      I believe that Necos is talking about binding:
      $widget->bind(tag,sequence,callback);

      Further more it might be much cheaper for you to take a
      look at the Tk::Bind reference that comes with the
      Module to see if it fits your situation.
        Thanks for the info! It is a little overkill for what I was doing but I will look into the suggested book! :) Thanks again!
Re: control-d out of loop?
by Anonymous Monk on Dec 03, 2001 at 10:39 UTC
    you have to use signal %SIG
      Thanks but I don't think there is a signal for a control-d. At least what a kill -l shows.....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://129026]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (9)
As of 2024-03-28 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found