Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Can't exit, except when I press ^C?

by TalsiOrah (Initiate)
on Feb 02, 2012 at 05:15 UTC ( #951356=perlquestion: print w/ replies, xml ) Need Help??
TalsiOrah has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks ^_^ This is my very first post. I honestly did not think I would need to, but after much searching, I am left with no choice because it's making me think about switching to C. (I know I said it... Lol!) Well first off I am trying to create a very large AI bot that will be embedded into my next computer's ArchLinux. It will have voice recognition, and say some random quotes through a generated voice at random times to simulate actual intelligence. I know I think I'm crazy too. I most definitely fail, but it will be a very good learning experience. So here is my start off code taken from http://mind.sourceforge.net/perl.html
#!/usr/bin/perl -w # sub security; sub sensorium; sub emotion; sub think; sub volition; sub motorium; while (1) { security(); sensorium(); emotion(); think(); volition(); motorium(); } sub security { # security } sub sensorium { # sensorium print "Press ENTER or ESCAPE key: "; $_ = <STDIN>; exit if (/^\027/); } sub emotion { # emotion } sub think { # think } sub volition { # volition } sub motorium { # motorium } #---
The problem is that I cannot exit the loop at all unless I press <CONTROL>-C (^C). I thought that it might be trying to terminate just the sensory stub, and then looping back and executing it again. So I moved the "exit if" up into the actual while loop out of frustration, but to no avail. This is a screenie of the output: http://is100.imagesocket.com/images/2012/02/02/2425592-4k99.png I pressed enter twice, then I pressed the ESC key then enter, then finally ^C. That is the right hex for ESC correct? I was really hoping that I could find a way to close/exit the script with having to type into the terminal also. But nothing seems to be working right now. It should be such a simplistic script. I really cannot figure it out. I even tried chomping, and chopping. Help would very much be appreciated. I don't want to end my project before I haven't even started. I am merely a beginner, Thanks for your time ^_^

Comment on Can't exit, except when I press ^C?
Download Code
Re: Can't exit, except when I press ^C?
by Anonymous Monk on Feb 02, 2012 at 05:22 UTC
      I'm running Linux. And the readline resulted in the same problem. Thanks though! ^_^
Re: Can't exit, except when I press ^C?
by roboticus (Chancellor) on Feb 02, 2012 at 10:51 UTC

    TalsiOrah:

    The ESC character is chr(27), but you're looking for chr(23). The reason? /^\027/ is looking for octal 27 which is decimal 23. You should use /^\033/ which will give you what you're looking for.

    Update: I (briefly) looked around, but didn't find a link to perl documentation on escaping numbers in strings. In an expression, prefix of '0' on a number tells perl to use octal, and a prefix of '0x' tells it to use hexadecimal. In a string, it's similar, you use a prefix of '\0' for octal, and '\x' for hexadecimal.

    Update 2: Perlbotics provided a link perlnumber to what I was looking for, and corrected me on the prefix used for hexadecimal in strings. I clarified the previous update accordingly. The way I worded it previously would have had people using '\0x4C' expecting an 'M', but they'd get chr(0) followed by 'x4C'.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks!! That worked!! Am I doing the looping correctly? Like I said, I'm extremely newbie haha. Is there a way to make a global like variable to exit the script? Like read input from outside the terminal? Oh I'm on Linux by the way.
Re: Can't exit, except when I press ^C?
by Anonymous Monk on Feb 02, 2012 at 15:13 UTC
    You've received two good answers here.

    Your code reads a line of text. Then you test to see if the 1st character on that line is a <ctrl-W>.

    Perhaps if you fixed the prompt?

      Thanks, I was using octal and not hex. Is there a way to make it read input from outside the script so that I could press <esc> anytime and it would close? Sorry I know I'm like super noobie. Thanks though for your time ^_^
        As the other AM says, look at the Term namespace.

        I tried to find a core solution to you and found  getc, but it's even its documentation points you to the CPAN module Term::ReadKey.

        Looks like now is as good a time as any to learn how to use CPAN (or PPM).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://951356]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2015-07-05 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls