Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: control-d out of loop?

by chipmunk (Parson)
on Dec 03, 2001 at 07:29 UTC ( #129028=note: print w/replies, xml ) Need Help??


in reply to control-d out of loop?

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.

Replies are listed 'Best First'.
EOF confusion (was: control-d out of loop?)
by clintp (Curate) on Dec 03, 2001 at 20:19 UTC
    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.
Re(2): control-d out of loop?
by dmmiller2k (Chaplain) on Dec 03, 2001 at 23:32 UTC

    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
    
Re: Re: control-d out of loop?
by Fastolfe (Vicar) on Dec 04, 2001 at 02:57 UTC
    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.
There is a way!
by Marza (Vicar) on Dec 05, 2001 at 04:49 UTC
    <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>
Re: Re: control-d out of loop?
by Marza (Vicar) on Dec 04, 2001 at 23:03 UTC
    <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>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129028]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2020-01-23 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?