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

I'm writing a script which will run under multiple term types - but in some they seem to ignore the default "working" backspace and delete when doing an "$a = <>;". Is there a quick'n'easy way of making this work? Preferably without having to use extra CPAN modules... Billy.

Replies are listed 'Best First'.
Re: Backspace & delete
by tadman (Prior) on Aug 01, 2001 at 22:17 UTC
    As dragonchild says, it's probably your term that is causing the problem. If you're seeing ^H or ^? showing up in your input when you test your program, it's not Perl that's the problem, but your stty setting for erase that's not totally jiving with what you type.

    Perl, like any other UNIX program, just takes what it's given. That is, unless you're using something like Term::ReadLine which flips the terminal into "raw" mode. If you're seeing something messed up, the chances are that it would happen with something as simple as cat as well.
      I can't test which terms exactly at the moment as I'm working off site and am relying on reports from other users - though some of them will definetly be using the hyperterminal from win9x.
      Two have reported the old ^H. I was really wondering if there is a perl way to do it without `stty`-ing...

      Billy

        Since this can be a problem with other applications too, why don't you fix it outside of Perl? It's definitely not a Perl problem after all. Naturally Perl can fix it, but being lazy programmers we want to fix it only once, and for all, don't we? :-)

        Fixing the backspace problem is simple:

        • At the shell prompt, type cat or equivalent program waiting for your input;
        • Type a few letters then hit the key you want to act as delete (erase) key
        • You'll probably see ^? or ^H instead of what you expect
        • Quit the program (usually Enter followed by ^D will do
        • Type stty erase ^H or ^?, according to the character you've seen
        • Try again the cat test, this time you should be able to delete properly

        This is all you need, just add the command to your .tcsh or .bashrc or equivalent, so that it will be permanent and it will help other programs that do simple I/O too.

        Note: some versions of stty want you to type a ^ followed by the caracter, others want you to type the exact character. In the latter case, most modern shells (and many editors) allows you to press ^V (that is, a real CTRL+V) followed by the real ^? or ^H. Again, because of the special meaning of a ? for the shell, you might have to type \? instead.

        -- TMTOWTDI

Re: Backspace & delete
by ginseng (Pilgrim) on Aug 01, 2001 at 22:29 UTC
    Some of your options for dealing with terminal problems (or misconfigurations) are using Term::ReadKey or Term::Readline;, use Term::Cap; and use POSIX qw(:termios.h);. All of these, I believe, will give you options for changing the terminal's understanding of how to delete a character (among other things). (Yeah, I read the part about CPAN. Sorry.)

    I was going to post some real code, but I couldn't remember how to get into 'cbreak' mode for my test to work; so you probably ought to start with the perl docs, rather than trust my code ;)

Re: Backspace & delete (More info, please!)
by dragonchild (Archbishop) on Aug 01, 2001 at 21:54 UTC
    It would help more if you told us what worked and what didn't. As in, "This works under termtype A, but not termtype B. Under termtype C, backspace works, but not delete."

    The reason being that there are a number of reasons why something might not work, ranging from your setup to your environment to your interpreter version to the alignment of the planets. (Usually not the latter, but you never know!)