Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

trapping ctrl-d

by hotshot (Prior)
on Dec 31, 2001 at 21:40 UTC ( [id://135395]=perlquestion: print w/replies, xml ) Need Help??

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

hi again !

Is there a way on earth to trap the CTRL-D combination in my script. It's not a signal so I can't trap it with my signal handlers. it's supposed to send 'end of file' to the input and by that ending an interactive input from the keyboard.
anyone to the rescue?

Hotshot

Replies are listed 'Best First'.
Re: trapping ctrl-d
by jlongino (Parson) on Dec 31, 2001 at 22:22 UTC
    clintp gave you perfectly valid answers in your last post. I recommend the Term::ReadKey option as it is probably more portable, either can also be used to detect/intercept pesky things like Ctrl-C as well.

    For examples of using Term::ReadKey, check out an otherwise not very useful module I wrote: Term::TermKeys.

    HTH,

    --Jim

Re: trapping ctrl-d
by grep (Monsignor) on Dec 31, 2001 at 21:44 UTC
    <stong>UNTESTED</stong>
    try:

    binmode(STDIN)
    UPDATE: I have tested this and it DOES NOT WORK. I am looking at another solution.

    UPDATE2:
    OK... I apparently forgot the wonderful idiom Know thy FAQ this is a FAQ.
    Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)? Because some stdio's set error and eof flags that need clearing. The POSIX module defines clearerr() that you can use. That is the technically correct way to do it. Here are some less reliable workarounds: 1 Try keeping around the seekpointer and go there, like this: $where = tell(LOG); seek(LOG, $where, 0); 2 If that doesn't work, try seeking to a different part of the file and then back. 3 If that doesn't work, try seeking to a different part of the file, reading something, and then seeking back. 4 If that doesn't work, give up on your stdio package and use sysread.

    you can look up this FAQ  perldoc -q EOF

    grep
    grep> cd pub grep> more beer
Re: trapping ctrl-d
by count0 (Friar) on Dec 31, 2001 at 21:46 UTC
    You don't really need to stop the user from sending an EOF, you just need to keep prompting for input until you get what you want:
    until ($condition) { # Keep reading from STDIN. # Store and/or process the input. }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found