Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

perlfaq8

Perl 5 version 8.9 documentation
Recently read

perlfaq8

NAME

perlfaq8 - System Interaction ($Revision: 10183 $)

DESCRIPTION

This section of the Perl FAQ covers questions involving operating system interaction. Topics include interprocess communication (IPC), control over the user-interface (keyboard, screen and pointing devices), and most anything else not related to data manipulation.

Read the FAQs and documentation specific to the port of perl to your operating system (eg, perlvms, perlplan9, ...). These should contain more detailed information on the vagaries of your perl.

How do I find out which operating system I'm running under?

The $^O variable ($OSNAME if you use English) contains an indication of the name of the operating system (not its release number) that your perl binary was built for.

How come exec() doesn't return?

Because that's what it does: it replaces your currently running program with a different one. If you want to keep going (as is probably the case if you're asking this question) use system() instead.

How do I do fancy stuff with the keyboard/screen/mouse?

How you access/control keyboards, screens, and pointing devices ("mice") is system-dependent. Try the following modules:

  • Keyboard
    1. Term::Cap Standard perl distribution
    2. Term::ReadKey CPAN
    3. Term::ReadLine::Gnu CPAN
    4. Term::ReadLine::Perl CPAN
    5. Term::Screen CPAN
  • Screen
    1. Term::Cap Standard perl distribution
    2. Curses CPAN
    3. Term::ANSIColor CPAN
  • Mouse
    1. Tk CPAN

Some of these specific cases are shown as examples in other answers in this section of the perlfaq.

How do I print something out in color?

In general, you don't, because you don't know whether the recipient has a color-aware display device. If you know that they have an ANSI terminal that understands color, you can use the Term::ANSIColor module from CPAN:

  1. use Term::ANSIColor;
  2. print color("red"), "Stop!\n", color("reset");
  3. print color("green"), "Go!\n", color("reset");

Or like this:

  1. use Term::ANSIColor qw(:constants);
  2. print RED, "Stop!\n", RESET;
  3. print GREEN, "Go!\n", RESET;

How do I read just one key without waiting for a return key?

Controlling input buffering is a remarkably system-dependent matter. On many systems, you can just use the stty command as shown in getc, but as you see, that's already getting you into portability snags.

  1. open(TTY, "+</dev/tty") or die "no tty: $!";
  2. system "stty cbreak </dev/tty >/dev/tty 2>&1";
  3. $key = getc(TTY); # perhaps this works
  4. # OR ELSE
  5. sysread(TTY, $key, 1); # probably this does
  6. system "stty -cbreak </dev/tty >/dev/tty 2>&1";

The Term::ReadKey module from CPAN offers an easy-to-use interface that should be more efficient than shelling out to stty for each key. It even includes limited support for Windows.

  1. use Term::ReadKey;
  2. ReadMode('cbreak');
  3. $key = ReadKey(0);
  4. ReadMode('normal');

However, using the code requires that you have a working C compiler and can use it to build and install a CPAN module. Here's a solution using the standard POSIX module, which is already on your systems (assuming your system supports POSIX).

  1. use HotKey;
  2. $key = readkey();

And here's the HotKey module, which hides the somewhat mystifying calls to manipulate the POSIX termios structures.

  1. # HotKey.pm
  2. package HotKey;
  3. @ISA = qw(Exporter);
  4. @EXPORT = qw(cbreak cooked readkey);
  5. use strict;
  6. use POSIX qw(:termios_h);
  7. my ($term, $oterm, $echo, $noecho, $fd_stdin);
  8. $fd_stdin = fileno(STDIN);
  9. $term = POSIX::Termios->new();
  10. $term->getattr($fd_stdin);
  11. $oterm = $term->getlflag();
  12. $echo = ECHO | ECHOK | ICANON;
  13. $noecho = $oterm & ~$echo;
  14. sub cbreak {
  15. $term->setlflag($noecho); # ok, so i don't want echo either
  16. $term->setcc(VTIME, 1);
  17. $term->setattr($fd_stdin, TCSANOW);
  18. }
  19. sub cooked {
  20. $term->setlflag($oterm);
  21. $term->setcc(VTIME, 0);
  22. $term->setattr($fd_stdin, TCSANOW);
  23. }
  24. sub readkey {
  25. my $key = '';
  26. cbreak();
  27. sysread(STDIN, $key, 1);
  28. cooked();
  29. return $key;
  30. }
  31. END { cooked() }
  32. 1;

How do I check whether input is ready on the keyboard?

The easiest way to do this is to read a key in nonblocking mode with the Term::ReadKey module from CPAN, passing it an argument of -1 to indicate not to block:

  1. use Term::ReadKey;
  2. ReadMode('cbreak');
  3. if (defined ($char = ReadKey(-1)) ) {
  4. # input was waiting and it was $char
  5. } else {
  6. # no input was waiting
  7. }
  8. ReadMode('normal'); # restore normal tty settings

How do I clear the screen?

If you only have do so infrequently, use system:

  1. system("clear");

If you have to do this a lot, save the clear string so you can print it 100 times without calling a program 100 times:

  1. $clear_string = `clear`;
  2. print $clear_string;

If you're planning on doing other screen manipulations, like cursor positions, etc, you might wish to use Term::Cap module:

  1. use Term::Cap;
  2. $terminal = Term::Cap->Tgetent( {OSPEED => 9600} );
  3. $clear_string = $terminal->Tputs('cl');

How do I get the screen size?

If you have Term::ReadKey module installed from CPAN, you can use it to fetch the width and height in characters and in pixels:

  1. use Term::ReadKey;
  2. ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();

This is more portable than the raw ioctl, but not as illustrative:

  1. require 'sys/ioctl.ph';
  2. die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
  3. open(TTY, "+</dev/tty") or die "No tty: $!";
  4. unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
  5. die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
  6. }
  7. ($row, $col, $xpixel, $ypixel) = unpack('S4', $winsize);
  8. print "(row,col) = ($row,$col)";
  9. print " (xpixel,ypixel) = ($xpixel,$ypixel)" if $xpixel || $ypixel;
  10. print "\n";

How do I ask the user for a password?

(This question has nothing to do with the web. See a different FAQ for that.)

There's an example of this in crypt). First, you put the terminal into "no echo" mode, then just read the password normally. You may do this with an old-style ioctl() function, POSIX terminal control (see POSIX or its documentation the Camel Book), or a call to the stty program, with varying degrees of portability.

You can also do this for most systems using the Term::ReadKey module from CPAN, which is easier to use and in theory more portable.

  1. use Term::ReadKey;
  2. ReadMode('noecho');
  3. $password = ReadLine(0);

How do I read and write the serial port?

This depends on which operating system your program is running on. In the case of Unix, the serial ports will be accessible through files in /dev; on other systems, device names will doubtless differ. Several problem areas common to all device interaction are the following:

  • lockfiles

    Your system may use lockfiles to control multiple access. Make sure you follow the correct protocol. Unpredictable behavior can result from multiple processes reading from one device.

  • open mode

    If you expect to use both read and write operations on the device, you'll have to open it for update (see open for details). You may wish to open it without running the risk of blocking by using sysopen() and O_RDWR|O_NDELAY|O_NOCTTY from the Fcntl module (part of the standard perl distribution). See sysopen for more on this approach.

  • end of line

    Some devices will be expecting a "\r" at the end of each line rather than a "\n". In some ports of perl, "\r" and "\n" are different from their usual (Unix) ASCII values of "\012" and "\015". You may have to give the numeric values you want directly, using octal ("\015"), hex ("0x0D"), or as a control-character specification ("\cM").

    1. print DEV "atv1\012"; # wrong, for some devices
    2. print DEV "atv1\015"; # right, for some devices

    Even though with normal text files a "\n" will do the trick, there is still no unified scheme for terminating a line that is portable between Unix, DOS/Win, and Macintosh, except to terminate ALL line ends with "\015\012", and strip what you don't need from the output. This applies especially to socket I/O and autoflushing, discussed next.

  • flushing output

    If you expect characters to get to your device when you print() them, you'll want to autoflush that filehandle. You can use select() and the $| variable to control autoflushing (see perlvar/$ and select, or perlfaq5, "How do I flush/unbuffer an output filehandle? Why must I do this?"):

    1. $oldh = select(DEV);
    2. $| = 1;
    3. select($oldh);

    You'll also see code that does this without a temporary variable, as in

    1. select((select(DEV), $| = 1)[0]);

    Or if you don't mind pulling in a few thousand lines of code just because you're afraid of a little $| variable:

    1. use IO::Handle;
    2. DEV->autoflush(1);

    As mentioned in the previous item, this still doesn't work when using socket I/O between Unix and Macintosh. You'll need to hard code your line terminators, in that case.

  • non-blocking input

    If you are doing a blocking read() or sysread(), you'll have to arrange for an alarm handler to provide a timeout (see alarm). If you have a non-blocking open, you'll likely have a non-blocking read, which means you may have to use a 4-arg select() to determine whether I/O is ready on that device (see select.

While trying to read from his caller-id box, the notorious Jamie Zawinski <jwz@netscape.com> , after much gnashing of teeth and fighting with sysread, sysopen, POSIX's tcgetattr business, and various other functions that go bump in the night, finally came up with this:

  1. sub open_modem {
  2. use IPC::Open2;
  3. my $stty = `/bin/stty -g`;
  4. open2( \*MODEM_IN, \*MODEM_OUT, "cu -l$modem_device -s2400 2>&1");
  5. # starting cu hoses /dev/tty's stty settings, even when it has
  6. # been opened on a pipe...
  7. system("/bin/stty $stty");
  8. $_ = <MODEM_IN>;
  9. chomp;
  10. if ( !m/^Connected/ ) {
  11. print STDERR "$0: cu printed `$_' instead of `Connected'\n";
  12. }
  13. }

How do I decode encrypted password files?

You spend lots and lots of money on dedicated hardware, but this is bound to get you talked about.

Seriously, you can't if they are Unix password files--the Unix password system employs one-way encryption. It's more like hashing than encryption. The best you can do is check whether something else hashes to the same string. You can't turn a hash back into the original string. Programs like Crack can forcibly (and intelligently) try to guess passwords, but don't (can't) guarantee quick success.

If you're worried about users selecting bad passwords, you should proactively check when they try to change their password (by modifying passwd(1), for example).

How do I start a process in the background?

Several modules can start other processes that do not block your Perl program. You can use IPC::Open3, Parallel::Jobs, IPC::Run, and some of the POE modules. See CPAN for more details.

You could also use

  1. system("cmd &")

or you could use fork as documented in fork, with further examples in perlipc. Some things to be aware of, if you're on a Unix-like system:

  • STDIN, STDOUT, and STDERR are shared

    Both the main process and the backgrounded one (the "child" process) share the same STDIN, STDOUT and STDERR filehandles. If both try to access them at once, strange things can happen. You may want to close or reopen these for the child. You can get around this with opening a pipe (see open) but on some systems this means that the child process cannot outlive the parent.

  • Signals

    You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too. SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die). This is not an issue with system("cmd&").

  • Zombies

    You have to be prepared to "reap" the child process when it finishes.

    1. $SIG{CHLD} = sub { wait };
    2. $SIG{CHLD} = 'IGNORE';

    You can also use a double fork. You immediately wait() for your first child, and the init daemon will wait() for your grandchild once it exits.

    1. unless ($pid = fork) {
    2. unless (fork) {
    3. exec "what you really wanna do";
    4. die "exec failed!";
    5. }
    6. exit 0;
    7. }
    8. waitpid($pid, 0);

    See Signals in perlipc for other examples of code to do this. Zombies are not an issue with system("prog &") .

How do I trap control characters/signals?

You don't actually "trap" a control character. Instead, that character generates a signal which is sent to your terminal's currently foregrounded process group, which you then trap in your process. Signals are documented in Signals in perlipc and the section on "Signals" in the Camel.

You can set the values of the %SIG hash to be the functions you want to handle the signal. After perl catches the signal, it looks in %SIG for a key with the same name as the signal, then calls the subroutine value for that key.

  1. # as an anonymous subroutine
  2. $SIG{INT} = sub { syswrite(STDERR, "ouch\n", 5 ) };
  3. # or a reference to a function
  4. $SIG{INT} = \&ouch;
  5. # or the name of the function as a string
  6. $SIG{INT} = "ouch";

Perl versions before 5.8 had in its C source code signal handlers which would catch the signal and possibly run a Perl function that you had set in %SIG. This violated the rules of signal handling at that level causing perl to dump core. Since version 5.8.0, perl looks at %SIG *after* the signal has been caught, rather than while it is being caught. Previous versions of this answer were incorrect.

How do I modify the shadow password file on a Unix system?

If perl was installed correctly and your shadow library was written properly, the getpw*() functions described in perlfunc should in theory provide (read-only) access to entries in the shadow password file. To change the file, make a new shadow password file (the format varies from system to system--see passwd for specifics) and use pwd_mkdb(8) to install it (see pwd_mkdb for more details).

How do I set the time and date?

Assuming you're running under sufficient permissions, you should be able to set the system-wide date and time by running the date(1) program. (There is no way to set the time and date on a per-process basis.) This mechanism will work for Unix, MS-DOS, Windows, and NT; the VMS equivalent is set time .

However, if all you want to do is change your time zone, you can probably get away with setting an environment variable:

  1. $ENV{TZ} = "MST7MDT"; # unixish
  2. $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms
  3. system "trn comp.lang.perl.misc";

How can I sleep() or alarm() for under a second?

If you want finer granularity than the 1 second that the sleep() function provides, the easiest way is to use the select() function as documented in select. Try the Time::HiRes and the BSD::Itimer modules (available from CPAN, and starting from Perl 5.8 Time::HiRes is part of the standard distribution).

How can I measure time under a second?

(contributed by brian d foy)

The Time::HiRes module (part of the standard distribution as of Perl 5.8) measures time with the gettimeofday() system call, which returns the time in microseconds since the epoch. If you can't install Time::HiRes for older Perls and you are on a Unixish system, you may be able to call gettimeofday(2) directly. See syscall.

How can I do an atexit() or setjmp()/longjmp()? (Exception handling)

Release 5 of Perl added the END block, which can be used to simulate atexit(). Each package's END block is called when the program or thread ends (see perlmod manpage for more details).

For example, you can use this to make sure your filter program managed to finish its output without filling up the disk:

  1. END {
  2. close(STDOUT) || die "stdout close failed: $!";
  3. }

The END block isn't called when untrapped signals kill the program, though, so if you use END blocks you should also use

  1. use sigtrap qw(die normal-signals);

Perl's exception-handling mechanism is its eval() operator. You can use eval() as setjmp and die() as longjmp. For details of this, see the section on signals, especially the time-out handler for a blocking flock() in Signals in perlipc or the section on "Signals" in the Camel Book.

If exception handling is all you're interested in, try the exceptions.pl library (part of the standard perl distribution).

If you want the atexit() syntax (and an rmexit() as well), try the AtExit module available from CPAN.

Why doesn't my sockets program work under System V (Solaris)? What does the error message "Protocol not supported" mean?

Some Sys-V based systems, notably Solaris 2.X, redefined some of the standard socket constants. Since these were constant across all architectures, they were often hardwired into perl code. The proper way to deal with this is to "use Socket" to get the correct values.

Note that even though SunOS and Solaris are binary compatible, these values are different. Go figure.

How can I call my system's unique C functions from Perl?

In most cases, you write an external module to do it--see the answer to "Where can I learn about linking C with Perl? [h2xs, xsubpp]". However, if the function is a system call, and your system supports syscall(), you can use the syscall function (documented in perlfunc).

Remember to check the modules that came with your distribution, and CPAN as well--someone may already have written a module to do it. On Windows, try Win32::API. On Macs, try Mac::Carbon. If no module has an interface to the C function, you can inline a bit of C in your Perl source with Inline::C.

Where do I get the include files to do ioctl() or syscall()?

Historically, these would be generated by the h2ph tool, part of the standard perl distribution. This program converts cpp(1) directives in C header files to files containing subroutine definitions, like &SYS_getitimer, which you can use as arguments to your functions. It doesn't work perfectly, but it usually gets most of the job done. Simple files like errno.h, syscall.h, and socket.h were fine, but the hard ones like ioctl.h nearly always need to hand-edited. Here's how to install the *.ph files:

  1. 1. become super-user
  2. 2. cd /usr/include
  3. 3. h2ph *.h */*.h

If your system supports dynamic loading, for reasons of portability and sanity you probably ought to use h2xs (also part of the standard perl distribution). This tool converts C header files to Perl extensions. See perlxstut for how to get started with h2xs.

If your system doesn't support dynamic loading, you still probably ought to use h2xs. See perlxstut and ExtUtils::MakeMaker for more information (in brief, just use make perl instead of a plain make to rebuild perl with a new static extension).

Why do setuid perl scripts complain about kernel problems?

Some operating systems have bugs in the kernel that make setuid scripts inherently insecure. Perl gives you a number of options (described in perlsec) to work around such systems.

How can I open a pipe both to and from a command?

The IPC::Open2 module (part of the standard perl distribution) is an easy-to-use approach that internally uses pipe(), fork(), and exec() to do the job. Make sure you read the deadlock warnings in its documentation, though (see IPC::Open2). See Bidirectional Communication with Another Process in perlipc and Bidirectional Communication with Yourself in perlipc

You may also use the IPC::Open3 module (part of the standard perl distribution), but be warned that it has a different order of arguments from IPC::Open2 (see IPC::Open3).

Why can't I get the output of a command with system()?

You're confusing the purpose of system() and backticks (``). system() runs a command and returns exit status information (as a 16 bit value: the low 7 bits are the signal the process died from, if any, and the high 8 bits are the actual exit value). Backticks (``) run a command and return what it sent to STDOUT.

  1. $exit_status = system("mail-users");
  2. $output_string = `ls`;

How can I capture STDERR from an external command?

There are three basic ways of running external commands:

  1. system $cmd; # using system()
  2. $output = `$cmd`; # using backticks (``)
  3. open (PIPE, "cmd |"); # using open()

With system(), both STDOUT and STDERR will go the same place as the script's STDOUT and STDERR, unless the system() command redirects them. Backticks and open() read only the STDOUT of your command.

You can also use the open3() function from IPC::Open3. Benjamin Goldberg provides some sample code:

To capture a program's STDOUT, but discard its STDERR:

  1. use IPC::Open3;
  2. use File::Spec;
  3. use Symbol qw(gensym);
  4. open(NULL, ">", File::Spec->devnull);
  5. my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
  6. while( <PH> ) { }
  7. waitpid($pid, 0);

To capture a program's STDERR, but discard its STDOUT:

  1. use IPC::Open3;
  2. use File::Spec;
  3. use Symbol qw(gensym);
  4. open(NULL, ">", File::Spec->devnull);
  5. my $pid = open3(gensym, ">&NULL", \*PH, "cmd");
  6. while( <PH> ) { }
  7. waitpid($pid, 0);

To capture a program's STDERR, and let its STDOUT go to our own STDERR:

  1. use IPC::Open3;
  2. use Symbol qw(gensym);
  3. my $pid = open3(gensym, ">&STDERR", \*PH, "cmd");
  4. while( <PH> ) { }
  5. waitpid($pid, 0);

To read both a command's STDOUT and its STDERR separately, you can redirect them to temp files, let the command run, then read the temp files:

  1. use IPC::Open3;
  2. use Symbol qw(gensym);
  3. use IO::File;
  4. local *CATCHOUT = IO::File->new_tmpfile;
  5. local *CATCHERR = IO::File->new_tmpfile;
  6. my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd");
  7. waitpid($pid, 0);
  8. seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
  9. while( <CATCHOUT> ) {}
  10. while( <CATCHERR> ) {}

But there's no real need for *both* to be tempfiles... the following should work just as well, without deadlocking:

  1. use IPC::Open3;
  2. use Symbol qw(gensym);
  3. use IO::File;
  4. local *CATCHERR = IO::File->new_tmpfile;
  5. my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd");
  6. while( <CATCHOUT> ) {}
  7. waitpid($pid, 0);
  8. seek CATCHERR, 0, 0;
  9. while( <CATCHERR> ) {}

And it'll be faster, too, since we can begin processing the program's stdout immediately, rather than waiting for the program to finish.

With any of these, you can change file descriptors before the call:

  1. open(STDOUT, ">logfile");
  2. system("ls");

or you can use Bourne shell file-descriptor redirection:

  1. $output = `$cmd 2>some_file`;
  2. open (PIPE, "cmd 2>some_file |");

You can also use file-descriptor redirection to make STDERR a duplicate of STDOUT:

  1. $output = `$cmd 2>&1`;
  2. open (PIPE, "cmd 2>&1 |");

Note that you cannot simply open STDERR to be a dup of STDOUT in your Perl program and avoid calling the shell to do the redirection. This doesn't work:

  1. open(STDERR, ">&STDOUT");
  2. $alloutput = `cmd args`; # stderr still escapes

This fails because the open() makes STDERR go to where STDOUT was going at the time of the open(). The backticks then make STDOUT go to a string, but don't change STDERR (which still goes to the old STDOUT).

Note that you must use Bourne shell (sh(1)) redirection syntax in backticks, not csh(1)! Details on why Perl's system() and backtick and pipe opens all use the Bourne shell are in the versus/csh.whynot article in the "Far More Than You Ever Wanted To Know" collection in http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz . To capture a command's STDERR and STDOUT together:

  1. $output = `cmd 2>&1`; # either with backticks
  2. $pid = open(PH, "cmd 2>&1 |"); # or with an open pipe
  3. while (<PH>) { } # plus a read

To capture a command's STDOUT but discard its STDERR:

  1. $output = `cmd 2>/dev/null`; # either with backticks
  2. $pid = open(PH, "cmd 2>/dev/null |"); # or with an open pipe
  3. while (<PH>) { } # plus a read

To capture a command's STDERR but discard its STDOUT:

  1. $output = `cmd 2>&1 1>/dev/null`; # either with backticks
  2. $pid = open(PH, "cmd 2>&1 1>/dev/null |"); # or with an open pipe
  3. while (<PH>) { } # plus a read

To exchange a command's STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out our old STDERR:

  1. $output = `cmd 3>&1 1>&2 2>&3 3>&-`; # either with backticks
  2. $pid = open(PH, "cmd 3>&1 1>&2 2>&3 3>&-|");# or with an open pipe
  3. while (<PH>) { } # plus a read

To read both a command's STDOUT and its STDERR separately, it's easiest to redirect them separately to files, and then read from those files when the program is done:

  1. system("program args 1>program.stdout 2>program.stderr");

Ordering is important in all these examples. That's because the shell processes file descriptor redirections in strictly left to right order.

  1. system("prog args 1>tmpfile 2>&1");
  2. system("prog args 2>&1 1>tmpfile");

The first command sends both standard out and standard error to the temporary file. The second command sends only the old standard output there, and the old standard error shows up on the old standard out.

Why doesn't open() return an error when a pipe open fails?

If the second argument to a piped open() contains shell metacharacters, perl fork()s, then exec()s a shell to decode the metacharacters and eventually run the desired program. If the program couldn't be run, it's the shell that gets the message, not Perl. All your Perl program can find out is whether the shell itself could be successfully started. You can still capture the shell's STDERR and check it for error messages. See How can I capture STDERR from an external command? elsewhere in this document, or use the IPC::Open3 module.

If there are no shell metacharacters in the argument of open(), Perl runs the command directly, without using the shell, and can correctly report whether the command started.

What's wrong with using backticks in a void context?

Strictly speaking, nothing. Stylistically speaking, it's not a good way to write maintainable code. Perl has several operators for running external commands. Backticks are one; they collect the output from the command for use in your program. The system function is another; it doesn't do this.

Writing backticks in your program sends a clear message to the readers of your code that you wanted to collect the output of the command. Why send a clear message that isn't true?

Consider this line:

  1. `cat /etc/termcap`;

You forgot to check $? to see whether the program even ran correctly. Even if you wrote

  1. print `cat /etc/termcap`;

this code could and probably should be written as

  1. system("cat /etc/termcap") == 0
  2. or die "cat program failed!";

which will echo the cat command's output as it is generated, instead of waiting until the program has completed to print it out. It also checks the return value.

system also provides direct control over whether shell wildcard processing may take place, whereas backticks do not.

How can I call backticks without shell processing?

This is a bit tricky. You can't simply write the command like this:

  1. @ok = `grep @opts '$search_string' @filenames`;

As of Perl 5.8.0, you can use open() with multiple arguments. Just like the list forms of system() and exec(), no shell escapes happen.

  1. open( GREP, "-|", 'grep', @opts, $search_string, @filenames );
  2. chomp(@ok = <GREP>);
  3. close GREP;

You can also:

  1. my @ok = ();
  2. if (open(GREP, "-|")) {
  3. while (<GREP>) {
  4. chomp;
  5. push(@ok, $_);
  6. }
  7. close GREP;
  8. } else {
  9. exec 'grep', @opts, $search_string, @filenames;
  10. }

Just as with system(), no shell escapes happen when you exec() a list. Further examples of this can be found in Safe Pipe Opens in perlipc.

Note that if you're using Windows, no solution to this vexing issue is even possible. Even if Perl were to emulate fork(), you'd still be stuck, because Windows does not have an argc/argv-style API.

Why can't my script read from STDIN after I gave it EOF (^D on Unix, ^Z on MS-DOS)?

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:

  1. $where = tell(LOG);
  2. 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.

How can I convert my shell script to perl?

Learn Perl and rewrite it. Seriously, there's no simple converter. Things that are awkward to do in the shell are easy to do in Perl, and this very awkwardness is what would make a shell->perl converter nigh-on impossible to write. By rewriting it, you'll think about what you're really trying to do, and hopefully will escape the shell's pipeline datastream paradigm, which while convenient for some matters, causes many inefficiencies.

Can I use perl to run a telnet or ftp session?

Try the Net::FTP, TCP::Client, and Net::Telnet modules (available from CPAN). http://www.cpan.org/scripts/netstuff/telnet.emul.shar will also help for emulating the telnet protocol, but Net::Telnet is quite probably easier to use..

If all you want to do is pretend to be telnet but don't need the initial telnet handshaking, then the standard dual-process approach will suffice:

  1. use IO::Socket; # new in 5.004
  2. $handle = IO::Socket::INET->new('www.perl.com:80')
  3. or die "can't connect to port 80 on www.perl.com: $!";
  4. $handle->autoflush(1);
  5. if (fork()) { # XXX: undef means failure
  6. select($handle);
  7. print while <STDIN>; # everything from stdin to socket
  8. } else {
  9. print while <$handle>; # everything from socket to stdout
  10. }
  11. close $handle;
  12. exit;

How can I write expect in Perl?

Once upon a time, there was a library called chat2.pl (part of the standard perl distribution), which never really got finished. If you find it somewhere, don't use it. These days, your best bet is to look at the Expect module available from CPAN, which also requires two other modules from CPAN, IO::Pty and IO::Stty.

Is there a way to hide perl's command line from programs such as "ps"?

First of all note that if you're doing this for security reasons (to avoid people seeing passwords, for example) then you should rewrite your program so that critical information is never given as an argument. Hiding the arguments won't make your program completely secure.

To actually alter the visible command line, you can assign to the variable $0 as documented in perlvar. This won't work on all operating systems, though. Daemon programs like sendmail place their state there, as in:

  1. $0 = "orcus [accepting connections]";

I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?

  • Unix

    In the strictest sense, it can't be done--the script executes as a different process from the shell it was started from. Changes to a process are not reflected in its parent--only in any children created after the change. There is shell magic that may allow you to fake it by eval()ing the script's output in your shell; check out the comp.unix.questions FAQ for details.

How do I close a process's filehandle without waiting for it to complete?

Assuming your system supports such things, just send an appropriate signal to the process (see kill). It's common to first send a TERM signal, wait a little bit, and then send a KILL signal to finish it off.

How do I fork a daemon process?

If by daemon process you mean one that's detached (disassociated from its tty), then the following process is reported to work on most Unixish systems. Non-Unix users should check their Your_OS::Process module for other solutions.

  • Open /dev/tty and use the TIOCNOTTY ioctl on it. See tty for details. Or better yet, you can just use the POSIX::setsid() function, so you don't have to worry about process groups.

  • Change directory to /

  • Reopen STDIN, STDOUT, and STDERR so they're not connected to the old tty.

  • Background yourself like this:

    1. fork && exit;

The Proc::Daemon module, available from CPAN, provides a function to perform these actions for you.

How do I find out if I'm running interactively or not?

Good question. Sometimes -t STDIN and -t STDOUT can give clues, sometimes not.

  1. if (-t STDIN && -t STDOUT) {
  2. print "Now what? ";
  3. }

On POSIX systems, you can test whether your own process group matches the current process group of your controlling terminal as follows:

  1. use POSIX qw/getpgrp tcgetpgrp/;
  2. # Some POSIX systems, such as Linux, can be
  3. # without a /dev/tty at boot time.
  4. if (!open(TTY, "/dev/tty")) {
  5. print "no tty\n";
  6. } else {
  7. $tpgrp = tcgetpgrp(fileno(*TTY));
  8. $pgrp = getpgrp();
  9. if ($tpgrp == $pgrp) {
  10. print "foreground\n";
  11. } else {
  12. print "background\n";
  13. }
  14. }

How do I timeout a slow event?

Use the alarm() function, probably in conjunction with a signal handler, as documented in Signals in perlipc and the section on "Signals" in the Camel. You may instead use the more flexible Sys::AlarmCall module available from CPAN.

The alarm() function is not implemented on all versions of Windows. Check the documentation for your specific version of Perl.

How do I set CPU limits?

(contributed by Xho)

Use the BSD::Resource module from CPAN. As an example:

  1. use BSD::Resource;
  2. setrlimit(RLIMIT_CPU,10,20) or die $!;

This sets the soft and hard limits to 10 and 20 seconds, respectively. After 10 seconds of time spent running on the CPU (not "wall" time), the process will be sent a signal (XCPU on some systems) which, if not trapped, will cause the process to terminate. If that signal is trapped, then after 10 more seconds (20 seconds in total) the process will be killed with a non-trappable signal.

See the BSD::Resource and your systems documentation for the gory details.

How do I avoid zombies on a Unix system?

Use the reaper code from Signals in perlipc to call wait() when a SIGCHLD is received, or else use the double-fork technique described in How do I start a process in the background? in perlfaq8.

How do I use an SQL database?

The DBI module provides an abstract interface to most database servers and types, including Oracle, DB2, Sybase, mysql, Postgresql, ODBC, and flat files. The DBI module accesses each database type through a database driver, or DBD. You can see a complete list of available drivers on CPAN: http://www.cpan.org/modules/by-module/DBD/ . You can read more about DBI on http://dbi.perl.org .

Other modules provide more specific access: Win32::ODBC, Alzabo, iodbc, and others found on CPAN Search: http://search.cpan.org .

How do I make a system() exit on control-C?

You can't. You need to imitate the system() call (see perlipc for sample code) and then have a signal handler for the INT signal that passes the signal on to the subprocess. Or you can check for it:

  1. $rc = system($cmd);
  2. if ($rc & 127) { die "signal death" }

How do I open a file without blocking?

If you're lucky enough to be using a system that supports non-blocking reads (most Unixish systems do), you need only to use the O_NDELAY or O_NONBLOCK flag from the Fcntl module in conjunction with sysopen():

  1. use Fcntl;
  2. sysopen(FH, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644)
  3. or die "can't open /foo/somefile: $!":

How do I tell the difference between errors from the shell and perl?

(answer contributed by brian d foy)

When you run a Perl script, something else is running the script for you, and that something else may output error messages. The script might emit its own warnings and error messages. Most of the time you cannot tell who said what.

You probably cannot fix the thing that runs perl, but you can change how perl outputs its warnings by defining a custom warning and die functions.

Consider this script, which has an error you may not notice immediately.

  1. #!/usr/locl/bin/perl
  2. print "Hello World\n";

I get an error when I run this from my shell (which happens to be bash). That may look like perl forgot it has a print() function, but my shebang line is not the path to perl, so the shell runs the script, and I get the error.

  1. $ ./test
  2. ./test: line 3: print: command not found

A quick and dirty fix involves a little bit of code, but this may be all you need to figure out the problem.

  1. #!/usr/bin/perl -w
  2. BEGIN {
  3. $SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; };
  4. $SIG{__DIE__} = sub{ print STDERR "Perl: ", @_; exit 1};
  5. }
  6. $a = 1 + undef;
  7. $x / 0;
  8. __END__

The perl message comes out with "Perl" in front. The BEGIN block works at compile time so all of the compilation errors and warnings get the "Perl:" prefix too.

  1. Perl: Useless use of division (/) in void context at ./test line 9.
  2. Perl: Name "main::a" used only once: possible typo at ./test line 8.
  3. Perl: Name "main::x" used only once: possible typo at ./test line 9.
  4. Perl: Use of uninitialized value in addition (+) at ./test line 8.
  5. Perl: Use of uninitialized value in division (/) at ./test line 9.
  6. Perl: Illegal division by zero at ./test line 9.
  7. Perl: Illegal division by zero at -e line 3.

If I don't see that "Perl:", it's not from perl.

You could also just know all the perl errors, and although there are some people who may know all of them, you probably don't. However, they all should be in the perldiag manpage. If you don't find the error in there, it probably isn't a perl error.

Looking up every message is not the easiest way, so let perl to do it for you. Use the diagnostics pragma with turns perl's normal messages into longer discussions on the topic.

  1. use diagnostics;

If you don't get a paragraph or two of expanded discussion, it might not be perl's message.

How do I install a module from CPAN?

The easiest way is to have a module also named CPAN do it for you. This module comes with perl version 5.004 and later.

  1. $ perl -MCPAN -e shell
  2. cpan shell -- CPAN exploration and modules installation (v1.59_54)
  3. ReadLine support enabled
  4. cpan> install Some::Module

To manually install the CPAN module, or any well-behaved CPAN module for that matter, follow these steps:

1

Unpack the source into a temporary area.

2
  1. perl Makefile.PL
3
  1. make
4
  1. make test
5
  1. make install

If your version of perl is compiled without dynamic loading, then you just need to replace step 3 (make) with make perl and you will get a new perl binary with your extension linked in.

See ExtUtils::MakeMaker for more details on building extensions. See also the next question, "What's the difference between require and use?".

What's the difference between require and use?

Perl offers several different ways to include code from one file into another. Here are the deltas between the various inclusion constructs:

  1. 1) do $file is like eval `cat $file`, except the former
  2. 1.1: searches @INC and updates %INC.
  3. 1.2: bequeaths an *unrelated* lexical scope on the eval'ed code.
  4. 2) require $file is like do $file, except the former
  5. 2.1: checks for redundant loading, skipping already loaded files.
  6. 2.2: raises an exception on failure to find, compile, or execute $file.
  7. 3) require Module is like require "Module.pm", except the former
  8. 3.1: translates each "::" into your system's directory separator.
  9. 3.2: primes the parser to disambiguate class Module as an indirect object.
  10. 4) use Module is like require Module, except the former
  11. 4.1: loads the module at compile time, not run-time.
  12. 4.2: imports symbols and semantics from that package to the current one.

In general, you usually want use and a proper Perl module.

How do I keep my own module/library directory?

When you build modules, tell Perl where to install the modules.

For Makefile.PL -based distributions, use the PREFIX and LIB options when generating Makefiles:

  1. perl Makefile.PL PREFIX=/mydir/perl LIB=/mydir/perl/lib

You can set this in your CPAN.pm configuration so modules automatically install in your private library directory when you use the CPAN.pm shell:

  1. % cpan
  2. cpan> o conf makepl_arg PREFIX=/mydir/perl,LIB=/mydir/perl/lib
  3. cpan> o conf commit

For Build.PL -based distributions, use the --install_base option:

  1. perl Build.PL --install_base /mydir/perl

You can configure CPAN.pm to automatically use this option too:

  1. % cpan
  2. cpan> o conf mbuild_arg --install_base /mydir/perl
  3. cpan> o conf commit

How do I add the directory my program lives in to the module/library search path?

(contributed by brian d foy)

If you know the directory already, you can add it to @INC as you would for any other directory. You might <use lib> if you know the directory at compile time:

  1. use lib $directory;

The trick in this task is to find the directory. Before your script does anything else (such as a chdir), you can get the current working directory with the Cwd module, which comes with Perl:

  1. BEGIN {
  2. use Cwd;
  3. our $directory = cwd;
  4. }
  5. use lib $directory;

You can do a similar thing with the value of $0 , which holds the script name. That might hold a relative path, but rel2abs can turn it into an absolute path. Once you have the

  1. BEGIN {
  2. use File::Spec::Functions qw(rel2abs);
  3. use File::Basename qw(dirname);
  4. my $path = rel2abs( $0 );
  5. our $directory = dirname( $path );
  6. }
  7. use lib $directory;

The FindBin module, which comes with Perl, might work. It searches through $ENV{PATH} (so your script has to be in one of those directories). You can then use that directory (in $FindBin::Bin ) to locate nearby directories you want to add:

  1. use FindBin;
  2. use lib "$FindBin::Bin/../lib";

How do I add a directory to my include path (@INC) at runtime?

Here are the suggested ways of modifying your include path, including environment variables, run-time switches, and in-code statements:

  • the PERLLIB environment variable
    1. $ export PERLLIB=/path/to/my/dir
    2. $ perl program.pl
  • the PERL5LIB environment variable
    1. $ export PERL5LIB=/path/to/my/dir
    2. $ perl program.pl
  • the perl -Idir command line flag
    1. $ perl -I/path/to/my/dir program.pl
  • the use lib pragma:
    1. use lib "$ENV{HOME}/myown_perllib";

The last is particularly useful because it knows about machine dependent architectures. The lib.pm pragmatic module was first included with the 5.002 release of Perl.

What is socket.ph and where do I get it?

It's a Perl 4 style file defining values for system networking constants. Sometimes it is built using h2ph when Perl is installed, but other times it is not. Modern programs use Socket; instead.

REVISION

Revision: $Revision: 10183 $

Date: $Date: 2007-11-07 09:35:12 +0100 (Wed, 07 Nov 2007) $

See perlfaq for source control details and availability.

AUTHOR AND COPYRIGHT

Copyright (c) 1997-2007 Tom Christiansen, Nathan Torkington, and other authors as noted. All rights reserved.

This documentation is free; you can redistribute it and/or modify it under the same terms as Perl itself.

Irrespective of its distribution, all code examples in this file are hereby placed into the public domain. You are permitted and encouraged to use this code in your own programs for fun or for profit as you see fit. A simple comment in the code giving credit would be courteous but is not required.

Page index