The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

#!./perl -w
use strict;
use Config;
plan(skip_all => "POSIX is unavailable")
unless $Config{extensions} =~ /\bPOSIX\b/;
require POSIX;
my %valid;
my @all;
my $argc = 0;
for my $list ([qw(errno fork getchar getegid geteuid getgid getgroups getlogin
getpgrp getpid getppid gets getuid time wait)],
[qw(abs alarm assert chdir closedir cos exit exp fabs fstat getc
getenv getgrgid getgrnam getpwnam getpwuid gmtime isatty
localtime log opendir raise readdir remove rewind rewinddir
rmdir sin sleep sqrt stat strerror system
umask unlink)],
[qw(atan2 chmod creat kill link mkdir pow rename strstr waitpid)],
[qw(chown fcntl utime)]) {
$valid{$_} = $argc foreach @$list;
push @all, @$list;
++$argc;
}
my @try = 0 .. $argc - 1;
foreach my $func (sort @all) {
my $arg_pat = join ', ', ('[a-z]+') x $valid{$func};
my $expect = qr/\AUsage: POSIX::$func\($arg_pat\) at \(eval/;
foreach my $try (@try) {
next if $valid{$func} == $try;
my $call = "POSIX::$func(" . join(', ', 1 .. $try) . ')';
is(eval "$call; 1", undef, "$call fails");
like($@, $expect, "POSIX::$func for $try arguments gives expected error")
}
}
foreach my $func (qw(printf sprintf)) {
is(eval "POSIX::$func(); 1", undef, "POSIX::$func() fails");
like($@, qr/\AUsage: POSIX::$func\(pattern, args\.\.\.\) at \(eval/,
"POSIX::$func for 0 arguments gives expected error");
}
foreach my $func (qw(cos exp fabs log sin sqrt)) {
local $_ = 3;
is(
eval "POSIX::$func(); 1",
undef,
"POSIX::$func() fails; needs explicit argument"
);
}
{
my ($current_umask, $umask_is_now);
$current_umask = umask();
$umask_is_now = POSIX::umask($current_umask);
is($umask_is_now, $current_umask,
"POSIX::umask, when provided with current umask, returns same as builtin umask()");
is(
eval "$umask_is_now = POSIX::umask($current_umask); 1",
undef,
"POSIX::umask() fails; needs explicit argument"
);
}
done_testing();