ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH

Reference
ActivePerl 5.8
Modules
ActivePerl
ActiveState
Algorithm
AnyDBM File
Apache
Archive
Attribute
AutoLoader
AutoSplit
B
Benchmark
Bit
BSD
Bundle
ByteLoader
Carp
CGI
Class
Compress
Config
CORE
CPAN
Crypt
Cwd
Data
Date
DB
DBD
DBI
DBM Filter
DB File
Devel
Digest
DirHandle
Dumpvalue
DynaLoader
Encode
English
Env
Errno
Exporter
ExtUtils
Fatal
Fcntl
File
FileCache
FileHandle
Filter
FindBin
Font
GD
Getopt
Hash
HTML
HTTP
I18N
IO
IPC
JSON
List
Locale
LWP
lwpcook
lwptut
Mac
MacPerl
Math
MD5
Memoize
MIME
MLDBM
Module
NDBM File
Net
NEXT
O
Opcode
Oraperl
perl5db
PerlEx
PerlIO
perllocal
Pod
POSIX
Roadmap
Safe
Scalar
SDBM File
Search
SelectSaver
SelfLoader
Shell
SOAP
Socket
SQL
Storable
Sub
Switch
Symbol
Sys
TASKS
Tcl
Term
Test
Text
Thread
Tie
Time
Tk
Tkx
UDDI
Unicode
UNIVERSAL
URI
User
Win32
Win32API
Win32CORE
WWW
XML
XMLRPC
XSLoader
YAML

MyASPN >> Reference >> ActivePerl 5.8 >> Modules
ActivePerl 5.8 documentation

NAME

POSIX - Perl interface to IEEE Std 1003.1


SYNOPSIS

    use POSIX;
    use POSIX qw(setsid);
    use POSIX qw(:errno_h :fcntl_h);
    printf "EINTR is %d\n", EINTR;
    $sess_id = POSIX::setsid();
    $fd = POSIX::open($path, O_CREAT|O_EXCL|O_WRONLY, 0644);
        # note: that's a filedescriptor, *NOT* a filehandle


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).

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 the perlfunc manpage 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.


NOTE

The POSIX module is probably the most complex Perl module supplied with the standard distribution. It incorporates autoloading, namespace games, and dynamic loading of code that's in Perl, C, or both. It's a great source of wisdom.


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.

access

Determines the accessibility of a file.

        if( POSIX::access( "/", &POSIX::R_OK ) ){
                print "have read permission\n";
        }

Returns undef on failure. Note: do not use access() for security purposes. Between the access() call and the operation you are preparing for the permissions might change: a classic race condition.

acos

This is identical to the C function acos(), returning the arcus cosine of its numerical argument. See also the Math::Trig manpage.

alarm

This is identical to Perl's builtin alarm() function, either for arming or disarming the SIGARLM timer.

asctime

This is identical to the C function asctime(). It returns a string of the form

        "Fri Jun  2 18:22:13 2000\n\0"

and it is called thusly

        $asctime = asctime($sec, $min, $hour, $mday, $mon, $year,
                           $wday, $yday, $isdst);

The $mon is zero-based: January equals 0. The $year is 1900-based: 2001 equals 101. The $wday, $yday, and $isdst default to zero (and the first two are usually ignored anyway).

asin

This is identical to the C function asin(), returning the arcus sine of its numerical argument. See also the Math::Trig manpage.

assert

Unimplemented, but you can use die in the perlfunc manpage and the the Carp manpage module to achieve similar things.

atan

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

atan2

This is identical to Perl's builtin atan2() function, returning the arcus tangent defined by its two numerical arguments, the y coordinate and the x coordinate. See also the Math::Trig manpage.

atexit

atexit() is C-specific: use END {} instead, see the perlsub manpage.

atof

atof() is C-specific. Perl converts strings to numbers transparently. If you need to force a scalar to a number, add a zero to it.

atoi

atoi() is C-specific. Perl converts strings to numbers transparently. If you need to force a scalar to a number, add a zero to it. If you need to have just the integer part, see int in the perlfunc manpage.

atol

atol() is C-specific. Perl converts strings to numbers transparently. If you need to force a scalar to a number, add a zero to it. If you need to have just the integer part, see int in the perlfunc manpage.

bsearch

bsearch() not supplied. For doing binary search on wordlists, see the Search::Dict manpage.

calloc

calloc() is C-specific. Perl does memory management transparently.

ceil

This is identical to the C function ceil(), returning the smallest integer value greater than or equal to the given numerical argument.

chdir

This is identical to Perl's builtin chdir() function, allowing one to change the working (default) directory, see chdir in the perlfunc manpage.

chmod

This is identical to Perl's builtin chmod() function, allowing one to change file and directory permissions, see chmod in the perlfunc manpage.

chown

This is identical to Perl's builtin chown() function, allowing one to change file and directory owners and groups, see chown in the perlfunc manpage.

clearerr

Use the method IO::Handle::clearerr() instead, to reset the error state (if any) and EOF state (if any) of the given stream.

clock

This is identical to the C function clock(), returning the amount of spent processor time in microseconds.

close

Close the file. This uses file descriptors such as those obtained by calling POSIX::open.

        $fd = POSIX::open( "foo", &POSIX::O_RDONLY );
        POSIX::close( $fd );

Returns undef on failure.

See also close in the perlfunc manpage.

closedir

This is identical to Perl's builtin closedir() function for closing a directory handle, see closedir in the perlfunc manpage.

cos

This is identical to Perl's builtin cos() function, for returning the cosine of its numerical argument, see cos in the perlfunc manpage. See also the Math::Trig manpage.

cosh

This is identical to the C function cosh(), for returning the hyperbolic cosine of its numeric argument. See also the Math::Trig manpage.

creat

Create a new file. This returns a file descriptor like the ones returned by POSIX::open. Use POSIX::close to close the file.

        $fd = POSIX::creat( "foo", 0611 );
        POSIX::close( $fd );

See also sysopen in the perlfunc manpage and its O_CREAT flag.

ctermid

Generates the path name for the controlling terminal.

        $path = POSIX::ctermid();
ctime

This is identical to the C function ctime() and equivalent to asctime(localtime(...)), see asctime and localtime.

cuserid

Get the login name of the owner of the current process.

        $name = POSIX::cuserid();
difftime

This is identical to the C function difftime(), for returning the time difference (in seconds) between two times (as returned by time()), see time.

div

div() is C-specific, use int in the perlfunc manpage on the usual / division and the modulus %.

dup

This is similar to the C function dup(), for duplicating a file descriptor.

This uses file descriptors such as those obtained by calling POSIX::open.

Returns undef on failure.

dup2

This is similar to the C function dup2(), for duplicating a file descriptor to an another known file descriptor.

This uses file descriptors such as those obtained by calling POSIX::open.

Returns undef on failure.

errno

Returns the value of errno.

        $errno = POSIX::errno();

This identical to the numerical values of the $!, see $ERRNO in the perlvar manpage.

execl

execl() is C-specific, see exec in the perlfunc manpage.

execle

execle() is C-specific, see exec in the perlfunc manpage.

execlp

execlp() is C-specific, see exec in the perlfunc manpage.

execv

execv() is C-specific, see exec in the perlfunc manpage.

execve

execve() is C-specific, see exec in the perlfunc manpage.

execvp

execvp() is C-specific, see exec in the perlfunc manpage.

exit

This is identical to Perl's builtin exit() function for exiting the program, see exit in the perlfunc manpage.

exp

This is identical to Perl's builtin exp() function for returning the exponent (e-based) of the numerical argument, see exp in the perlfunc manpage.

fabs

This is identical to Perl's builtin abs() function for returning the absolute value of the numerical argument, see abs in the perlfunc manpage.

fclose

Use method IO::Handle::close() instead, or see close in the perlfunc manpage.

fcntl

This is identical to Perl's builtin fcntl() function, see fcntl in the perlfunc manpage.

fdopen

Use method IO::Handle::new_from_fd() instead, or see open in the perlfunc manpage.

feof

Use method IO::Handle::eof() instead, or see eof in the perlfunc manpage.

ferror

Use method IO::Handle::error() instead.

fflush

Use method IO::Handle::flush() instead. See also $OUTPUT_AUTOFLUSH in the perlvar manpage.

fgetc

Use method IO::Handle::getc() instead, or see read in the perlfunc manpage.

fgetpos

Use method IO::Seekable::getpos() instead, or see seek in the L manpage.

fgets

Use method IO::Handle::gets() instead. Similar to <>, also known as readline in the perlfunc manpage.

fileno

Use method IO::Handle::fileno() instead, or see fileno in the perlfunc manpage.

floor

This is identical to the C function floor(), returning the largest integer value less than or equal to the numerical argument.

fmod

This is identical to the C function fmod().

        $r = fmod($x, $y);

It returns the remainder $r = $x - $n*$y, where $n = trunc($x/$y). The $r has the same sign as $x and magnitude (absolute value) less than the magnitude of $y.

fopen

Use method IO::File::open() instead, or see open in the perlfunc manpage.

fork

This is identical to Perl's builtin fork() function for duplicating the current process, see fork in the perlfunc manpage and the perlfork manpage if you are in Windows.

fpathconf

Retrieves the value of a configurable limit on a file or directory. This uses file descriptors such as those obtained by calling POSIX::open.

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

        $fd = POSIX::open( "/var/foo", &POSIX::O_RDONLY );
        $path_max = POSIX::fpathconf( $fd, &POSIX::_PC_PATH_MAX );

Returns undef on failure.

fprintf

fprintf() is C-specific, see printf in the perlfunc manpage instead.

fputc

fputc() is C-specific, see print in the perlfunc manpage instead.

fputs

fputs() is C-specific, see print in the perlfunc manpage instead.

fread

fread() is C-specific, see read in the perlfunc manpage instead.

free

free() is C-specific. Perl does memory management transparently.

freopen

freopen() is C-specific, see open in the perlfunc manpage instead.

frexp

Return the mantissa and exponent of a floating-point number.

        ($mantissa, $exponent) = POSIX::frexp( 1.234e56 );
fscanf

fscanf() is C-specific, use <> and regular expressions instead.

fseek

Use method IO::Seekable::seek() instead, or see seek in the perlfunc manpage.

fsetpos

Use method IO::Seekable::setpos() instead, or seek seek in the perlfunc manpage.

fstat

Get file status. This uses file descriptors such as those obtained by calling POSIX::open. The data returned is identical to the data from Perl's builtin stat function.

        $fd = POSIX::open( "foo", &POSIX::O_RDONLY );
        @stats = POSIX::fstat( $fd );
fsync

Use method IO::Handle::sync() instead.

ftell

Use method IO::Seekable::tell() instead, or see tell in the perlfunc manpage.

fwrite

fwrite() is C-specific, see print in the perlfunc manpage instead.

getc

This is identical to Perl's builtin getc() function, see getc in the perlfunc manpage.

getchar

Returns one character from STDIN. Identical to Perl's getc(), see getc in the perlfunc manpage.

getcwd

Returns the name of the current working directory. See also the Cwd manpage.

getegid

Returns the effective group identifier. Similar to Perl' s builtin variable $(, see $EGID in the perlvar manpage.

getenv

Returns the value of the specified environment variable. The same information is available through the %ENV array.

geteuid

Returns the effective user identifier. Identical to Perl's builtin $> variable, see $EUID in the perlvar manpage.

getgid

Returns the user's real group identifier. Similar to Perl's builtin variable $), see $GID in the perlvar manpage.

getgrgid

This is identical to Perl's builtin getgrgid() function for returning group entries by group identifiers, see getgrgid in the perlfunc manpage.

getgrnam

This is identical to Perl's builtin getgrnam() function for returning group entries by group names, see getgrnam in the perlfunc manpage.

getgroups

Returns the ids of the user's supplementary groups. Similar to Perl's builtin variable $), see $GID in the perlvar manpage.

getlogin

This is identical to Perl's builtin getlogin() function for returning the user name associated with the current session, see getlogin in the perlfunc manpage.

getpgrp

This is identical to Perl's builtin getpgrp() function for returning the process group identifier of the current process, see getpgrp in the perlfunc manpage.

getpid

Returns the process identifier. Identical to Perl's builtin variable $$, see $PID in the perlvar manpage.

getppid

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 the perlfunc manpage.

getpwnam

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

getpwuid

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

gets

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

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.

getuid

Returns the user's identifier. Identical to Perl's builtin $< variable, see $UID in the perlvar manpage.

gmtime

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 the perlfunc manpage.

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 the perlfunc manpage.

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 the perlfunc manpage.

labs

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

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 the perlfunc manpage.

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.

        $loc = POSIX::setlocale( &POSIX::LC_ALL, "de" );
        print "Locale = $loc\n";
        $lconv = POSIX::localeconv();
        print "decimal_point    = ", $lconv->{decimal_point},   "\n";
        print "thousands_sep    = ", $lconv->{thousands_sep},   "\n";
        print "grouping = ", $lconv->{grouping},        "\n";
        print "int_curr_symbol  = ", $lconv->{int_curr_symbol}, "\n";
        print "currency_symbol  = ", $lconv->{currency_symbol}, "\n";
        print "mon_decimal_point = ", $lconv->{mon_decimal_point}, "\n";
        print "mon_thousands_sep = ", $lconv->{mon_thousands_sep}, "\n";
        print "mon_grouping     = ", $lc