Peta::FFI::Libc#

📦 std

Call common libc functions from Perl without writing a single line of FFI glue.

Pre-baked libc bindings: each function here calls the C library directly, so there is no handle to open and no signature string to write. Reach for Peta::FFI when you need a function this module does not cover.

use Peta::FFI::Libc qw(getpid strlen strerror gethostname uname access R_OK);

print getpid() == $$ ? "getpid is \$\$\n" : "?\n";
print strlen("hello"), " ", strerror(2), "\n";   # 5 No such file or directory
my ($sys, $node) = uname();
print "$sys on $node\n" if gethostname() eq $node;
print access("/etc/passwd", R_OK) == 0 ? "readable\n" : "not readable: $!\n";

The functions keep C’s conventions: the ones that report success return 0 for it and -1 for failure, with $! set to the C errno. Nothing is exported by default; name what you want.

Function

Returns

getpid, getppid

the process id, the parent’s

getuid, getgid

the real user and group id

geteuid, getegid

the effective user and group id

strlen($s)

the length of $s; a NUL byte in it dies

strerror($errno)

the system’s message for an error number

getenv($name)

the value, or undef when unset

setenv($name, $value, $over)

0 / -1; $over false keeps an existing value

unsetenv($name)

0 / -1

abs($n), labs($n)

the absolute value, as an int / a long

sleep($s), usleep($us)

seconds left unslept / 0 or -1

gethostname()

the host name

uname()

(sysname, nodename, release, version, machine)

access($path, $mode)

0 / -1, $mode an OR of F_OK, R_OK, W_OK, X_OK

unlink, rmdir($path)

0 / -1

mkdir($path, $mode)

0 / -1

chmod($path, $mode)

0 / -1

chown($path, $uid, $gid)

0 / -1

time()

seconds since the epoch

clock()

processor time used, in CLOCKS_PER_SEC units

Peta::FFI::Libc is pperl’s own module, version 0.2.0.