Book Home Programming PerlSearch this book

32.36. POSIX

use POSIX;

# Round floats up or down to nearest integer.
$n = ceil($n);      # round up
$n = floor($n);     # round down

# Produces "2000-04-01" for today.
$datestr = strftime("%Y-%m-%d", localtime);

# Produces "Saturday 04/01/00" for same date.
$datestr = strftime("%A %D", localtime);

# Try new temporary filenames until we get one
# that didn't already exist; see also File::Temp
# on CPAN, or in v5.6.1.
do {
    $name = tmpnam();
} until sysopen(FH, $name, O_CREAT|O_EXCL|O_RDWR, 0666);

# Check for whether system has insecure chown giveaway.
if (sysconf(_PC_CHOWN_RESTRICTED)) {
    print "Hurray -- only the superuser may call chown\n";
}

# Find current system's uname info.
my($kernel, $hostname, $release, $version, $hardware) = uname();

use POSIX ":sys_wait_h";
while (($dead_pid = waitpid(-1, &WNOHANG)) > 0) {
    # Do something with $dead_pid if you want.
}

# Become new session/process-group leader (needed to create daemons
# unaffected by keyboard signals or exiting login shells).
setsid(0)           or die "setsid failed: $!";
Perl's POSIX module permits you to access all (or nearly all) the standard POSIX 1003.1 identifiers, plus a few more from ANSI C that we didn't know where else to put. This module provides more functions than any other. See its online documentation for the gory details or the POSIX Programmer's Guide, by Donald Lewine (O'Reilly, 1991).

Identifiers that are parameterless #defines in C, such as EINTR or O_NDELAY, are automatically exported into your namespace as constant functions. Functions that aren't normally available in Perl (like floor, ceil, strftime, uname, setsid, setlocale, and sysconf) are exported by default. Functions with the same name as a Perl built-in, like open, are not exported unless specifically requested, but most folks are likely to prefer fully qualified function names to distinguish POSIX::open from CORE::open.

A few functions are not implemented because they are C-specific. If you attempt to call these, they print a message telling you that they aren't implemented, and suggest using the Perl equivalent should one exist. For example, trying to access the setjmp function elicits the message "setjmp() is C-specific: use eval {} instead", and tmpfile tells you to "Use method IO::File::new_tmpfile()". (But as of 5.6.1 you should be using File::Temp instead.)

The POSIX module lets you get as close to the operating system (or those parts of the POSIX standard addresses, at least) as any C programmer could. This lets you do some phenomenally powerful and useful things, like blocking signals and controlling the terminal I/O settings. However, it also means that your code will end up looking quasi-C-like. By way of useful demonstration of how to get around input buffering, here's an example of a complete program for getting unbuffered, single-character input under any POSIX system:

#!/usr/bin/perl -w
use strict;
$| = 1;
for (1..4) {
    my $got;
    print "gimme: ";
    $got = getone();
    print "--> $got\n";
}
exit;

BEGIN {
    use POSIX qw(:termios_h);
    my ($term, $oterm, $echo, $noecho, $fd_stdin);
    $fd_stdin = fileno(STDIN);
    $term     = POSIX::Termios->new();
    $term->getattr($fd_stdin);
    $oterm    = $term->getlflag();
    $echo     = ECHO | ECHOK | ICANON;
    $noecho   = $oterm & ~$echo;
    sub cbreak {
        $term->setlflag($noecho);
        $term->setcc(VTIME, 1);
        $term->setattr($fd_stdin, TCSANOW);
    }

    sub cooked {
        $term->setlflag($oterm);
        $term->setcc(VTIME, 0);
        $term->setattr($fd_stdin, TCSANOW);
    }
    sub getone {
        my $key = "";
        cbreak();
        sysread(STDIN, $key, 1);
        cooked();
        return $key;
    }
}
END { cooked() }
The POSIX module's manpage provides a complete listing of which functions and constants it exports. It has so many that you'll often wind up importing only a subset of them, such as ":sys_wait_h", ":sys_stat_h", or ":termios_h". An example of blocking signals with the POSIX module is given in Chapter 16, "Interprocess Communication".



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.


HIVE: All information for read only. Please respect copyright!
Hosted by hive КГБ: Киевская городская библиотека