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.
| [reply] [d/l] |
|
| [reply] |
|
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
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
<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>
| [reply] |
|
<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>
| [reply] |
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 | [reply] |
|
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.
| [reply] |
|
Thanks for the info! It is a little overkill for what I was doing but I will look into the suggested book! :)
Thanks again!
| [reply] |
Re: control-d out of loop?
by Anonymous Monk on Dec 03, 2001 at 10:39 UTC
|
you have to use signal %SIG
| [reply] |
|
Thanks but I don't think there is a signal for a control-d. At least what a kill -l shows.....
| [reply] |