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

In Perl, I'm trying to get the functionality of the unix command stty without having to do a system call. Two functionalities I am trying to use is setting the interrupt key to something else, e.g.

stty intr 0x7f
stty intr 0x03

Or setting input settings like:

stty raw
stty -crlf

I have seen a package on CPAN named IO::Stty, but my boss would prefer not to install any extra packages on the system. I couldn't see how the POSIX module could do this. I know in newer versions of Perl, I can use the open pragma, e.g.
use open IN => ":crlf", OUT => ":bytes";
but I am using an older version of Perl that does not have this pragma.
Any ideas?

Thanks,
Imran

Replies are listed 'Best First'.
Re: stty functionality in Perl
by ambrus (Abbot) on Apr 08, 2005 at 19:40 UTC

    You have to use POSIX::Termios, see POSIX. Alternatively, call the raw ioctls.

      With POSIX, I could do this:
      #!/usr/bin/perl use strict; use POSIX qw(:termios_h); my $term = POSIX::Termios->new; $term->getattr(fileno(STDIN)); print join(" - ", IGNCR, ECHO, ~ECHO, ECHOK, ICANON, $term->getlflag() + )." \n"; print ($term->getlflag() | (ECHO | ECHOK |ICANON) ); print "\n"; print ($term->getlflag() & ~(ECHO | ECHOK |ICANON) ); print "\n"; print ($term->getlflag() | (ECHO | ECHOK |ICANON) ); print "\n";
      Is there a simpler way than this to set and unset raw/crlf or echo on/off mode?

        Yes, I call stty from the perl code like

        system "stty", qw"-echo -echonl -echoke -echoe -isig" and die "error during stty"; ... system "stty", "sane" and warn "cannot reset stty settings";
        in a script I've written. Replace the flags with whatever you want. If you don't want to install addittional modules, I doubt there's a simpler way then these two.

        Well ... yes. Term::ReadKey but your boss is in the way there if it's not on your system or system but your in your own way not wanting to use system.

        -derby