DESCRIPTION

The POSIX module permits you to access all (or nearly all) the standard POSIX 1003.1 identifiers. Many of these identifiers have been given Perl-ish interfaces.

Everything is exported by default with the exception of any POSIX functions with the same name as a built-in Perl function, such as abs, alarm, rmdir, write, etc.., which will be exported only if you ask for them explicitly. This is an unfortunate backwards compatibility feature. You can stop the exporting by saying use POSIX () and then use the fully qualified names (ie. POSIX::SEEK_END), or by giving an explicit import list. If you do neither, and opt for the default, use POSIX; has to import 553 symbols.

This document gives a condensed list of the features available in the POSIX module. Consult your operating system's manpages for general information on most features. Consult perlfunc for functions which are noted as being identical to Perl's builtin functions.

The first section describes POSIX functions from the 1003.1 specification. The second section describes some classes for signal objects, TTY objects, and other miscellaneous objects. The remaining sections list various constants and macros in an organization which roughly follows IEEE Std 1003.1b-1993.

CAVEATS

A few functions are not implemented because they are C specific. If you attempt to call these, they will 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() call will elicit the message "setjmp() is C-specific: use eval {} instead".

Furthermore, some evil vendors will claim 1003.1 compliance, but in fact are not so: they will not pass the PCTS (POSIX Compliance Test Suites). For example, one vendor may not define EDEADLK, or the semantics of the errno values set by open(2) might not be quite right. Perl does not attempt to verify POSIX compliance. That means you can currently successfully say "use POSIX", and then later in your program you find that your vendor has been lax and there's no usable ICANON macro after all. This could be construed to be a bug.

FUNCTIONS

_exit

This is identical to the C function _exit(). It exits the program immediately which means among other things buffered I/O is not flushed.

Note that when using threads and in Linux this is not a good way to exit a thread because in Linux processes and threads are kind of the same thing (Note: while this is the situation in early 2003 there are projects under way to have threads with more POSIXly semantics in Linux). If you want not to return from a thread, detach the thread.

abort

This is identical to the C function abort(). It terminates the process with a SIGABRT signal unless caught by a signal handler or if the handler does not return normally (it e.g. does a longjmp).

abs

This is identical to Perl's builtin abs() function, returning the absolute value of its numerical argument.

This is identical to Perl's builtin getppid() function for returning the process identifier of the parent process of the current process , see "getppid" in perlfunc.

getpwnam

This is identical to Perl's builtin getpwnam() function for returning user entries by user names, see "getpwnam" in perlfunc.

getpwuid

This is identical to Perl's builtin getpwuid() function for returning user entries by user identifiers, see "getpwuid" in perlfunc.

gets

Returns one line from STDIN, similar to <>, also known as the readline() function, see "readline" in perlfunc.

NOTE: if you have C programs that still use gets(), be very afraid. The gets() function is a source of endless grief because it has no buffer overrun checks. It should never be used. The fgets() function should be preferred instead.

This is identical to Perl's builtin gmtime() function for converting seconds since the epoch to a date in Greenwich Mean Time, see "gmtime" in perlfunc.

isalnum

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isalnum. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:alnum:]]/ construct instead, or possibly the /\w/ construct.

isalpha

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isalpha. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:alpha:]]/ construct instead.

isatty

Returns a boolean indicating whether the specified filehandle is connected to a tty. Similar to the -t operator, see "-X" in perlfunc.

iscntrl

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered iscntrl. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:cntrl:]]/ construct instead.

isdigit

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isdigit (unlikely, but still possible). Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:digit:]]/ construct instead, or the /\d/ construct.

isgraph

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isgraph. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:graph:]]/ construct instead.

islower

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered islower. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:lower:]]/ construct instead. Do not use /[a-z]/.

isprint

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isprint. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:print:]]/ construct instead.

ispunct

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered ispunct. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:punct:]]/ construct instead.

isspace

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isspace. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:space:]]/ construct instead, or the /\s/ construct. (Note that /\s/ and /[[:space:]]/ are slightly different in that /[[:space:]]/ can normally match a vertical tab, while /\s/ does not.)

isupper

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isupper. Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:upper:]]/ construct instead. Do not use /[A-Z]/.

isxdigit

This is identical to the C function, except that it can apply to a single character or to a whole string. Note that locale settings may affect what characters are considered isxdigit (unlikely, but still possible). Does not work on Unicode characters code point 256 or higher. Consider using regular expressions and the /[[:xdigit:]]/ construct instead, or simply /[0-9a-f]/i.

kill

This is identical to Perl's builtin kill() function for sending signals to processes (often to terminate them), see "kill" in perlfunc.

labs

(For returning absolute values of long integers.) labs() is C-specific, see "abs" in perlfunc instead.

lchown

This is identical to the C function, except the order of arguments is consistent with Perl's builtin chown() with the added restriction of only one path, not an list of paths. Does the same thing as the chown() function but changes the owner of a symbolic link instead of the file the symbolic link points to.

ldexp

This is identical to the C function ldexp() for multiplying floating point numbers with powers of two.

$x_quadrupled = POSIX::ldexp($x, 2);

ldiv

(For computing dividends of long integers.) ldiv() is C-specific, use / and int() instead.

link

This is identical to Perl's builtin link() function for creating hard links into files, see "link" in perlfunc.

localeconv

Get numeric formatting information. Returns a reference to a hash containing the current locale formatting values.

Here is how to query the database for the de (Deutsch or German) locale.

The month (mon), weekday (wday), and yearday (yday) begin at zero. I.e. January is 0, not 1; Sunday is 0, not 1; January 1st is 0, not 1. The year (year) is given in years since 1900. I.e. The year 1995 is 95; the year 2001 is 101. Consult your system's mktime() manpage for details about these and the other arguments.

This is similar to the C function nice(), for changing the scheduling preference of the current process. Positive arguments mean more polite process, negative values more needy process. Normal user processes can only be more polite.

The following will determine the maximum length of the longest allowable pathname on the filesystem which holds /var.

$path_max = POSIX::pathconf( "/var", &POSIX::_PC_PATH_MAX );

Returns undef on failure.

pause

This is similar to the C function pause(), which suspends the execution of the current process until a signal is received.

Returns undef on failure.

perror

This is identical to the C function perror(), which outputs to the standard error stream the specified message followed by ": " and the current error string. Use the warn() function and the $! variable instead, see "warn" in perlfunc and "$ERRNO" in perlvar.

pipe

Create an interprocess channel. This returns file descriptors like those returned by POSIX::open.

This is identical to Perl's builtin rewinddir() function for rewinding directory entry streams, see "rewinddir" in perlfunc.

rmdir

This is identical to Perl's builtin rmdir() function for removing (empty) directories, see "rmdir" in perlfunc.

scanf

scanf() is C-specific, use <> and regular expressions instead, see perlre.

setgid

Sets the real group identifier and the effective group identifier for this process. Similar to assigning a value to the Perl's builtin $) variable, see "$EGID" in perlvar, except that the latter will change only the real user identifier, and that the setgid() uses only a single numeric argument, as opposed to a space-separated list of numbers.

The following will set the traditional UNIX system locale behavior (the second argument "C").

$loc = setlocale( LC_ALL, "C" );

The following will query the current LC_CTYPE category. (No second argument means 'query'.)

$loc = setlocale( LC_CTYPE );

The following will set the LC_CTYPE behaviour according to the locale environment variables (the second argument ""). Please see your systems setlocale(3) documentation for the locale environment variables' meaning or consult perllocale.

$loc = setlocale( LC_CTYPE, "" );

The following will set the LC_COLLATE behaviour to Argentinian Spanish. NOTE: The naming and availability of locales depends on your operating system. Please consult perllocale for how to find out which locales are available in your system.

$loc = setlocale( LC_COLLATE, "es_AR.ISO8859-1" );

setpgid

This is similar to the C function setpgid() for setting the process group identifier of the current process.

Returns undef on failure.

setsid

This is identical to the C function setsid() for setting the session identifier of the current process.

setuid

Sets the real user identifier and the effective user identifier for this process. Similar to assigning a value to the Perl's builtin $< variable, see "$UID" in perlvar, except that the latter will change only the real user identifier.

sigaction

Detailed signal management. This uses POSIX::SigAction objects for the action and oldaction arguments (the oldaction can also be just a hash reference). Consult your system's sigaction manpage for details, see also POSIX::SigRt.

Synopsis:

sigaction(signal, action, oldaction = 0)

Returns undef on failure. The signal must be a number (like SIGHUP), not a string (like "SIGHUP"), though Perl does try hard to understand you.

If you use the SA_SIGINFO flag, the signal handler will in addition to the first argument, the signal name, also receive a second argument, a hash reference, inside which are the following keys with the following semantics, as defined by POSIX/SUSv3:

signo the signal number
errno the error number
code if this is zero or less, the signal was sent by
a user process and the uid and pid make sense,
otherwise the signal was sent by the kernel

The following are also defined by POSIX/SUSv3, but unfortunately not very widely implemented:

pid the process id generating the signal
uid the uid of the process id generating the signal
status exit value or signal for SIGCHLD
band band event for SIGPOLL

A third argument is also passed to the handler, which contains a copy of the raw binary contents of the siginfo structure: if a system has some non-POSIX fields, this third argument is where to unpack() them from.

Note that not all siginfo values make sense simultaneously (some are valid only for certain signals, for example), and not all values make sense from Perl perspective, you should to consult your system's sigaction and possibly also siginfo documentation.

This is identical to Perl's builtin sin() function for returning the sine of the numerical argument, see "sin" in perlfunc. See also Math::Trig.

sinh

This is identical to the C function sinh() for returning the hyperbolic sine of the numerical argument. See also Math::Trig.

sleep

This is functionally identical to Perl's builtin sleep() function for suspending the execution of the current for process for certain number of seconds, see "sleep" in perlfunc. There is one significant difference, however: POSIX::sleep() returns the number of unslept seconds, while the CORE::sleep() returns the number of slept seconds.

sprintf

This is similar to Perl's builtin sprintf() function for returning a string that has the arguments formatted as requested, see "sprintf" in perlfunc.

sqrt

This is identical to Perl's builtin sqrt() function. for returning the square root of the numerical argument, see "sqrt" in perlfunc.

The month (mon), weekday (wday), and yearday (yday) begin at zero. I.e. January is 0, not 1; Sunday is 0, not 1; January 1st is 0, not 1. The year (year) is given in years since 1900. I.e., the year 1995 is 95; the year 2001 is 101. Consult your system's strftime() manpage for details about these and the other arguments.

If you want your code to be portable, your format (fmt) argument should use only the conversion specifiers defined by the ANSI C standard (C89, to play safe). These are aAbBcdHIjmMpSUwWxXyYZ%. But even then, the results of some of the conversion specifiers are non-portable. For example, the specifiers aAbBcpZ change according to the locale settings of the user, and both how to set locales (the locale names) and what output to expect are non-standard. The specifier c changes according to the timezone settings of the user and the timezone computation rules of the operating system. The Z specifier is notoriously unportable since the names of timezones are non-standard. Sticking to the numeric specifiers is the safest route.

The given arguments are made consistent as though by calling mktime() before calling your system's strftime() function, except that the isdst value is not affected.

String to double translation. Returns the parsed number and the number of characters in the unparsed portion of the string. Truly POSIX-compliant systems set $! ($ERRNO) to indicate a translation error, so clear $! before calling strtod. However, non-POSIX systems may not check for overflow, and therefore will never set $!.

String to (long) integer translation. Returns the parsed number and the number of characters in the unparsed portion of the string. Truly POSIX-compliant systems set $! ($ERRNO) to indicate a translation error, so clear $! before calling strtol. However, non-POSIX systems may not check for overflow, and therefore will never set $!.

strtol should respect any POSIX setlocale() settings.

To parse a string $str as a number in some base $base use

$! = 0;
($num, $n_unparsed) = POSIX::strtol($str, $base);

The base should be zero or between 2 and 36, inclusive. When the base is zero or omitted strtol will use the string itself to determine the base: a leading "0x" or "0X" means hexadecimal; a leading "0" means octal; any other leading characters mean decimal. Thus, "1234" is parsed as a decimal number, "01234" as an octal number, and "0x1234" as a hexadecimal number.

This is identical to the C function tan(), returning the tangent of the numerical argument. See also Math::Trig.

tanh

This is identical to the C function tanh(), returning the hyperbolic tangent of the numerical argument. See also Math::Trig.

tcdrain

This is similar to the C function tcdrain() for draining the output queue of its argument stream.

Returns undef on failure.

tcflow

This is similar to the C function tcflow() for controlling the flow of its argument stream.

Returns undef on failure.

tcflush

This is similar to the C function tcflush() for flushing the I/O buffers of its argument stream.

Returns undef on failure.

tcgetpgrp

This is identical to the C function tcgetpgrp() for returning the process group identifier of the foreground process group of the controlling terminal.

tcsendbreak

This is similar to the C function tcsendbreak() for sending a break on its argument stream.

Returns undef on failure.

tcsetpgrp

This is similar to the C function tcsetpgrp() for setting the process group identifier of the foreground process group of the controlling terminal.

Returns undef on failure.

time

This is identical to Perl's builtin time() function for returning the number of seconds since the epoch (whatever it is for the system), see "time" in perlfunc.

times

The times() function returns elapsed realtime since some point in the past (such as system startup), user and system times for this process, and user and system times used by child processes. All times are returned in clock ticks.

For security reasons, which are probably detailed in your system's documentation for the C library tmpnam() function, this interface should not be used; instead see File::Temp.

tolower

This is identical to the C function, except that it can apply to a single character or to a whole string. Consider using the lc() function, see "lc" in perlfunc, or the equivalent \L operator inside doublequotish strings.

toupper

This is identical to the C function, except that it can apply to a single character or to a whole string. Consider using the uc() function, see "uc" in perlfunc, or the equivalent \U operator inside doublequotish strings.

ttyname

This is identical to the C function ttyname() for returning the name of the current terminal.

tzname

Retrieves the time conversion information from the tzname variable.

POSIX::tzset();
($std, $dst) = POSIX::tzname();

tzset

This is identical to the C function tzset() for setting the current timezone based on the environment variable TZ, to be used by ctime(), localtime(), mktime(), and strftime() functions.

umask

This is identical to Perl's builtin umask() function for setting (and querying) the file creation permission mask, see "umask" in perlfunc.

uname

Get name of current operating system.

($sysname, $nodename, $release, $version, $machine) = POSIX::uname();

Note that the actual meanings of the various fields are not that well standardized, do not expect any great portability. The $sysname might be the name of the operating system, the $nodename might be the name of the host, the $release might be the (major) release number of the operating system, the $version might be the (minor) release number of the operating system, and the $machine might be a hardware identifier. Maybe.

CLASSES

POSIX::SigAction

new

Creates a new POSIX::SigAction object which corresponds to the C struct sigaction. This object will be destroyed automatically when it is no longer needed. The first parameter is the handler, a sub reference. The second parameter is a POSIX::SigSet object, it defaults to the empty set. The third parameter contains the sa_flags, it defaults to 0.

This POSIX::SigAction object is intended for use with the POSIX::sigaction() function.

handler

mask

flags

accessor functions to get/set the values of a SigAction object.

$sigset = $sigaction->mask;
$sigaction->flags(&POSIX::SA_RESTART);

safe

accessor function for the "safe signals" flag of a SigAction object; see perlipc for general information on safe (a.k.a. "deferred") signals. If you wish to handle a signal safely, use this accessor to set the "safe" flag in the POSIX::SigAction object:

$sigaction->safe(1);

You may also examine the "safe" flag on the output action object which is filled in when given as the third parameter to POSIX::sigaction():

POSIX::SigRt

%SIGRT

A hash of the POSIX realtime signal handlers. It is an extension of the standard %SIG, the $POSIX::SIGRT{SIGRTMIN} is roughly equivalent to $SIG{SIGRTMIN}, but the right POSIX moves (see below) are made with the POSIX::SigSet and POSIX::sigaction instead of accessing the %SIG.

You can set the %POSIX::SIGRT elements to set the POSIX realtime signal handlers, use delete and exists on the elements, and use scalar on the %POSIX::SIGRT to find out how many POSIX realtime signals there are available (SIGRTMAX - SIGRTMIN + 1, the SIGRTMAX is a valid POSIX realtime signal).

The flags default to zero, if you want something different you can either use local on $POSIX::SigRt::SIGACTION_FLAGS, or you can derive from POSIX::SigRt and define your own new() (the tied hash STORE method of the %SIGRT calls new($rtsig, $handler, $SIGACTION_FLAGS), where the $rtsig ranges from zero to SIGRTMAX - SIGRTMIN + 1).

Just as with any signal, you can use sigaction($rtsig, undef, $oa) to retrieve the installed signal handler (or, rather, the signal action).

NOTE: whether POSIX realtime signals really work in your system, or whether Perl has been compiled so that it works with them, is outside of this discussion.

SIGRTMIN

Return the minimum POSIX realtime signal number available, or undef if no POSIX realtime signals are available.

SIGRTMAX

Return the maximum POSIX realtime signal number available, or undef if no POSIX realtime signals are available.

POSIX::SigSet

new

Create a new SigSet object. This object will be destroyed automatically when it is no longer needed. Arguments may be supplied to initialize the set.

POSIX::Termios

new

Create a new Termios object. This object will be destroyed automatically when it is no longer needed. A Termios object corresponds to the termios C struct. new() mallocs a new one, getattr() fills it from a file descriptor, and setattr() sets a file descriptor's parameters to match Termios' contents.

$termios = POSIX::Termios->new;

getattr

Get terminal control attributes.

Obtain the attributes for stdin.

$termios->getattr( 0 ) # Recommended for clarity.
$termios->getattr()

Obtain the attributes for stdout.

$termios->getattr( 1 )

Returns undef on failure.

getcc

Retrieve a value from the c_cc field of a termios object. The c_cc field is an array so an index must be specified.

$c_cc[1] = $termios->getcc(1);

getcflag

Retrieve the c_cflag field of a termios object.

$c_cflag = $termios->getcflag;

getiflag

Retrieve the c_iflag field of a termios object.

$c_iflag = $termios->getiflag;

getispeed

Retrieve the input baud rate.

$ispeed = $termios->getispeed;

getlflag

Retrieve the c_lflag field of a termios object.

$c_lflag = $termios->getlflag;

getoflag

Retrieve the c_oflag field of a termios object.

$c_oflag = $termios->getoflag;

getospeed

Retrieve the output baud rate.

$ospeed = $termios->getospeed;

setattr

Set terminal control attributes.

Set attributes immediately for stdout.

$termios->setattr( 1, &POSIX::TCSANOW );

Returns undef on failure.

setcc

Set a value in the c_cc field of a termios object. The c_cc field is an array so an index must be specified.