Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

pperl

pperl is a next-generation Perl 5 platform written in Rust, targeting Perl 5.42+ compatibility.

Goals

  • Auto-Parallelization - Automatic parallel map, grep, for, while loops via Rayon work-stealing
  • JIT Compilation - Native code generation via Cranelift for hot loops (up to 76x faster than perl5)
  • Pure Perl Viability - Fast enough that XS becomes optional

Architecture Overview

flowchart LR
    subgraph Input
        A[Perl Source]
    end

    subgraph Parser
        B[Lexer] --> C[Parser]
        C --> D[AST]
    end

    subgraph Compiler
        D --> E[Codegen]
        E --> F[OpArena]
    end

    subgraph Runtime
        F --> G[Interpreter]
        G --> H[PP Dispatch]
    end

    subgraph Output
        H --> I[Result]
    end

    A --> B

Quick Start

# Run a script
pperl script.pl

# Run one-liner
pperl -e 'print "Hello, World!\n"'

# Check syntax only
pperl -c script.pl

Compatibility

pperl aims for high compatibility with Perl 5.42+. See Differences from Perl 5 for known incompatibilities.

Platform Support

  • Linux only (any architecture)
  • No Windows, macOS, or other platforms

Getting Started

Installation

Binaries will be available from a GitHub release page (TBD). That is also where to report issues via the tracker.

First Script

#!/usr/bin/env pperl
print "Hello from PetaPerl!\n";

Command Line Options

OptionDescription
-e 'code'Execute code (modern features always on)
-cCheck syntax only
-wEnable warnings
-vShow version
--no-jitDisable JIT compilation
--no-parallelDisable auto-parallelization
--p5Use p5 self-contained runtime (experimental)
--cacheEnable bytecode caching

See Running Scripts for the full option list.

Notes

  • Modern features (say, state, //, signatures) are always enabled — no use feature needed
  • XS modules are not supported; native Rust reimplementations are provided for common modules
  • -I is not supported; use PERL5LIB environment variable to add include paths

Running Scripts

PetaPerl’s pperl command executes Perl scripts with a familiar interface. Most perl5 options work identically.

Basic Execution

# Run a script
pperl script.pl

# Run with arguments
pperl script.pl arg1 arg2

# Execute one-liner
pperl -e 'say "Hello, World!"'

# Check syntax without running
pperl -c script.pl

Command-Line Options

Perl-Compatible Options

OptionDescriptionExample
-e 'code'Execute one-liner (modern features always on)pperl -e 'say "hi"'
-cCheck syntax onlypperl -c script.pl
-wEnable warningspperl -w script.pl
-vShow versionpperl -v
-VVerbose configurationpperl -V
-h, -?, --helpShow help messagepperl --help

Combined short flags:

pperl -wc script.pl  # Enable warnings + syntax check

PetaPerl-Specific Options

OptionDescriptionDefault
--stats, -sShow performance statistics (time, memory, ops)Disabled
--trace, -tEnable execution tracingDisabled
--timeout=SECSSet execution timeout in secondsNone
--p5Use P5 runtime (see below)Disabled

Choosing a Runtime

PetaPerl has two runtime engines. The default PP runtime uses safe, idiomatic Rust and supports JIT compilation and auto-parallelization. The P5 runtime (--p5) is a line-for-line Rust transliteration of perl5’s C interpreter — it matches perl5 performance out of the box but does not support JIT or parallelization.

Use --p5 when you need maximum compatibility with perl5’s edge-case behaviour or baseline performance without JIT. Use the default PP runtime (with JIT enabled) for compute-heavy workloads where parallelization and JIT provide significant speedups.

See Dual Runtime Architecture for technical details.

Performance Options

OptionDescriptionDefault
--no-jitDisable JIT compilationJIT enabled
--no-parallelDisable auto-parallelizationParallel enabled
--threads=NNumber of threads for parallelizationAll CPUs
--parallel-threshold=NMinimum iterations to parallelize100

Caching and Analysis Options

OptionDescription
--cacheEnable bytecode caching (~/.pperl/cache/)
--flushClear all bytecode caches and exit
--dump-optreeDump canonical op tree (don’t execute)
--from-json, -jRead op tree from stdin (B::PetaPerl JSON format)
--optree, -oRead op tree from stdin (B::Concise format)
--compare-bytecodeCompare op trees: perl5 backend vs native parser

Unsupported perl5 Options

These perl5 flags are not implemented:

OptionDescriptionWorkaround
-IAdd include pathUse PERL5LIB
-EExecute with featuresUse -e (features always on)
-M / -mLoad moduleUse use in code
-n / -pImplicit input loopWrite explicit loop
-lAuto line-endingUse chomp/say
-a / -FAutosplitUse split explicitly
-iIn-place editUse open/close
-dDebugger-
-xExtract script-
-TTaint mode-

Examples

One-Liners

Modern features (say, state, etc.) are always enabled.

# Print hello
pperl -e 'say "Hello!"'

# Process input
echo "hello" | pperl -e 'while (<>) { say uc($_) }'

# Math
pperl -e 'say 2 + 2'

# Generate data
pperl -e 'say join ",", 1..10'

Syntax Checking

# Check single file
pperl -c script.pl
# Output: script.pl syntax OK

# Check with warnings enabled
pperl -wc script.pl

Script Arguments

Arguments after the script name populate @ARGV.

pperl process.pl file1.txt file2.txt
# process.pl
for my $file (@ARGV) {
    say "Processing: $file";
}

Performance Statistics

pperl --stats script.pl

Output:

--- Performance Statistics ---
Wall-clock time: 0.042 seconds
Peak memory (RSS): 12.34 MB (12640 KB)
Ops executed: 1523 (36261 ops/sec)
------------------------------

Execution Tracing

See every op executed (debugging aid).

pperl --trace script.pl

Output shows:

[TRACE] NextState
[TRACE] Const("Hello")
[TRACE] Print

Parser Comparison

Compare native Rust parser output against a perl5-generated JSON op tree (analysis tool).

pperl --compare-bytecode script.pl

Shows differences in generated op trees. Useful for parser validation.

Op Tree Inspection

Dump canonical op tree representation without executing.

pperl --dump-optree script.pl

Shows internal op tree structure:

NextState(file="script.pl", line=1)
  → Const(sv="Hello")
    → Print
      → LeaveSub

Execution Model

Parser Pipeline

Source → Lexer → Parser → AST → Codegen → OpArena → Interpreter

Fast, pure Rust. No perl5 dependency at runtime.

JSON Op Tree Loading (Analysis Only)

Source → perl + B::PetaPerl → JSON → OpArena → Interpreter

For analysis and parser validation, pperl can load a perl5-generated op tree via --from-json or compare it against the native parser via --compare-bytecode. This is not used during normal execution.

Timeouts

By default, pperl has no execution timeout. Use --timeout=SECS to set one:

# No timeout (default)
pperl script.pl

# Set timeout (useful for untrusted code or testing)
pperl --timeout=60 script.pl
pperl --timeout=5 script.pl

The test harness sets its own timeout (default 5 seconds per test). This is separate from the --timeout flag.

Exit Codes

CodeMeaning
0Success
1Runtime error or compile error
2Command-line usage error

Environment Variables

VariableEffectDefault
PPERL_MAX_RECURSIONMaximum subroutine recursion depth1000
PPERL_CACHEBytecode cache directory-
PERL5LIBModule search path-

Example:

export PPERL_MAX_RECURSION=5000
pperl deeply_recursive.pl

export PPERL_CACHE=/tmp/pperl-cache
pperl --cache script.pl

Warnings

Enable warnings with -w or -W:

pperl -w script.pl

Warnings appear on stderr. Currently implemented:

  • Use of uninitialized value
  • Undefined subroutine call

Standard Streams

# Read stdin
while (my $line = <STDIN>) {
    print "Got: $line";
}

# Write stdout
print "Output\n";
say "Output with newline";

# Write stderr
warn "Warning message\n";

Redirection works normally:

pperl script.pl < input.txt > output.txt 2> errors.txt

Shebang Support

PetaPerl scripts support shebang for direct execution.

#!/usr/bin/env pperl
say "Hello from PetaPerl!";

Make executable and run:

chmod +x script.pl
./script.pl

Module Loading

use strict;
use warnings;
use Data::Dumper;

my $data = { foo => 42 };
print Dumper($data);

Module search path: same as perl5’s @INC. Set PERL5LIB to add directories.

Current limitations:

  • XS modules not supported (native modules only)
  • Some pragmas (strict, warnings) parsed but not fully enforced
  • Pure Perl modules work if they don’t use unsupported features

Differences from perl5

Always-Enabled Features

Modern Perl features are always available (no use feature needed):

  • say - print with newline
  • state - persistent lexical variables
  • // - defined-or operator
  • ~~ - smart match (partial)

Default Timeout

pperl has no default timeout (same as perl5). Use --timeout=SECS to set one for untrusted scripts.

Parser

pperl always uses the native Rust parser. There is no runtime fallback to perl5.

Debugging

Syntax Errors

pperl -c script.pl

Reports parse errors with line numbers:

Parse error: Expected ';' after statement
  at script.pl line 42

Runtime Errors

Errors show stack trace:

Runtime error: Undefined subroutine &main::foo
  called at script.pl line 10

Execution Tracing

pperl --trace script.pl

Shows every op executed. Very verbose. Useful for understanding execution flow.

Performance Tips

Benchmarking

time pperl script.pl

Or use --stats for detailed metrics:

pperl --stats script.pl

Performance Characteristics

PetaPerl’s performance varies by workload:

  • Interpreter fast paths: Common operations (integer arithmetic, assignment, comparison) run at or near perl5 speed
  • JIT-compiled loops: Arithmetic-heavy loops compile to native code via Cranelift, achieving up to 76x faster than perl5
  • JIT + parallelization: Embarrassingly parallel workloads on multi-core systems achieve up to 431x faster than perl5 (Mandelbrot 1000x1000)
  • String-heavy code: Comparable to perl5 with PvBuf optimizations
  • Module loading: Similar to perl5 for Pure Perl modules

Use --no-jit and --no-parallel to isolate interpreter-only performance.

Common Workflows

Script Development

# Edit script
vim script.pl

# Check syntax
pperl -c script.pl

# Run with warnings
pperl -w script.pl

# Debug with tracing (if needed)
pperl --trace script.pl

Testing

# Syntax check all scripts
find . -name '*.pl' -exec pperl -c {} \;

# Run test suite
pperl t/test.pl

Production Deployment

# Run production script (no timeout by default)
pperl production.pl

# Monitor performance
pperl --stats production.pl

Advanced Usage

Loading Op Tree from JSON (Analysis)

For parser validation and analysis:

# JSON format (B::PetaPerl) — reads from stdin
perl -MO=PetaPerl script.pl | pperl --from-json

# Short form
perl -MO=PetaPerl script.pl | pperl -j

This loads a perl5-generated op tree and executes it in the PetaPerl runtime, bypassing the native parser.

Comparing Parsers

Compare native parser output against a perl5-generated op tree:

pperl --compare-bytecode script.pl

Shows structural differences. Exit code 0 = equivalent, 1 = different.

Troubleshooting

“Can’t open perl script”

File doesn’t exist or wrong path.

pperl script.pl  # Must exist

“Undefined subroutine”

Sub not defined or not visible in current scope.

sub greet { say "hi" }
greet();  # OK

greeet();  # Error: Undefined subroutine

“Timeout expired”

Script ran longer than the --timeout value.

pperl --timeout=120 long_script.pl  # Increase timeout
pperl long_script.pl                # No timeout (default)

“Parse error”

Syntax error in script. Use -c to check:

pperl -c script.pl

“Is perl installed and in PATH?”

The --from-json/-j and --compare-bytecode analysis tools require system perl 5.42+ with B::PetaPerl installed. Normal execution does not require perl.

Version Information

pperl -v

Shows brief version and copyright.

pperl -V

Shows detailed configuration:

Summary of PetaPerl configuration:

  PetaPerl:
    version='0.1.0'
    binary='pperl'

  Runtime:
    backend=Rust interpreter + function-pointer dispatch
    jit=Cranelift (for/while loops)
    parallelism=Rayon (auto-parallel map/grep/for/while)

  Perl compatibility:
    parser=native Rust
    perl_required=no (5.42+ optional for --from-json/--compare-bytecode analysis)
    platforms=Linux only

Platform Support

PetaPerl runs on Linux only (any architecture).

Not supported: Windows, macOS, BSD, VMS, etc.

This simplification allows aggressive optimization for Linux-specific features.

Future Enhancements

Planned features:

  • Profiling output (--profile)
  • Extended JIT coverage (string operations, subroutine inlining)
  • Extended parallelization (more loop patterns, pipeline parallelism)

Track progress: https://gl.petatech.eu/petatech/peta-perl

perlvar - PetaPerl Predefined Variables

DESCRIPTION

This document describes the special variables supported by PetaPerl. For full Perl 5 documentation, see perldoc perlvar.

SPECIAL VARIABLES

General Variables

$_ - The Default Variable

The default input and pattern-searching space. Many functions operate on $_ when no argument is given.

for (1, 2, 3) {
    print;      # prints $_ implicitly
}

$_ = "hello";
print length;   # prints 5

Implementation: src/runtime/pp/globals.rs:95

@_ - Subroutine Arguments

Within a subroutine, @_ contains the arguments passed to that subroutine. Modifying elements of @_ modifies the caller’s variables (aliasing).

sub double {
    $_[0] *= 2;    # modifies caller's variable
}

my $x = 5;
double($x);
print $x;          # prints 10

Implementation: src/runtime/pp/sub.rs

Global Special Variables

$0 - Program Name

Contains the name of the program being executed.

print "Running: $0\n";

$$ - Process ID

The process ID of the Perl process running this script.

print "PID: $$\n";

$? - Child Error Status

The status returned by the last pipe close, backtick command, or system() call.

system("false");
print "Exit status: ", $? >> 8, "\n";

$@ - Eval Error

The error message from the last eval that failed.

eval { die "oops" };
print "Error: $@" if $@;

$! - OS Error

When used in numeric context, yields the current value of errno. In string context, yields the corresponding error string.

open(my $fh, '<', '/nonexistent') or print "Error: $!\n";

I/O Variables

$/ - Input Record Separator

The input record separator, newline by default. Setting to undef enables slurp mode.

{
    local $/;           # slurp mode
    my $content = <$fh>;
}

{
    local $/ = "";      # paragraph mode
    while (<$fh>) { ... }
}

$\ - Output Record Separator

Appended to the end of every print. Default is undef (nothing appended).

{
    local $\ = "\n";
    print "line1";      # outputs "line1\n"
    print "line2";      # outputs "line2\n"
}

$, - Output Field Separator

Printed between arguments to print. Default is undef.

{
    local $, = ", ";
    print "a", "b", "c";   # outputs "a, b, c"
}

$" - List Separator

Used when arrays are interpolated into strings. Default is a space.

my @arr = (1, 2, 3);
print "@arr\n";            # outputs "1 2 3"

{
    local $" = ",";
    print "@arr\n";        # outputs "1,2,3"
}

$| - Output Autoflush

If set to nonzero, forces a flush after every write on the currently selected output channel. Default is 0.

$| = 1;    # enable autoflush on STDOUT

$. - Current Line Number

The current line number of the last filehandle read.

while (<$fh>) {
    print "$.: $_";    # prints line number and content
}

$; - Subscript Separator

The subscript separator for multidimensional hash emulation. Default is "\034" (SUBSEP).

$hash{$x, $y} = $value;    # equivalent to $hash{"$x\034$y"}

$^T - Basetime

The time at which the program began running, in seconds since the epoch.

print "Started at: ", scalar localtime($^T), "\n";

$^W - Warning Flag

The current value of the warning switch. Set by -w.

$^W = 1;    # enable warnings

Regular Expression Variables

$& - Match String

The string matched by the last successful pattern match.

"hello world" =~ /wo\w+/;
print $&;      # prints "world"

$` - Prematch String

The string preceding what was matched by the last successful pattern match.

"hello world" =~ /wo\w+/;
print $`;      # prints "hello "

$' - Postmatch String

The string following what was matched by the last successful pattern match.

"hello world!" =~ /wo\w+/;
print $';      # prints "!"

$1, $2, … - Capture Groups

Contain the text matched by capture groups in the last pattern match.

"hello world" =~ /(\w+) (\w+)/;
print "$1, $2\n";    # prints "hello, world"

%+ - Named Captures

Hash containing the text matched by named capture groups.

"hello world" =~ /(?<first>\w+) (?<second>\w+)/;
print "$+{first}, $+{second}\n";    # prints "hello, world"

Process Variables

$< - Real UID

The real user ID of this process.

print "Real UID: $<\n";

$> - Effective UID

The effective user ID of this process.

print "Effective UID: $>\n";

$( - Real GID

The real group ID of this process.

print "Real GID: $(\n";

$) - Effective GID

The effective group ID of this process, followed by supplementary group IDs.

print "Effective GID: $)\n";

System Variables

%ENV - Environment Variables

Hash containing the current environment variables.

print $ENV{HOME};
$ENV{MY_VAR} = "value";

@ARGV - Command Line Arguments

Contains the command-line arguments passed to the script.

# script.pl arg1 arg2
print "Args: @ARGV\n";    # prints "Args: arg1 arg2"

@INC - Include Path

List of directories to search for use and require statements.

push @INC, '/my/lib/path';

%SIG - Signal Handlers

Hash used to set signal handlers.

$SIG{INT} = sub { die "Caught SIGINT" };

Version Variables

$] - Perl Version (Numeric)

The version number of the Perl interpreter, as a decimal.

print $];    # e.g., 5.042000

$^V - Perl Version (String)

The version of Perl as a version string.

print $^V;   # e.g., v5.42.0

$^O - Operating System

The name of the operating system.

print $^O;   # "linux" for PetaPerl

$^X - Perl Executable Path

The path to the Perl (or PetaPerl) executable.

print $^X;   # e.g., /usr/local/bin/pperl

PetaPerl-Specific Variables

Reserved for future PetaPerl-specific extensions.

SEE ALSO

Built-in Functions

PetaPerl implements the Perl 5 built-in function library. This document lists all implemented functions, organized by category.

Legend:

  • ✅ Fully implemented
  • ⚠️ Partially implemented
  • ❌ Not yet implemented

I/O Functions

FunctionStatusDescription
printPrint to filehandle or STDOUT
sayPrint with newline
printfFormatted print
sprintfFormatted string
print "Hello\n";
print $fh "data\n";
say "Hello";                    # Adds newline
printf "%d items\n", $count;
my $str = sprintf "%05d", $num;

File Operations

FunctionStatusDescription
openOpen file or pipe
closeClose filehandle
readRead fixed-length data
sysreadSystem-level read
syswriteSystem-level write
readlineRead line(s) from filehandle
getcRead single character
eofTest for end-of-file
seekPosition filehandle
tellGet filehandle position
filenoGet file descriptor number
binmodeSet binary mode
selectSet default filehandle
writeWrite formatted record
open my $fh, '<', $filename or die $!;
my $data;
read $fh, $data, 1024;          # Read 1024 bytes
my $line = readline($fh);       # or <$fh>
my $char = getc($fh);
seek $fh, 0, 0;                 # Rewind to start
my $pos = tell $fh;
close $fh;

Directory Operations

FunctionStatusDescription
opendirOpen directory handle
readdirRead directory entry
closedirClose directory handle
rewinddirReset directory handle
seekdirPosition directory handle
telldirGet directory position
mkdirCreate directory
rmdirRemove directory
chdirChange working directory
opendir my $dh, $dir or die $!;
my @entries = readdir $dh;
closedir $dh;

mkdir "newdir", 0755 or die $!;
rmdir "olddir";
chdir "/tmp";

File System Operations

FunctionStatusDescription
unlinkDelete files
renameRename file
linkCreate hard link
symlinkCreate symbolic link
readlinkRead symbolic link
chmodChange file permissions
chownChange file owner/group
utimeChange access/modification times
truncateTruncate file to length
statGet file status
lstatGet file status (no link follow)
globFile name expansion
unlink "file.txt" or die $!;
rename "old.txt", "new.txt";
link "source", "link";
symlink "target", "symlink";
my $target = readlink "symlink";
chmod 0644, @files;
chown $uid, $gid, @files;
utime $atime, $mtime, @files;
truncate $fh, $length;
my @info = stat $file;
my @info = lstat $symlink;      # Doesn't follow symlink
my @files = glob "*.txt";

String Functions

FunctionStatusDescription
lengthString/array length
substrExtract/replace substring
indexFind substring position
rindexFind substring (reverse)
chrNumber to character
ordCharacter to number
lcLowercase string
ucUppercase string
lcfirstLowercase first char
ucfirstUppercase first char
quotemetaQuote metacharacters
chopRemove last character
chompRemove line ending
hexHex string to number
octOctal string to number
packPack values into binary string
unpackUnpack binary string
vecBit vector access
my $len = length $str;
my $sub = substr $str, 0, 5;    # First 5 chars
substr($str, 7, 5) = "replace";  # Lvalue substr
my $pos = index $str, "find";
my $rpos = rindex $str, "last";

my $char = chr(65);              # "A"
my $code = ord("A");             # 65

my $lower = lc "HELLO";          # "hello"
my $upper = uc "hello";          # "HELLO"
my $cap = ucfirst "hello";       # "Hello"

chomp $line;                     # Remove \n if present
chop $str;                       # Remove last char
my $quoted = quotemeta '.*';     # '\\.\\\*'

my $num = hex "FF";              # 255
my $num = oct "377";             # 255

Lvalue substr: substr can be used as assignment target.

substr($str, 0, 3) = "New";      # Replace first 3 chars

Array Functions

FunctionStatusDescription
pushAppend to array
popRemove last element
shiftRemove first element
unshiftPrepend to array
spliceRemove/replace array elements
joinJoin array with separator
splitSplit string into array
reverseReverse list/string
sortSort list
grepFilter list
mapTransform list
push @arr, $val;
push @arr, @more;
my $last = pop @arr;
my $first = shift @arr;
unshift @arr, $val;

splice @arr, $offset, $length, @replacement;
my $str = join ",", @arr;
my @parts = split /,/, $str;
my @reversed = reverse @arr;
my @sorted = sort @arr;
my @sorted = sort { $a <=> $b } @numbers;
my @filtered = grep { $_ > 10 } @numbers;
my @doubled = map { $_ * 2 } @numbers;

Parallel execution: map, grep, and simple for loops may execute in parallel when safe.

Hash Functions

FunctionStatusDescription
keysGet hash keys
valuesGet hash values
eachIterate key-value pairs
existsCheck if key exists
deleteRemove hash key
my @keys = keys %hash;
my @vals = values %hash;
while (my ($k, $v) = each %hash) { ... }
if (exists $hash{key}) { ... }
my $removed = delete $hash{key};

Context sensitivity: keys and values return list in list context, count in scalar context.

my $count = keys %hash;          # Number of keys
my @all_keys = keys %hash;       # All keys

Math Functions

FunctionStatusDescription
absAbsolute value
intInteger truncation
sqrtSquare root
sinSine
cosCosine
expe raised to power
logNatural logarithm
atan2Arc tangent of y/x
randRandom number
srandSeed random number generator
my $abs = abs -5;                # 5
my $int = int 3.7;               # 3
my $sqrt = sqrt 16;              # 4
my $sin = sin $angle;
my $cos = cos $angle;
my $exp = exp 1;                 # e
my $log = log 10;
my $atan = atan2 $y, $x;
my $rand = rand 10;              # 0 to <10
srand 42;                        # Seed with specific value

Type and Reference Functions

FunctionStatusDescription
refGet reference type
blessBless reference into package
definedTest if defined
undefUndefine variable
scalarForce scalar context
my $type = ref $value;           # 'ARRAY', 'HASH', 'CODE', etc.
my $obj = bless {}, 'MyClass';
my $obj = bless $ref, 'MyClass';
if (defined $var) { ... }
undef $var;                      # Set to undef
my $count = scalar @arr;         # Force scalar context

Control and Introspection

FunctionStatusDescription
callerGet caller information
wantarrayGet calling context
prototypeGet subroutine prototype
posGet/set regex position
studyOptimize regex matching
resetReset variables
my ($package, $file, $line) = caller;
my ($pkg, $file, $line, $sub) = caller(1);
if (wantarray) {
    return @list;
} else {
    return $scalar;
}
my $proto = prototype \&mysub;
my $pos = pos $str;
study $str;                      # Optimize for repeated regex
reset;                           # Reset ?? searches

Process and System

FunctionStatusDescription
dieRaise exception
warnPrint warning
exitExit program
systemExecute external command
execExecute and replace process
forkFork child process
waitWait for child
waitpidWait for specific child
sleepSleep for seconds
timeGet current Unix time
timesGet process times
localtimeConvert time to local
gmtimeConvert time to GMT
getloginGet login name
die "Error: $!" if $error;
warn "Warning message";
exit 0;

my $status = system "ls", "-l";
exec "command", @args;           # No return if success
my $pid = fork;
if ($pid == 0) {
    # Child process
} else {
    # Parent process
}
wait;                            # Wait for any child
waitpid $pid, 0;                 # Wait for specific child

sleep 5;                         # Sleep 5 seconds
my $now = time;
my @times = times;               # (user, system, cuser, csystem)
my @lt = localtime time;
my @gmt = gmtime time;
my $user = getlogin;

User and Group Database

All user/group database functions are implemented:

FunctionStatusDescription
getpwnamGet password entry by name
getpwuidGet password entry by UID
getpwentGet next password entry
setpwentReset password iteration
endpwentEnd password iteration
getgrnamGet group entry by name
getgrgidGet group entry by GID
getgrentGet next group entry
setgrentReset group iteration
endgrentEnd group iteration
my @pwinfo = getpwnam "username";
my @pwinfo = getpwuid $uid;
while (my @entry = getpwent) { ... }
endpwent;

my @grinfo = getgrnam "groupname";
my @grinfo = getgrgid $gid;

Network Database

All network database functions are implemented:

FunctionStatusDescription
gethostbynameGet host by name
gethostbyaddrGet host by address
gethostentGet next host entry
sethostentReset host iteration
endhostentEnd host iteration
getnetbynameGet network by name
getnetbyaddrGet network by address
getnetentGet next network entry
setnetentReset network iteration
endnetentEnd network iteration
getprotobynameGet protocol by name
getprotobynumberGet protocol by number
getprotoentGet next protocol entry
setprotoentReset protocol iteration
endprotoentEnd protocol iteration
getservbynameGet service by name
getservbyportGet service by port
getserventGet next service entry
setserventReset service iteration
endserventEnd service iteration
my @host = gethostbyname "example.com";
my @net = getnetbyname "loopback";
my @proto = getprotobyname "tcp";
my @serv = getservbyname "http", "tcp";

Tie Mechanism

FunctionStatusDescription
tie⚠️Bind variable to object (stub — returns undef)
tied⚠️Get tied object (stub — returns undef)
untie⚠️Unbind variable (stub — no-op)

The functions are recognized and callable but do not dispatch to tie class methods. Modules depending on tie (e.g., Tie::File) will not function correctly.

Formats (Legacy)

FunctionStatusDescription
formline⚠️Format line (stub)
write⚠️Write formatted output (stub)

Perl formats are a legacy feature. The functions exist but the format output system is not implemented. Use printf/sprintf instead.

Socket Functions

All socket operations are implemented:

FunctionStatusDescription
socketCreate socket
socketpairCreate socket pair
bindBind socket to address
listenListen on socket
acceptAccept connection
connectConnect to remote
shutdownShut down socket
sendSend data
recvReceive data
getsockoptGet socket option
setsockoptSet socket option
getsocknameGet local address
getpeernameGet remote address

IPC Functions

FunctionStatusDescription
pipeCreate pipe
msgctlMessage queue control
msggetGet message queue
msgsndSend message
msgrcvReceive message
semctlSemaphore control
semgetGet semaphore set
semopSemaphore operations
shmctlShared memory control
shmgetGet shared memory
shmreadRead shared memory
shmwriteWrite shared memory

Known Limitations

Regular Expressions

  • qr// is fully implemented including precompiled pattern reuse
  • (?{code}) embedded code execution is implemented
  • Possessive quantifiers (*+, ++, ?+) are implemented
  • See Regular Expressions for remaining edge cases (self-referential captures, multi-char case folding)

Module System

  • use and require work for Pure Perl modules
  • XS modules are not supported (native reimplementations provided for common ones)
  • Some pragma effects may differ from perl5

Special Variables

  • Most special variables are populated ($^O, $^X, $], $^V, etc.)
  • Internals::SvREADONLY is not implemented

PetaPerl-Specific Notes

Constant Folding

PetaPerl performs compile-time constant folding for:

length("hello")      # Folded to 5 at compile time
substr("text", 0, 2) # Folded to "te"

Parallel Execution

These functions may execute in parallel when safe:

  • map - Parallel element transformation
  • grep - Parallel filtering
  • sort with pure comparison function

Parallelization requires:

  • No side effects in the callback
  • No shared mutable state
  • Array size above parallelization threshold

Performance

  • String functions use UTF-8 aware operations
  • Array operations minimize copying
  • Hash operations use optimized hash tables
  • Math functions map to CPU instructions when possible

Operators

PetaPerl implements the full set of Perl 5 operators with identical semantics. All operators maintain Perl’s context sensitivity and precedence rules.

Binary Operators

Arithmetic

OperatorOperationExample
+Addition$a + $b
-Subtraction$a - $b
*Multiplication$a * $b
/Division$a / $b
%Modulus$a % $b
**Exponentiation$a ** $b

All arithmetic operators coerce operands to numeric context. Division by zero produces a warning and returns infinity or NaN.

my $x = 5 + 3;        # 8
my $y = 10 / 3;       # 3.333...
my $z = 2 ** 10;      # 1024
my $m = 17 % 5;       # 2

String

OperatorOperationExample
.Concatenation$a . $b
xRepetition$str x $count

String operators coerce operands to string context.

my $str = "Hello" . " " . "World";  # "Hello World"
my $rep = "X" x 5;                   # "XXXXX"
my $pad = " " x 10;                  # 10 spaces

Numeric Comparison

OperatorOperationReturns
==EqualTrue if equal
!=Not equalTrue if not equal
<Less thanTrue if less
>Greater thanTrue if greater
<=Less or equalTrue if ≤
>=Greater or equalTrue if ≥
<=>Three-way compare-1, 0, or 1

Numeric comparisons coerce operands to numbers.

if ($age >= 18) { ... }
my $cmp = $a <=> $b;  # -1 if $a < $b, 0 if equal, 1 if $a > $b

String Comparison

OperatorOperationReturns
eqEqualTrue if equal
neNot equalTrue if not equal
ltLess thanTrue if less
gtGreater thanTrue if greater
leLess or equalTrue if ≤
geGreater or equalTrue if ≥
cmpThree-way compare-1, 0, or 1

String comparisons use lexicographic (dictionary) ordering.

if ($name eq "John") { ... }
my $cmp = $a cmp $b;  # -1, 0, or 1

Logical

OperatorOperationShort-circuitsPrecedence
&&AndYesHigh
``Or
//Defined-orYesHigh
andAndYesLow
orOrYesLow
xorExclusive orNoLow

Logical operators return the last evaluated value, not just true/false.

my $result = $x && $y;         # Returns $y if $x true, else $x
my $default = $user || "guest"; # Returns "guest" if $user false
my $value = $config // 0;      # Returns 0 only if $config undefined

Defined-or (//): Unlike ||, this only checks if the left side is defined, not just true. 0 and "" are both false but defined.

my $x = 0;
my $a = $x || 10;   # 10 (0 is false)
my $b = $x // 10;   # 0 (0 is defined)

Bitwise

OperatorOperationExample
&Bitwise AND$a & $b
``Bitwise OR
^Bitwise XOR$a ^ $b
<<Left shift$a << $n
>>Right shift$a >> $n

Bitwise operators work on integers. Operands are converted to unsigned integers.

my $mask = 0xFF & $value;
my $flags = $READ | $WRITE;
my $double = $x << 1;

Range

OperatorOperationContext
..Inclusive rangeList context creates list
...Flip-flopScalar context is stateful
my @digits = (0..9);              # (0, 1, 2, ..., 9)
my @letters = ('a'..'z');         # ('a', 'b', ..., 'z')
for my $i (1..100) { ... }       # Loop 1 to 100

In scalar context, .. and ... are flip-flop operators (stateful boolean range).

Binding

OperatorOperationExample
=~Match$str =~ /pattern/
!~Not match$str !~ /pattern/

Binding operators connect strings with regex operations.

if ($email =~ /\@/) { ... }       # Contains @
if ($name !~ /^\d/) { ... }       # Doesn't start with digit

Unary Operators

Arithmetic

OperatorOperationExample
-Negation-$x
+Unary plus+$x
my $neg = -5;
my $pos = +$x;  # Numeric context

Logical

OperatorOperationExample
!Not!$x
notNot (low precedence)not $x
if (!$error) { ... }
die "Failed" if not $ok;

Bitwise

OperatorOperationExample
~Bitwise complement~$x
my $inverted = ~$bits;

Reference

OperatorOperationExample
\Create reference\$x, \@arr, \%hash
my $scalar_ref = \$value;
my $array_ref = \@data;
my $hash_ref = \%config;

Increment/Decrement

OperatorOperationWhen evaluated
++$xPre-incrementAfter increment
--$xPre-decrementAfter decrement
$x++Post-incrementBefore increment
$x--Post-decrementBefore decrement
my $x = 5;
my $a = ++$x;  # $x is 6, $a is 6
my $b = $x++;  # $x is 7, $b is 6

String increment: ++ on strings performs “magic increment” (Perl-style).

my $s = "aa";
$s++;  # "ab"
$s++;  # "ac"

File Test Operators

File test operators check properties of files and filehandles. All return true/false except -s which returns file size.

OperatorTestReturns
-eExistsBoolean
-rReadableBoolean
-wWritableBoolean
-xExecutableBoolean
-oOwned by effective UIDBoolean
-RReadable by real UIDBoolean
-WWritable by real UIDBoolean
-XExecutable by real UIDBoolean
-OOwned by real UIDBoolean
-zZero sizeBoolean
-sNon-zero sizeSize in bytes or false
-fRegular fileBoolean
-dDirectoryBoolean
-lSymbolic linkBoolean
-pNamed pipe (FIFO)Boolean
-SSocketBoolean
-bBlock special fileBoolean
-cCharacter special fileBoolean
-tTTY (terminal)Boolean
-uSetuid bit setBoolean
-gSetgid bit setBoolean
-kSticky bit setBoolean
-TText fileBoolean
-BBinary fileBoolean
-MModification time (days)Number
-AAccess time (days)Number
-CInode change time (days)Number
if (-e $file) { ... }               # File exists
if (-f $path && -r $path) { ... }   # Regular file and readable
my $size = -s $file;                # Size in bytes
if (-d $path) { ... }               # Is directory

Stacked file tests: -f -w -r $file checks all three conditions.

Other Unary Operators

OperatorOperationExample
definedCheck if defineddefined $x
if (defined $value) { ... }

Assignment Operators

Simple Assignment

OperatorOperationExample
=Assignment$x = 5
my $x = 10;
my ($a, $b, $c) = (1, 2, 3);  # List assignment

Compound Assignment

All binary operators have compound assignment forms:

OperatorEquivalentExample
+=$x = $x + $y$x += 5
-=$x = $x - $y$x -= 3
*=$x = $x * $y$x *= 2
/=$x = $x / $y$x /= 10
%=$x = $x % $y$x %= 7
**=$x = $x ** $y$x **= 2
.=$x = $x . $y$str .= "more"
x=$x = $x x $y$str x= 3
&=$x = $x & $y$bits &= $mask
`=``$x = $x
^=$x = $x ^ $y$x ^= $y
<<=$x = $x << $y$x <<= 2
>>=$x = $x >> $y$x >>= 1
&&=$x = $x && $y$x &&= $default
`=`
//=$x = $x // $y$x //= 0
my $count = 10;
$count += 5;        # 15
$count *= 2;        # 30

my $path = "/home";
$path .= "/user";   # "/home/user"

$cache ||= load_data();   # Only load if $cache is false
$config //= {};           # Only assign if $config is undef

Ternary Operator

OperatorSyntaxExample
? :Conditional$cond ? $then : $else
my $result = $x > 0 ? "positive" : "non-positive";
my $max = $a > $b ? $a : $b;

Ternary as lvalue: The ternary operator can be used as an assignment target.

($x > 0 ? $pos : $neg) = 10;  # Assigns to $pos or $neg

Array/Hash Subscripts

SyntaxOperationExample
$arr[index]Array element access$arr[0]
$hash{key}Hash element access$hash{name}
@arr[indices]Array slice@arr[1, 3, 5]
@hash{keys}Hash slice@hash{qw(a b c)}
my $first = $arr[0];
my $value = $hash{key};
my @subset = @arr[0, 2, 4];       # Elements 0, 2, 4
my @values = @hash{'a', 'b'};     # Values for keys 'a', 'b'

Arrow Operator

SyntaxOperationExample
->[]Array deref subscript$aref->[0]
->{}Hash deref subscript$href->{key}
->()Method call$obj->method()
my $element = $array_ref->[5];
my $value = $hash_ref->{name};
my $result = $object->method(@args);

Comma Operators

OperatorOperationUse
,List separator(1, 2, 3)
=>Fat commakey => value

The fat comma (=>) autoquotes barewords on its left side.

my %hash = (
    name => "John",     # 'name' autoquoted
    age => 30,
);

Operator Precedence

Highest to lowest precedence (same as Perl 5):

  1. Terms and list operators (left)
  2. -> (left)
  3. ++ -- (none)
  4. ** (right)
  5. ! ~ \ unary + unary - (right)
  6. =~ !~ (left)
  7. * / % x (left)
  8. + - . (left)
  9. << >> (left)
  10. Named unary operators
  11. < > <= >= lt gt le ge (none)
  12. == != <=> eq ne cmp ~~ (none) — ~~ smart match is partial
  13. & (left)
  14. | ^ (left)
  15. && (left)
  16. || // (left)
  17. .. ... (none)
  18. ?: (right)
  19. = += -= etc. (right)
  20. , => (left)
  21. List operators (right)
  22. not (right)
  23. and (left)
  24. or xor (left)

Use parentheses when precedence is unclear.

PetaPerl-Specific Notes

Parallelization

Operators execute sequentially by default. PetaPerl’s parallelization applies at the loop/map/grep level, not individual operators.

Performance

  • Bitwise operations compile to native machine instructions
  • String concatenation may allocate new memory (use .= for efficiency)
  • Numeric operations are optimized for integers and floats separately

Regular Expressions

PetaPerl implements a Perl 5-compatible regex engine with support for the full range of Perl regex features.

Pattern Syntax

Literals

/hello/         # Match literal "hello"
/foo bar/       # Match "foo bar"

Metacharacters

CharMeaning
.Any character except newline
^Start of string
$End of string
\AStart of string (absolute)
\zEnd of string (absolute)
\ZEnd of string or before final newline
\bWord boundary
\BNot word boundary
\GPosition of last match
/^start/        # Must be at start
/end$/          # Must be at end
/\bword\b/      # Whole word match
/\Abegin/       # Absolute start
/finish\z/      # Absolute end

Character Classes

[abc]           # Match a, b, or c
[^abc]          # Match anything except a, b, c
[a-z]           # Match lowercase letter
[A-Z0-9]        # Match uppercase or digit
[a-zA-Z_]       # Match word character

Predefined Character Classes

ClassMatchesNegated
\dDigit [0-9]\D (non-digit)
\wWord char [a-zA-Z0-9_]\W (non-word)
\sWhitespace [ \t\n\r\f]\S (non-whitespace)
\hHorizontal whitespace\H
\vVertical whitespace\V
/\d+/           # One or more digits
/\w+/           # One or more word chars
/\s*/           # Zero or more spaces

POSIX Character Classes

[:alnum:]       # Alphanumeric [a-zA-Z0-9]
[:alpha:]       # Alphabetic [a-zA-Z]
[:ascii:]       # ASCII characters [0-127]
[:blank:]       # Space and tab
[:cntrl:]       # Control characters
[:digit:]       # Digits [0-9]
[:graph:]       # Visible characters (not space)
[:lower:]       # Lowercase letters
[:print:]       # Printable characters
[:punct:]       # Punctuation
[:space:]       # Whitespace
[:upper:]       # Uppercase letters
[:word:]        # Word characters [a-zA-Z0-9_]
[:xdigit:]      # Hex digits [0-9A-Fa-f]

Usage: [[:digit:]] or [[:alpha:][:digit:]]

Quantifiers

QuantifierMeaningGreedyNon-greedyPossessive
*0 or moreYes*?*+
+1 or moreYes+?++
?0 or 1Yes???+
{n}Exactly nYesN/AN/A
{n,}n or moreYes{n,}?N/A
{n,m}n to mYes{n,m}?N/A
/a*/            # 0 or more 'a' (greedy)
/a*?/           # 0 or more 'a' (non-greedy)
/a+/            # 1 or more 'a'
/a?/            # 0 or 1 'a'
/a{3}/          # Exactly 3 'a'
/a{3,}/         # 3 or more 'a'
/a{3,5}/        # 3 to 5 'a'

Greedy vs Non-greedy: Greedy quantifiers match as much as possible, non-greedy match as little as possible.

# Given: "foo123bar"
/\d+/           # Matches "123" (greedy)
/\d+?/          # Matches "1" (non-greedy, but entire pattern must match)

Groups and Captures

Capturing Groups

/(foo)/         # Capture "foo" in $1
/(foo)(bar)/    # Capture in $1 and $2

Access captures with $1, $2, etc., or use @+ array.

Non-Capturing Groups

/(?:foo)/       # Group but don't capture

Use when grouping is needed but capture overhead is not.

Named Captures

/(?<name>\w+)/  # Named capture "name"
/(?'name'\w+)/  # Alternative syntax

Access with $+{name} hash.

Alternation

/foo|bar/       # Match "foo" or "bar"
/(red|green|blue)/ # Capture color

Anchors and Assertions

Zero-Width Assertions

AssertionMeaning
(?=pattern)Positive lookahead
(?!pattern)Negative lookahead
(?<=pattern)Positive lookbehind
(?<!pattern)Negative lookbehind
/foo(?=bar)/    # "foo" followed by "bar" (bar not consumed)
/foo(?!bar)/    # "foo" not followed by "bar"
/(?<=foo)bar/   # "bar" preceded by "foo"
/(?<!foo)bar/   # "bar" not preceded by "foo"

Atomic Groups

/(?>pattern)/   # Atomic group (no backtracking)

Once matched, the group’s contents are fixed. Used for performance optimization.

Backreferences

/(foo)\1/       # Match "foofoo" - \1 references first capture
/(['"]).*?\1/   # Match quoted string (same quote type)

Named Backreferences

/(?<tag>\w+)...\k<tag>/ # Named backreference

Conditionals

/(?(condition)yes|no)/ # If condition matches, try "yes", else try "no"

Conditions can be:

  • Capture group number: (?(1)yes|no) - if group 1 matched
  • Named capture: (?(<name>)yes|no) - if named group matched
  • Lookahead: (?(?=test)yes|no) - if lookahead succeeds

Modifiers

Modifiers change regex behavior. Applied after closing delimiter:

ModifierMeaning
iCase-insensitive
mMultiline (^/$ match line boundaries)
sSingle-line (. matches newline)
xExtended (ignore whitespace, allow comments)
gGlobal (find all matches)
cContinue searching after failed match
oCompile once (legacy, not needed in PetaPerl)
eEvaluate replacement as code (in s///)
/pattern/i      # Case-insensitive
/pattern/ms     # Multiline + single-line
/pattern/x      # Extended (readable)
/pattern/g      # Global matching

Examples

# Case-insensitive
if ($str =~ /hello/i) { ... }

# Multiline: ^ and $ match line starts/ends
while ($text =~ /^Line: (.+)$/mg) {
    print "Found: $1\n";
}

# Extended: whitespace and comments ignored
my $email_re = qr{
    (\w+)           # Username
    @               # At sign
    ([\w.]+)        # Domain
}x;

# Global: find all matches
my @words = $text =~ /\w+/g;

Matching and Substitution

Match Operator

$str =~ /pattern/       # True if matches
$str =~ /pattern/g      # Global, returns all matches

In list context with captures:

my ($user, $domain) = $email =~ /(\w+)@([\w.]+)/;

In list context with global:

my @numbers = $text =~ /\d+/g;  # All numbers

Substitution

$str =~ s/old/new/      # Replace first occurrence
$str =~ s/old/new/g     # Replace all occurrences
$str =~ s/old/new/i     # Case-insensitive replace
$str =~ s/old/new/gi    # Global + case-insensitive

Replacement with captures:

$str =~ s/(\w+)@(\w+)/$2\@$1/;  # Reverse user@domain

Evaluated replacement:

$str =~ s/(\d+)/$1 * 2/e;       # Double all numbers

Transliteration

$str =~ tr/abc/xyz/     # Replace a→x, b→y, c→z
$str =~ y/abc/xyz/      # Same as tr
$str =~ tr/a-z/A-Z/     # Uppercase
$str =~ tr/ //d         # Delete spaces
$str =~ tr/a-z//c       # Count non-lowercase

Special Variables

After a successful match:

VariableContains
$&Entire matched string
$`String before match
$'String after match
$1, $2, …Capture groups
$+Last matched capture
@+End positions of captures
@-Start positions of captures
%+Named captures
if ($str =~ /(foo)(bar)/) {
    print "Full match: $&\n";     # "foobar"
    print "Group 1: $1\n";        # "foo"
    print "Group 2: $2\n";        # "bar"
    print "Before: $`\n";
    print "After: $'\n";
}

Regex Compilation

qr// Operator

Compile regex for reuse:

my $word = qr/\w+/;
my $email = qr/\w+@\w+\.\w+/;

if ($str =~ $word) { ... }
if ($str =~ /$word@$word/) { ... }  # Interpolate

Benefits:

  • Compile once, use many times
  • Readable regex composition
  • Performance optimization

Performance Considerations

Anchored Patterns

Patterns anchored with ^ or \A are faster:

/^pattern/      # Fast: only checks start
/pattern/       # Slower: scans entire string

Atomic Groups

Use atomic groups (?>...) to prevent backtracking:

# Slow: backtracks on failure
/\d+\w+/

# Fast: no backtracking in \d+
/(?>\d+)\w+/

Non-Capturing Groups

Use (?:...) when captures aren’t needed:

/(?:foo|bar)/   # Faster than /(foo|bar)/ when capture not needed

PetaPerl-Specific Features

Bytecode Compilation

Regex patterns compile to bytecode for efficient execution. PetaPerl uses:

  • Bitmap character classes for fast ASCII matching
  • Literal prefix extraction to skip impossible positions
  • Anchored detection to avoid unnecessary scanning

Possessive Quantifiers

Possessive quantifiers prevent backtracking entirely (more efficient than atomic groups for simple cases):

/a++/           # 1 or more 'a', no backtracking
/a*+/           # 0 or more 'a', no backtracking
/a?+/           # 0 or 1 'a', no backtracking

Embedded Code

/pattern(?{ code })/    # Execute code during match
/(??{ code })/          # Postponed regex (code returns pattern)

(?{code}) executes Perl code at the point in the pattern where it appears. The code can access $1, $2, etc. from captures made so far.

Current Limitations

PetaPerl’s regex engine passes 99.3% of perl5’s re_tests suite (1959/1972 tests). Remaining gaps:

  • Self-referential captures — patterns like (a\1) (3 tests)
  • local in code blocks(?{ local $x = ... }) (2 tests)
  • Multi-character case folding — Unicode chars that fold to multiple chars (2 tests)
  • Branch reset backreferences — complex (?|...) with backrefs (5 tests)
  • String interpolation edge case — 1 test

Unicode property support (\p{Letter}, \p{Digit}, etc.) is fully implemented for standard Unicode categories.

Examples

Email Validation

my $email_re = qr/^[\w.+-]+@[\w.-]+\.[a-zA-Z]{2,}$/;
if ($email =~ $email_re) {
    print "Valid email\n";
}

URL Parsing

my ($protocol, $host, $path) = $url =~
    m{^(https?)://([^/]+)(/.*)$};

Log File Parsing

while ($line =~ /\[(\d{4}-\d{2}-\d{2})\] (\w+): (.+)/g) {
    my ($date, $level, $msg) = ($1, $2, $3);
    # Process log entry
}

String Cleanup

# Remove multiple spaces
$text =~ s/\s+/ /g;

# Remove leading/trailing whitespace
$text =~ s/^\s+|\s+$//g;

# Or using two substitutions
$text =~ s/^\s+//;
$text =~ s/\s+$//;

Template Substitution

my %vars = (name => "John", age => 30);
my $template = "Hello {{name}}, you are {{age}} years old.";
$template =~ s/\{\{(\w+)\}\}/$vars{$1}/ge;

See Also

  • perlop - Binding operators =~ and !~
  • perlvar - Special variables like $&, $1, etc.

Data Types

Perl has three fundamental data types: scalars, arrays, and hashes. PetaPerl implements these with identical semantics to Perl 5.

Scalars

Scalars are Perl’s basic data type, holding a single value. The variable name begins with $.

my $number = 42;
my $string = "hello";
my $float = 3.14;
my $ref = \@array;

Scalar Types

Internally, PetaPerl represents scalars as an enum with these variants:

TypeDescriptionExample
UndefUndefined valueundef
IvInteger (i64)42, -17
UvUnsigned integer (u64)Large positive numbers
NvFloat (f64)3.14, 1.5e10
PvImmutable string (Arc<str>)Constants, literals
PvBufMutable string (Arc<String>).= targets, $_
RvReference\$x, \@arr, \%hash
AvArrayCreated by []
HvHashCreated by {}

Dynamic typing: A scalar can change type during execution.

my $x = 42;          # Integer
$x = "hello";        # Now a string
$x = 3.14;           # Now a float

Scalar Context

Operations that expect a single value force scalar context:

my $count = @array;          # Array length
my $last = (1, 2, 3);        # Last element (3)
if (@array) { ... }          # True if non-empty

Special Scalars

ValueDescription
undefUndefined value
0Numeric zero, string “0”
""Empty string

Truth values: These are false in boolean context: undef, 0, "0", "". Everything else is true.

if ($value) { ... }          # False if undef, 0, "0", or ""
if (defined $value) { ... }  # False only if undef

Arrays

Arrays are ordered lists of scalars. Variable names begin with @.

my @numbers = (1, 2, 3, 4, 5);
my @words = qw(foo bar baz);
my @empty = ();

Array Access

my $first = $numbers[0];     # First element (index 0)
my $last = $numbers[-1];     # Last element
$numbers[5] = 6;             # Set element

Note the sigil change: Use $ to access a single element because you’re getting a scalar.

Array Length

my $length = @array;         # Scalar context
my $length = scalar @array;  # Explicit scalar context
my $max_index = $#array;     # Highest index (length - 1)

Array Operations

push @arr, $value;           # Append
my $value = pop @arr;        # Remove last
my $value = shift @arr;      # Remove first
unshift @arr, $value;        # Prepend

splice @arr, $offset, $len, @replacement;  # General removal/insertion

Array Slices

Extract multiple elements at once:

my @subset = @arr[0, 2, 4];  # Elements 0, 2, 4
my @range = @arr[0..5];      # Elements 0 through 5
@arr[1, 3] = (10, 20);       # Assign to multiple elements

List Context

Operations that expect multiple values force list context:

my @copy = @original;        # Array copy
my ($a, $b, $c) = (1, 2, 3); # List assignment
my @results = function();    # Function returns list

Array References

Create references to arrays:

my $aref = \@array;          # Reference to existing array
my $aref = [1, 2, 3];        # Anonymous array reference

Access via reference:

my $elem = $aref->[0];       # First element
my @copy = @$aref;           # Dereference to array
push @$aref, $value;         # Push via reference

Hashes

Hashes are unordered key-value pairs. Variable names begin with %.

my %user = (
    name => "John",
    age => 30,
    email => "john@example.com",
);

Hash Access

my $name = $user{name};      # Get value
$user{city} = "NYC";         # Set value

Sigil change: Use $ for single element access (getting a scalar).

Hash Operations

my @keys = keys %hash;       # All keys
my @values = values %hash;   # All values
while (my ($k, $v) = each %hash) { ... }  # Iterate

if (exists $hash{key}) { ... }  # Check existence
my $val = delete $hash{key};    # Remove and return

Hash Slices

Extract multiple values at once:

my @vals = @hash{qw(name age)};         # Values for keys
@hash{qw(x y)} = (10, 20);              # Assign multiple

Note: Hash slices use @ sigil because they return a list.

Hash References

Create references to hashes:

my $href = \%hash;           # Reference to existing hash
my $href = { key => "val" }; # Anonymous hash reference

Access via reference:

my $val = $href->{key};      # Get value
$href->{new} = "value";      # Set value
my @keys = keys %$href;      # Dereference to hash

References

References are scalars that point to other data.

Creating References

my $scalar_ref = \$scalar;
my $array_ref = \@array;
my $hash_ref = \%hash;
my $code_ref = \&sub;
my $anon_array = [1, 2, 3];
my $anon_hash = { a => 1, b => 2 };
my $anon_sub = sub { ... };

Dereferencing

my $value = $$scalar_ref;    # Scalar dereference
my @array = @$array_ref;     # Array dereference
my %hash = %$hash_ref;       # Hash dereference
my $result = &$code_ref();   # Code dereference

Arrow notation (preferred for clarity):

my $elem = $array_ref->[0];
my $val = $hash_ref->{key};
my $result = $code_ref->(@args);

Reference Types

Check reference type with ref:

my $type = ref $ref;
# Returns: 'SCALAR', 'ARRAY', 'HASH', 'CODE', 'REF', or '' (not a ref)

Nested Data Structures

References enable complex data structures:

my $data = {
    users => [
        { name => "John", age => 30 },
        { name => "Jane", age => 25 },
    ],
    config => {
        debug => 1,
        timeout => 30,
    },
};

my $name = $data->{users}->[0]->{name};  # "John"

Autovivification

Perl automatically creates intermediate references:

my %hash;
$hash{a}{b}{c} = 1;          # Creates nested hashes automatically
my $val = $hash{x}[0];       # Creates array ref at $hash{x}

Type Conversions

Perl performs automatic type conversions based on context.

String to Number

my $x = "42";
my $y = $x + 10;             # 52 (string → number)

Number to String

my $x = 42;
my $s = "Value: $x";         # "Value: 42" (number → string)

String Concatenation

my $result = 10 . 20;        # "1020" (both → string)

Boolean Context

if ("0")    { ... }          # False
if ("00")   { ... }          # True
if (0)      { ... }          # False
if (0.0)    { ... }          # False
if ("")     { ... }          # False
if (undef)  { ... }          # False

Typeglobs

Typeglobs are a special type that can hold entries for all variable types with the same name.

*name = \$scalar;            # Alias glob to scalar
*name = \&sub;               # Alias glob to subroutine

Primarily used for symbol table manipulation and importing.

PetaPerl-Specific Implementation

Memory Representation

PetaPerl uses efficient internal representations:

Scalars: Rust enum with variants for each type. Two string representations optimize for different access patterns.

#![allow(unused)]
fn main() {
pub enum Sv {
    Undef,
    Iv(i64),                 // Integer
    Uv(u64),                 // Unsigned
    Nv(f64),                 // Float
    Pv(Arc<str>, u32),       // Immutable string + virtual length (O(1) chomp)
    PvBuf(Arc<String>),      // Mutable string (COW via Arc::make_mut)
    Rv(RvInner),             // Reference
    Av(Av),                  // Array
    Hv(Hv),                  // Hash
    // ... additional types
}
}

Dual string representation: Pv is used for constants and literals — the u32 virtual length enables O(1) chomp without modifying the shared string. PvBuf is used for mutable strings (default for new_string()) — Arc::make_mut() provides copy-on-write semantics with zero allocation when unshared.

Arrays: Dynamic vectors with efficient push/pop operations.

Hashes: Optimized hash tables with fast key lookup.

Shared Ownership

String scalars use Arc<str> (atomic reference counting):

  • Cheap cloning (just increment counter)
  • Thread-safe sharing for parallel execution
  • Common strings can share storage

Aliasing Support

PetaPerl implements @_ aliasing correctly:

  • Arguments are aliases to caller’s variables
  • Modifications write through to original
  • Uses SvCell for mutable indirection
sub modify {
    $_[0] = "changed";       # Modifies caller's variable
}

my $x = "original";
modify($x);
print $x;                    # "changed"

Performance Characteristics

OperationComplexityNotes
Scalar assignmentO(1)String uses Arc (no copy)
Array push/popO(1) amortizedDynamic growth
Array shift/unshiftO(n)Must move elements
Hash accessO(1) averageOptimized hash function
Hash insertO(1) averageWith growth

Parallel Execution

PetaPerl’s parallelization model:

  • Each thread gets its own lexical pad (no shared mutation)
  • Array and hash operations are thread-safe
  • Arc<str> strings share across threads safely
  • Loop-level parallelism doesn’t require global locks

Context Sensitivity

Perl’s context system determines how expressions evaluate.

Scalar Context

Forces single-value evaluation:

my $count = @array;          # Length
my $last = (1, 2, 3);        # Last element
my $concat = (1, 2, 3);      # 3

List Context

Forces multiple-value evaluation:

my @copy = @array;           # All elements
my @results = func();        # All return values
my ($a, $b) = (1, 2, 3);     # First two elements

Void Context

Result is discarded:

func();                      # Return value ignored
print "hello";               # No assignment

Context Propagation

my @arr = (1, 2, 3);

# Scalar context
my $x = keys @arr;           # keys in scalar context → count

# List context
my @k = keys @arr;           # keys in list context → all keys

# Function argument context
func(@arr);                  # List context
func(scalar @arr);           # Scalar context (explicit)

Constants

Constants are immutable values.

Literal Constants

42                           # Integer
3.14                         # Float
"string"                     # String
qw(a b c)                    # List of strings

Named Constants

use constant PI => 3.14159;
use constant MAX => 100;
use constant {
    RED   => 0xFF0000,
    GREEN => 0x00FF00,
    BLUE  => 0x0000FF,
};

Compile-Time Constant Folding

PetaPerl performs constant folding at compile time:

my $x = 2 + 3;               # Folded to 5
my $len = length("hello");   # Folded to 5
my $sub = substr("text", 0, 2);  # Folded to "te"

This optimization eliminates runtime computation for constant expressions.

Special Types

Code References

Subroutines can be referenced and called dynamically:

my $coderef = sub { return $_[0] * 2 };
my $result = $coderef->(21);  # 42

my $coderef = \&existing_sub;
$coderef->(@args);

Filehandles

Filehandles are special scalars:

open my $fh, '<', $file or die $!;
my $line = <$fh>;
close $fh;

Globs

Typeglobs reference symbol table entries:

*alias = *original;          # Alias all types
*func = sub { ... };         # Install subroutine

See Also

  • perlop - Operators that work with these types
  • perlfunc - Functions for manipulating data
  • perlref - More on references (Perl 5 docs)

Subroutines

Subroutines are reusable code blocks in Perl. PetaPerl implements Perl5-compatible subroutines with full support for lexical variables, closures, and signatures.

Declaration

sub greet {
    print "Hello, World!\n";
}

# Named sub
sub add {
    my ($a, $b) = @_;
    return $a + $b;
}

# Anonymous sub
my $sub = sub {
    my $x = shift;
    return $x * 2;
};

Arguments (@_)

Arguments are passed via the special @_ array. Each element is aliased to the caller’s variable.

sub modify {
    $_[0] = "modified";  # Modifies caller's variable
}

my $x = "original";
modify($x);
say $x;  # "modified"

Copying arguments:

sub safe_modify {
    my ($val) = @_;      # Copy, not alias
    $val = "modified";   # Does not affect caller
}

Direct access:

sub first { $_[0] }
sub second { $_[1] }

Return Values

Explicit Return

sub max {
    my ($a, $b) = @_;
    return $a if $a > $b;
    return $b;
}

Implicit Return

The last expression’s value is returned automatically.

sub square {
    my $x = shift;
    $x * $x  # Implicit return
}

Context-Sensitive Returns

sub get_data {
    if (wantarray) {
        return (1, 2, 3);      # List context
    } else {
        return "scalar data";  # Scalar context
    }
}

my @list = get_data();   # (1, 2, 3)
my $scalar = get_data(); # "scalar data"

Note: PetaPerl’s wantarray works for direct calls and most common patterns. Complex nesting may have edge cases.

Prototypes

Prototypes provide compile-time argument checking and context forcing.

sub scalar_arg ($) {
    my $x = shift;
}

sub array_arg (@) {
    my @vals = @_;
}

sub no_args () {
    return 42;
}

Common prototypes:

PrototypeMeaning
$Single scalar argument
@Array of arguments (slurpy)
%Hash (even-length list)
&Subroutine reference
*Bareword or typeglob
\$Scalar reference
\@Array reference
\%Hash reference
()No arguments (constant sub)

PetaPerl Status: Basic prototype parsing implemented. Runtime enforcement partial.

Signatures (Experimental)

Perl 5.20+ signatures provide named parameters with optional type constraints and defaults.

sub greet ($name, $greeting = "Hello") {
    say "$greeting, $name!";
}

greet("Alice");              # Hello, Alice!
greet("Bob", "Hi");          # Hi, Bob!

Slurpy parameters:

sub sum ($first, @rest) {
    my $total = $first;
    $total += $_ for @rest;
    return $total;
}

sum(1, 2, 3, 4);  # 10

PetaPerl Status: Signature parsing implemented in AST. Codegen lowers to pad allocation. Runtime parameter assignment works. Default values and slurpy params functional.

Closures

Anonymous subs capture lexical variables from enclosing scope.

sub make_counter {
    my $count = 0;
    return sub {
        return ++$count;
    };
}

my $c1 = make_counter();
say $c1->();  # 1
say $c1->();  # 2

my $c2 = make_counter();
say $c2->();  # 1  (independent counter)

Closure capture mechanism:

  • Parser identifies outer lexicals referenced in sub body
  • Codegen records outer_refs mapping (sub_pad_slot, outer_pad_slot)
  • pp_anoncode captures cells at instantiation time
  • pp_entersub shares captured cells into sub’s pad

Example: Closure factory

sub make_adder {
    my $offset = shift;
    return sub {
        my $x = shift;
        return $x + $offset;  # Captures $offset
    };
}

my $add5 = make_adder(5);
my $add10 = make_adder(10);

say $add5->(3);   # 8
say $add10->(3);  # 13

Each closure captures its own $offset cell. Modifications affect only that closure’s instance.

Method Calls

my $obj = Some::Class->new();
$obj->method($arg);

# Indirect object notation (discouraged)
method $obj $arg;

PetaPerl Status: Method dispatch via pp_method, pp_method_named. ISA lookup implemented. AUTOLOAD supported.

Special Subroutines

AUTOLOAD

Called when undefined sub is invoked.

package Foo;

our $AUTOLOAD;

sub AUTOLOAD {
    my $method = $AUTOLOAD;
    $method =~ s/.*:://;  # Strip package
    print "Called undefined method: $method\n";
}

Foo->undefined_method();  # Triggers AUTOLOAD

PetaPerl Status: AUTOLOAD dispatch implemented in pp_entersub. Sets $Package::AUTOLOAD correctly.

BEGIN, END, INIT, CHECK, UNITCHECK

PetaPerl Status: BEGIN blocks execute during compilation. END blocks execute at program exit. INIT, CHECK, and UNITCHECK have partial support.

Recursion

Recursive calls are supported with depth limit.

sub factorial {
    my $n = shift;
    return 1 if $n <= 1;
    return $n * factorial($n - 1);
}

Recursion limit: Default 1000 levels (configurable via PPERL_MAX_RECURSION). Prevents stack overflow.

Calling Conventions

With parentheses

foo();        # No arguments
foo($a);      # One argument
foo($a, $b);  # Multiple arguments

Without parentheses (ampersand form)

&foo;   # Pass current @_ to foo

When called as &foo (no parens), the current @_ is passed to the callee. This enables wrapper functions:

sub wrapper {
    log_call();
    goto &real_function;  # Tail call with current @_
}

Goto and Tail Calls

sub outer {
    # ... setup ...
    goto &inner;  # Replace current call frame
}

goto &sub performs tail call optimization: replaces current frame instead of adding a new one.

PetaPerl Status: goto &sub implemented in pp_goto. Tail recursion optimization functional.

Advanced Features

Constant Subs

use constant PI => 3.14159;
use constant DEBUG => 0;

# Equivalent to:
sub PI () { 3.14159 }

Constant subs have empty prototype () and return fixed value. Perl can inline them at compile time.

PetaPerl Status: use constant creates constant subs. pp_entersub returns constant value directly without entering sub.

Lvalue Subs

sub get_value : lvalue {
    $global_var;
}

get_value() = 42;  # Assigns to $global_var

PetaPerl Status: :lvalue attribute not implemented. pp_leavesublv stub exists.

State Variables

sub counter {
    state $count = 0;
    return ++$count;
}

state variables persist across calls (initialized once).

PetaPerl Status: State variables are implemented. Variables are initialized once and persist across calls.

Built-in Functions

wantarray

Returns calling context: undef (void), 0 (scalar), 1 (list).

sub context_aware {
    if (!defined wantarray) {
        say "void context";
    } elsif (wantarray) {
        say "list context";
        return (1, 2, 3);
    } else {
        say "scalar context";
        return 42;
    }
}

PetaPerl Status: Partial implementation. Works for direct calls. May fail in complex nesting.

caller

Returns information about calling stack frame.

# Scalar context: boolean (is there a caller?)
if (caller) {
    # Called from another sub
}

# List context: detailed info
my ($package, $filename, $line) = caller(0);
my ($pkg, $file, $line, $sub) = caller(1);  # One frame up

PetaPerl Status: Implemented in pp_caller. Returns (package, filename, line, subroutine, ...) in list context.

PetaPerl Implementation Notes

Op Tree Structure

Named subs register CV (Code Value) in arena:

SubDef → lower_sub() → NextState → body ops → LeaveSub
                     ↓
                 Register CV(name, start_op, pad_size, outer_refs)

Anonymous subs emit AnonCode op:

AnonSub → lower_anon_sub() → NextState → body ops → LeaveSub
                           ↓
                       AnonCode (pushes CV onto stack)

Call Sequence

  1. Caller pushes args onto stack
  2. Caller pushes mark
  3. Caller pushes CV reference
  4. EnterSub pops CV, pops mark, collects args into aliased @_
  5. New pad allocated (slot 0 = @_)
  6. Closure cells shared into pad (if any)
  7. Jump to CV’s start_op
  8. Body executes
  9. LeaveSub collects return values, restores pad
  10. Return to caller

Pad Layout

Slot 0: @_ (always, populated by EnterSub)
Slot 1+: Lexical variables in declaration order

Signature parameters become pad slots:

sub foo ($x, $y, @rest) { }

# Pad layout:
# 0: @_
# 1: $x
# 2: $y
# 3: @rest

Closure Capture

Two-stage process:

  1. Codegen: Analyze outer lexicals, record outer_refs in CV
  2. Runtime: pp_anoncode captures cells, pp_entersub shares them

Why two stages? Closure factories need per-instance capture, not per-definition.

Known Limitations

FeatureStatus
Named parametersWorking
PrototypesParsed, partial enforcement
SignaturesWorking (basic)
ClosuresWorking
RecursionWorking (1000 depth limit)
AUTOLOADWorking
Method dispatchWorking
wantarrayPartial
callerWorking
BEGIN/END blocksBEGIN works; END/INIT/CHECK partial
Attributes (:lvalue, etc.)Parsed, not enforced
goto &subWorking
State variablesBasic support

Performance

PetaPerl subs execute via the interpreter with fast-path dispatch. The JIT compiler (Cranelift) handles hot loops within subroutines but does not JIT subroutine call/return itself.

The interpreter’s function-pointer dispatch table and inline fast paths for common operations (arithmetic, comparison, assignment) minimize per-op overhead.

Closure overhead: Cell allocation per captured variable. Minimal at runtime (Arc clone).

Testing

See t/05-op/sub.t, t/05-op/closure.t for comprehensive test coverage.

Built-in Functions

pperl PP runtime built-in functions extracted from the Rust implementation source.

Array Functions

  • delete — Removes the specified element(s) from a hash or array.
  • each — Returns the next key-value pair from a hash’s internal iterator.
  • exists — Tests whether a hash key or array index is present in the container.
  • keys — Returns all keys of a hash, or the number of keys in scalar context.
  • pop — Removes and returns the last element of an array.
  • push — Appends one or more values to the end of an array, growing it as needed.
  • shift — Removes and returns the first element of an array, shifting all
  • sort — Sorts a list of values and returns them in sorted order.
  • splice — Removes and/or replaces elements in an array, returning the removed
  • unshift — Prepends one or more values to the beginning of an array.
  • values — Returns all values of a hash, or the number of values in scalar context.

File Functions

  • chdir — Changes the process’s current working directory.
  • chmod — Changes file permissions on one or more files.
  • chown — Changes the owner and group of one or more files.
  • closedir — Closes a directory handle, releasing its resources.
  • glob — Expands shell-style filename patterns into matching pathnames.
  • link — Creates a hard link to an existing file.
  • lstat — Returns file metadata without following symbolic links.
  • mkdir — Creates a new directory with the specified permissions.
  • opendir — Opens a directory for reading via a directory handle.
  • readdir — Reads entries from an open directory handle.
  • readlink — Returns the target of a symbolic link.
  • rename — Renames or moves a file.
  • rewinddir — Resets the position of a directory handle back to the beginning.
  • rmdir — Removes an empty directory.
  • stat — Returns file metadata as a 13-element list.
  • symlink — Creates a symbolic (soft) link.
  • umask — Gets or sets the process file-creation permission mask.
  • unlink — Deletes one or more files from the filesystem.
  • utime — Sets the access and modification timestamps of one or more files.

Formatting

  • sprintf — Formats a string according to a printf-style format specification.

Hash Functions

  • delete — Removes the specified element(s) from a hash or array.
  • each — Returns the next key-value pair from a hash’s internal iterator.
  • exists — Tests whether a hash key or array index is present in the container.
  • keys — Returns all keys of a hash, or the number of keys in scalar context.
  • values — Returns all values of a hash, or the number of values in scalar context.

I/O Functions

  • binmode — Sets the I/O discipline (encoding layer) for a filehandle.
  • close — Closes a filehandle, flushing any buffered output.
  • eof — Tests whether the next read on a filehandle will return end-of-file.
  • fileno — Returns the underlying OS file descriptor for a Perl filehandle.
  • getc — Reads a single character from a filehandle.
  • open — Opens a file, pipe, or duplicates a file descriptor, associating
  • pipe — Creates a unidirectional pipe pair (read end + write end).
  • print — Outputs a list of values to a filehandle.
  • printf — Formats and prints a list of values to a filehandle using a format string.
  • read — Reads a fixed number of bytes from a filehandle into a scalar buffer.
  • readline — PP function for readline
  • say — Like print, but automatically appends a newline after the output.
  • seek — Sets the read/write position in a filehandle.
  • select — Sets the default output filehandle or performs I/O multiplexing.
  • sysopen — Opens a file using POSIX open(2) flags for precise mode control.
  • sysread — Performs an unbuffered read directly from the OS file descriptor.
  • syswrite — Performs an unbuffered write directly to the OS file descriptor.
  • tell — Returns the current byte position in a filehandle.
  • truncate — Truncates a file to a specified length.

Numeric Functions

  • rand — Returns a pseudo-random floating-point number.
  • srand — Seeds the pseudo-random number generator used by rand.

Process Functions

  • alarm — Schedules a SIGALRM signal to be delivered after a given number
  • die — Raises an exception, terminating execution unless caught by an eval
  • exec — Replaces the current process image with a new program via execvp(3).
  • exit — Terminates the program with the given exit status.
  • fork — Creates a child process by duplicating the current process.
  • kill — Sends a signal to one or more processes or process groups.
  • sleep — Suspends execution for the specified number of seconds.
  • system — Executes an external command and waits for it to complete.
  • wait — Waits for any child process to terminate.
  • waitpid — Waits for a specific child process to change state.
  • warn — Issues a warning message to STDERR without terminating the program.

Regular Expressions

  • m// — Tests a string against a regular expression pattern.
  • pos — Gets or sets the position of the last m//g match on a string.
  • qr — Creates a precompiled regular expression object.
  • quotemeta — Escapes all non-“word” characters with a preceding backslash.
  • s/// — Searches a string for a pattern and replaces matched text.
  • split — Splits a string into a list of substrings using a pattern.
  • tr — Performs character-by-character transliteration on a string.

Scope and Variables

  • bless — Associates a reference with a package, turning it into an object.
  • caller — Returns information about the calling subroutine’s context.
  • defined — Tests whether a value is defined (not undef).
  • do — Reads, compiles, and executes a Perl file, returning the value of
  • pos — Gets or sets the position of the last m//g match on a string.
  • prototype — Returns the prototype string of a subroutine, or undef if none.
  • ref — Returns the reference type or blessed class name of a value.
  • require — Loads and executes a Perl file or module, caching it in %INC so
  • reset — Resets ?? (one-shot match) operators so they can match again.
  • tie — Binds a variable to an object class, enabling magical access
  • tied — Returns the underlying object of a tied variable.
  • undef — Produces the undefined value, or undefines an existing variable.
  • untie — Removes the tie binding from a variable, restoring normal access.
  • vec — Treats a string as a bit vector and accesses individual bitfields.

String Functions

  • chomp — Removes the trailing record separator (default: newline) from each
  • chop — Removes the last character from each argument
  • chr — Converts a Unicode code point number to the corresponding character.
  • crypt — One-way password hashing using the system crypt(3) library.
  • hex — Interprets a hexadecimal string as an integer.
  • index — Finds the first occurrence of a substring within a string.
  • join — Concatenates a list of values into a single string with a separator.
  • lc — Converts a string to lowercase.
  • lcfirst — Lowercases the first character of a string.
  • length — Returns the number of characters in a string.
  • oct — Interprets a string as an octal number, with automatic prefix dispatch.
  • ord — Returns the Unicode code point of the first character of a string.
  • pack — Converts a list of values into a binary string according to a template.
  • quotemeta — Escapes all non-“word” characters with a preceding backslash.
  • reverse — Reverses a list of values or the characters of a string, depending
  • rindex — Finds the last occurrence of a substring within a string.
  • sprintf — Formats a string according to a printf-style format specification.
  • substr — and 3-argument forms)
  • uc — Converts a string to uppercase.
  • ucfirst — Uppercases the first character of a string.
  • unpack — Extracts values from a binary string according to a template.

Time Functions

  • gmtime — Converts a Unix timestamp to UTC (Greenwich Mean Time), returning
  • localtime — Converts a Unix timestamp to the local time zone, returning either
  • time — Returns the number of seconds since the Unix epoch.

alarm

Schedules a SIGALRM signal to be delivered after a given number of seconds.

Arranges for a SIGALRM to be delivered to the process after the specified number of seconds. If an alarm was already pending, the old alarm is cancelled and the number of seconds that were remaining is returned. Calling alarm(0) cancels the current alarm without setting a new one.

Commonly used with eval/die or $SIG{ALRM} to implement timeouts:

eval {
    local $SIG{ALRM} = sub { die "timeout\n" };
    alarm(10);
    # ... long operation ...
    alarm(0);
};

Negative values are clamped to 0.

Synopsis

my $remaining = alarm(30);   # fire SIGALRM in 30 seconds
alarm(0);                    # cancel any pending alarm

See Also

sleep, alarm, POSIX

backticks

Executes a shell command and captures its standard output.

Passes the command string to /bin/sh -c "..." for execution. In scalar context, returns the entire captured stdout as a single string. In list context, returns a list of lines (each including its trailing newline), split on $/ (input record separator).

After execution, $? (child error) is set to the child process wait status. Stdin is inherited from the parent so that interactive commands (e.g. tput) work correctly; stderr is also inherited.

Synopsis

my $output = `command`;
my $output = qx{command};
my @lines  = `command`;

See Also

system, exec, open

binmode

Sets the I/O discipline (encoding layer) for a filehandle.

Configures the encoding layer on a filehandle. When called with just a filehandle, sets binary mode (disabling any CRLF translation, though Linux does not perform CRLF translation by default). When called with a layer string, applies the specified encoding.

Currently recognized layers:

  • :utf8, :encoding(UTF-8) – mark the handle as UTF-8
  • :raw, :bytes – mark the handle as raw bytes

Returns true on success, false on failure.

Synopsis

binmode(STDOUT);              # set binary mode (no CRLF translation)
binmode(STDOUT, ":utf8");     # set UTF-8 encoding layer
binmode(STDOUT, ":raw");      # remove all layers
binmode($fh, ":encoding(UTF-8)");

See Also

open, PerlIO, Encode

bless

Associates a reference with a package, turning it into an object.

Marks the referent so that method calls will dispatch through the given class (or the current package if no class is specified). The reference is modified in place and also returned, enabling the common return bless {}, $class pattern.

Handles multiple referent types: for Av and Hv containers the blessing is stored via interior mutability on the container itself. For Rv wrapping an Av or Hv, the inner container is blessed. For scalar references, a new blessed RvInner is created.

Synopsis

my $obj = bless {}, 'MyClass';
my $obj = bless {};              # blesses into current package
my $obj = bless \$scalar, 'Foo';
my $obj = bless [], 'Bar';

See Also

ref, Scalar::Util

caller

Returns information about the calling subroutine’s context.

In scalar context, returns the package name of the calling subroutine. In list context, returns a multi-element list with (at minimum) package, filename, and line number. When called with a numeric argument N, returns information about the Nth stack frame above the current subroutine:

IndexField
0package name
1filename
2line number
3subroutine
4hasargs
5wantarray

Returns undef (scalar) or an empty list if the requested stack depth exceeds the actual call depth. caller(0) returns info from call_stack.last() (the current sub’s frame); caller(1) looks one frame further up.

When the op has WANT bits set to 0 (unknown), the context is inherited from the enclosing call frame, matching perl5 behavior.

Synopsis

my $package = caller;                  # scalar: package name
my ($pkg, $file, $line) = caller;      # list: basic triple
my ($pkg, $file, $line, $sub, ...) = caller(0);
my @info = caller(1);                  # caller's caller

See Also

die, warn, caller

chdir

Changes the process’s current working directory.

Changes the working directory to the specified path. If called with no argument or an empty string, changes to the directory specified by the HOME environment variable (or / if HOME is not set).

Returns 1 on success, undef on failure.

Synopsis

chdir("/tmp") or die "Cannot chdir: $!";
chdir($dir);
chdir();         # changes to $HOME
chdir("");       # also changes to $HOME

See Also

mkdir, rmdir, Cwd, getcwd

chmod

Changes file permissions on one or more files.

Changes the permissions of each listed file to MODE. The first argument is the numeric mode (typically given as an octal literal); the remaining arguments are file paths. Returns the number of files successfully changed. Preserves file-type bits (S_IFMT) from the existing mode and replaces only the permission bits (lower 12 bits).

Synopsis

chmod 0755, $file1, $file2;
chmod 0644, @files;
my $count = chmod 0600, glob("*.conf");

See Also

chown, stat, umask

chomp

Removes the trailing record separator (default: newline) from each argument. Returns the total number of characters removed.

Respects $/ (input record separator). When $/ is undef (slurp mode), chomp is a no-op. When $/ is "" (paragraph mode), removes all trailing newlines. Handles CRLF line endings — \r\n counts as 2 characters removed.

Synopsis

$count = chomp($var);
$count = chomp(@list);
chomp $var;

See Also

chop, $/

chop

Removes the last character from each argument. Returns the last character removed (from the final argument).

Unlike chomp, chop always removes exactly one character regardless of $/. Operates on each element when given an array. Returns the last character removed. Correctly handles multi-byte UTF-8 characters.

Synopsis

$char = chop($var);
$char = chop(@list);
chop $var;

See Also

chomp

chown

Changes the owner and group of one or more files.

Changes the owner and group of each listed file. The first argument is the numeric UID, the second is the numeric GID, and the remaining arguments are file paths. A value of -1 for either UID or GID means “don’t change that field” (converted to u32::MAX for the libc call). Uses libc::chown (follows symlinks). Returns the number of files successfully changed.

Synopsis

chown $uid, $gid, @files;
chown 0, 0, '/etc/shadow';
chown -1, $gid, @files;    # -1 leaves uid unchanged

See Also

chmod, stat

chr

Converts a Unicode code point number to the corresponding character.

Returns the character represented by the given code point. Negative values produce an empty string. Invalid Unicode code points (e.g. surrogates) produce the replacement character U+FFFD.

Synopsis

$char = chr(65);        # "A"
$char = chr(0x263A);    # "☺"

See Also

ord

close

Closes a filehandle, flushing any buffered output.

Closes the filehandle and releases its resources from the global FILEHANDLE_TABLE. For in-memory filehandles (opened against a scalar reference), the accumulated buffer is written back to the target variable. For pipe filehandles, the child process’s stdin is closed and waitpid is called; $? is set to the child’s exit status.

Both lexical ($fh) and bareword (FH) filehandle forms are supported. The standard handles STDIN/STDOUT/STDERR are recognized by name.

Returns 1 on success, undef on failure.

Synopsis

close($fh) or die "Cannot close: $!";
close(FH);

See Also

open, eof, $?

closedir

Closes a directory handle, releasing its resources.

Removes the directory handle from the global DIRHANDLE_TABLE. After closing, the handle is no longer valid for readdir or other directory operations.

Returns 1 on success, undef if the handle was not found or already closed.

Synopsis

closedir($dh);
closedir(DH);

See Also

opendir, readdir

crypt

One-way password hashing using the system crypt(3) library.

Calls the system crypt(3) function. The SALT determines the hashing algorithm (DES, MD5, SHA-256, SHA-512, etc. depending on the platform). The result can be compared against a stored hash to verify a password without storing the plaintext.

Synopsis

my $hashed = crypt($plaintext, $salt);
if (crypt($input, $hashed) eq $hashed) { ... }  # verify

See Also

Digest::SHA, Digest::MD5

defined

Tests whether a value is defined (not undef).

Pops one value from the stack and pushes 1 if it is defined, 0 otherwise. For code values (Cv), “defined” means the CV has actual executable code and is not merely an empty stub. For all other types, delegates to Sv::is_defined().

Synopsis

if (defined $x)        { ... }
if (defined &func)     { ... }   # is sub defined (not a stub)?
if (defined $hash{$k}) { ... }

See Also

undef, exists

delete

Removes the specified element(s) from a hash or array.

For hashes, removes the key-value pair and returns the deleted value (or undef if the key did not exist). Read-only hashes (such as %!) and Hash::Util-restricted hashes reject deletion with an error. For arrays, sets the element to undef without changing the array size; use splice to actually shrink.

Stash-backed hashes (accessed via %{"Package::"}) route the deletion through the PackageRegistry so the symbol is removed from the live stash, not just from the snapshot Hv.

Synopsis

delete $hash{$key};
delete @hash{@keys};               # hash slice
delete $array[$index];
my $val = delete $hash{$key};       # returns deleted value

See Also

exists, each, keys, splice

die

Raises an exception, terminating execution unless caught by an eval block or a try/catch construct.

If called with a string argument, raises that string as the exception message. If the string does not end with a newline, Perl appends at FILE line LINE. If called with a reference (e.g. a hashref or blessed object), the reference is propagated as the exception value in $@.

When called with no arguments (or an empty string / undef), re-raises the current value of $@. If $@ is also empty, dies with "Died".

If there is an active try block (from use feature 'try'), the exception is caught and execution transfers to the catch block instead of propagating.

Synopsis

die "something went wrong";
die "Error at $file line $line\n";
die $exception_object;
die;                              # re-raises $@

See Also

warn, eval

do

Reads, compiles, and executes a Perl file, returning the value of the last expression evaluated.

Unlike require, do FILE:

  • Always re-reads and re-executes the file (no %INC caching).
  • Does not die on failure; instead it returns undef and sets $@ to the error message.
  • Does not inherit caller lexicals (unlike eval STRING).
  • Evaluates the file in the current package context.

On success, returns the value of the last expression evaluated in the file and clears $@. On compilation or runtime error, returns undef and sets $@.

Synopsis

do "config.pl";
my $result = do "/path/to/file.pm";

See Also

require, eval, use

each

Returns the next key-value pair from a hash’s internal iterator.

Advances the hash’s internal iterator and returns the next entry. In list context, pushes a (key, value) pair. In scalar context, returns only the key. When the iterator is exhausted, returns an empty list (list context) or undef (scalar context), and the iterator resets automatically for the next traversal.

Note: keys and values reset the same internal iterator, so calling them during an each loop will restart iteration.

Synopsis

while (my ($k, $v) = each %hash) { ... }
while (my $k = each %hash) { ... }    # scalar context: key only

See Also

keys, values, exists, delete

eof

Tests whether the next read on a filehandle will return end-of-file.

Returns true (1) when the filehandle has reached end-of-file, or an empty string (false) otherwise. Called without arguments, eof() checks the filehandle most recently read via <> or readline.

If the filehandle is invalid or has never been read, returns true.

Synopsis

if (eof($fh))  { ... }   # test specific filehandle
if (eof())     { ... }   # test last-read filehandle
if (eof)       { ... }   # same as eof()

See Also

readline, read, tell

exec

Replaces the current process image with a new program via execvp(3).

On success, exec does not return because the current process image is replaced entirely. If exec does return, it means the call failed, and the return value is false.

In the single-argument form, shell metacharacters trigger execution via /bin/sh -c. If no metacharacters are detected, the command is executed directly. In the multi-argument form, the first argument is the program and subsequent arguments are passed directly (no shell involvement).

The child inherits Perl’s %ENV (the OS environment is replaced before exec).

Synopsis

exec("ls -la");              # shell form
exec("ls", "-la");           # list form (no shell)
exec { "ls" } "ls", "-l";   # indirect object form

See Also

system, fork, exec

exists

Tests whether a hash key or array index is present in the container.

Pops a key and a container (hash or array) from the stack. For hashes, checks whether the key is present (even if the associated value is undef). For arrays, checks whether the index is within bounds and the element has not been deleted. Pushes 1 (true) or 0 (false).

When the container is a stash (package symbol table accessed via %{"Package::"}), checks for the existence of any symbol type (scalar, array, hash, or code) with the given name.

Synopsis

if (exists $hash{$key})   { ... }
if (exists $array[$idx])  { ... }
if (exists $stash{$name}) { ... }   # stash symbol lookup

See Also

defined, delete, keys

exit

Terminates the program with the given exit status.

Evaluates the argument as an integer exit status and terminates the process. If no argument is given, exits with status 0. END blocks and object destructors are run before exit (in perl5; pperl currently calls std::process::exit directly).

In daemon mode (config.daemon_mode), exit does not terminate the process but instead returns an Err(RuntimeError::Exit) so the caller can handle it.

Synopsis

exit;          # exits with status 0
exit 0;        # success
exit 1;        # failure
exit $code;

See Also

die, END blocks

fileno

Returns the underlying OS file descriptor for a Perl filehandle.

Returns the numeric file descriptor associated with the given filehandle, or undef if the handle is not connected to a real OS file descriptor (e.g. in-memory handles, closed handles).

Accepts bareword filehandles (STDIN), lexical filehandles ($fh), and IO::Handle / IO::File objects (blessed hashrefs with __fh_id).

The standard streams always return their well-known descriptors: STDIN = 0, STDOUT = 1, STDERR = 2.

Synopsis

$fd = fileno(STDIN);     # 0
$fd = fileno(STDOUT);    # 1
$fd = fileno($fh);       # fd number or undef

See Also

open, close, IO::Handle

fork

Creates a child process by duplicating the current process.

Calls POSIX fork(2) to create a new child process. Both parent and child continue execution from the point of the fork call.

Returns:

  • undef on failure (fork could not be performed)
  • 0 in the child process
  • The child’s PID (positive integer) in the parent process

Synopsis

my $pid = fork();
die "fork failed: $!" unless defined $pid;
if ($pid == 0) {
    # child process
    exit(0);
} else {
    # parent process, $pid is child's PID
    waitpid($pid, 0);
}

See Also

exec, wait, waitpid, exit

getc

Reads a single character from a filehandle.

Returns the next character from the specified filehandle as a one-character string. When called without arguments, reads from STDIN (filehandle id 0). Returns undef at end-of-file or on error.

Accepts both lexical ($fh) and bareword (FH) filehandles.

Synopsis

$ch = getc($fh);   # one character from $fh
$ch = getc();      # one character from STDIN

See Also

readline, read, eof

glob

Expands shell-style filename patterns into matching pathnames.

Expands the given pattern using shell globbing rules. Supports *, ?, [charset], {alt1,alt2} (brace expansion), and ~ (home directory). In list context returns all matches; in scalar context returns the next match (iterating on successive calls with the same pattern).

The <PATTERN> syntax is equivalent to glob(PATTERN).

When File::Glob is loaded, delegates to bsd_glob semantics where whitespace in patterns is treated literally. Otherwise, the default behaviour splits the pattern on whitespace to process multiple globs.

Results are returned sorted and deduplicated.

Synopsis

@files = glob('*.pl');
@files = glob('lib/**/*.pm');
@files = <*.txt>;         # angle-bracket form

See Also

opendir, readdir, File::Glob

gmtime

Converts a Unix timestamp to UTC (Greenwich Mean Time), returning the same structure as localtime but without timezone or DST adjustments.

Identical to localtime in interface, but all values are in UTC rather than the local timezone. The $isdst field is always 0. If called without an argument, uses the current time.

The implementation uses a pure-Rust UTC calculation (no libc dependency), correctly handling negative timestamps (dates before the epoch) and leap years.

Synopsis

my @t = gmtime(0);     # (0,0,0,1,0,70,4,0,0) - epoch in UTC
my $s = gmtime(0);     # "Thu Jan  1 00:00:00 1970"
my @t = gmtime();      # current time in UTC

See Also

localtime, time, POSIX::strftime

hex

Interprets a hexadecimal string as an integer.

Accepts an optional 0x or 0X prefix. Underscores are allowed as digit separators. Leading and trailing whitespace is stripped. Returns 0 for empty or non-hex input.

Synopsis

$val = hex("ff");       # 255
$val = hex("0xDEAD");   # 57005
$val = hex("ff_ff");    # 65535

See Also

oct

index

Finds the first occurrence of a substring within a string.

Searches STRING for the first occurrence of SUBSTR starting at character position START (default 0). Returns the zero-based character position of the match, or -1 if the substring is not found.

Positions are in characters (not bytes), so this works correctly with multi-byte UTF-8 strings. An ASCII fast path avoids the overhead of character-index conversion when both operands are pure ASCII.

When the OPpTARGET_MY flag (0x10) is set, the result is stored directly into the pad slot rather than pushed to the stack.

Synopsis

$pos = index($string, $substr);
$pos = index($string, $substr, $start);

See Also

rindex, substr, pos

join

Concatenates a list of values into a single string with a separator.

Takes a separator string followed by a list of values (from a pushmark-delimited argument list on the stack). The separator is inserted between each adjacent pair of values. Array arguments are flattened. Returns the resulting string; an empty string when no list values are given.

Uses Cow<str> for the separator and parts to avoid allocation when the underlying SVs are already string-typed.

Synopsis

$str = join(',', @array);
$str = join(' ', 'hello', 'world');
$str = join('', @chars);           # no separator

See Also

split, sprintf

keys

Returns all keys of a hash, or the number of keys in scalar context.

In list context, pushes all keys of the hash onto the stack as individual string values. In scalar context, returns the number of key-value pairs. Accepts both %hash and $hashref (auto- dereferences). As a side effect, resets the hash’s internal iterator used by each.

Synopsis

my @k = keys %hash;
my $count = keys %hash;         # scalar context: number of keys
for my $key (keys %hash) { ... }

See Also

values, each, exists, delete

kill

Sends a signal to one or more processes or process groups.

The first argument is the signal, specified either as a name ('TERM', 'HUP', with or without SIG prefix) or as a number. The remaining arguments are PIDs to signal.

If the signal is negative, the absolute value is sent to the process group identified by each PID (using kill(-sig, -abs(pid))).

Signal 0 does not actually send a signal but checks whether the process exists and is reachable (permission check).

Returns the number of processes successfully signaled.

Synopsis

kill 'TERM', $pid;                # send SIGTERM by name
kill 15, $pid;                    # send SIGTERM by number
kill 'TERM', $pid1, $pid2;        # signal multiple processes
kill 0, $pid;                     # check if process exists
kill -15, $pgrp;                  # signal a process group

See Also

%SIG, POSIX signals, fork, waitpid

lc

Converts a string to lowercase.

Returns a lowercased copy of STRING. For ASCII input, an in-place byte mutation fast path avoids allocation when a TARG pad slot is available. For Unicode input, Rust’s to_lowercase() is used, which handles case mappings that change byte length.

Synopsis

$lower = lc($string);
$lower = lc('HELLO');   # "hello"

See Also

uc, ucfirst, lcfirst

lcfirst

Lowercases the first character of a string.

Returns a copy of STRING with the first character converted to lowercase and all other characters left unchanged. For ASCII first characters, a single-byte in-place mutation is used. For Unicode first characters, to_lowercase() may change the byte length.

Returns an empty string when given an empty string.

Synopsis

$result = lcfirst($string);
$result = lcfirst('HELLO');   # "hELLO"

See Also

ucfirst, uc, lc

length

Returns the number of characters in a string.

Returns the character count (not byte count) for Unicode correctness. length(undef) returns undef, matching perl5 behavior.

Synopsis

$len = length($string);
$len = length("café");   # 4 (characters, not bytes)

See Also

substr

link

Creates a hard link to an existing file.

Creates a new directory entry NEWFILE that refers to the same inode as OLDFILE. Both paths must be on the same filesystem. The link count of the file is incremented. Uses std::fs::hard_link. Returns 1 on success, 0 on failure (and sets $!).

Synopsis

link $oldfile, $newfile or die "Cannot link: $!";
link '/data/original', '/data/hardlink';

See Also

symlink, unlink, rename

localtime

Converts a Unix timestamp to the local time zone, returning either a 9-element list or a human-readable string depending on context.

If called without an argument (or with undef), uses the current time. In list context, returns a 9-element list:

IndexFieldRange
0$sec0-60 (60 = leap)
1$min0-59
2$hour0-23
3$mday1-31
4$mon0-11 (Jan = 0)
5$yearyears since 1900
6$wday0-6 (Sun = 0)
7$yday0-365
8$isdstDST flag (-1, 0, 1)

In scalar context, returns a ctime-style string like "Thu Jan 1 00:00:00 1970".

Uses libc::localtime_r for accurate timezone/DST handling.

Synopsis

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    = localtime($time);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    = localtime();           # uses current time
my $str = localtime($time);  # "Thu Jan  1 00:00:00 1970"

See Also

gmtime, time, POSIX::strftime, POSIX::mktime

lstat

Returns file metadata without following symbolic links.

Identical to stat except that if the target is a symbolic link, lstat returns information about the link itself rather than the file it points to. Uses std::fs::symlink_metadata. Returns the same 13-element list as stat, or an empty list on failure.

Synopsis

my @s = lstat($symlink);
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
    $atime, $mtime, $ctime, $blksize, $blocks) = lstat($path);
lstat _;               # reuse last stat buffer

See Also

stat, readlink, symlink

m//

Tests a string against a regular expression pattern.

Matches the target string against the compiled regex pattern. The target is determined by context:

  • If targ > 0: reads from the pad slot (lexical $str =~ /pat/)
  • If STACKED flag is set: pops from the stack (expression =~ /pat/)
  • Otherwise: matches against $_ (bare /pat/)

In scalar context, returns true (1) or false (“”) indicating whether the pattern matched. Sets $1, $2, etc. for capture groups and updates pos() for /g matches.

In list context without /g, returns the list of captured substrings ($1, $2, …), or (1) if there are no captures.

In list context with /g, returns all matches: if the pattern has captures, returns all captured groups from all matches; otherwise returns all matched substrings.

Patterns are cached per-op for static regexes; dynamic patterns (from regcomp) use a HashMap-based cache.

Synopsis

if ($str =~ /pattern/)       { ... }
if ($str =~ /pattern/flags)  { ... }
my @captures = ($str =~ /(\w+)/g);
/bare pattern/;                        # matches against $_

See Also

s///, split, qr//, perlre, perlop

mkdir

Creates a new directory with the specified permissions.

Creates a directory with the given name. The optional second argument specifies the permissions mode (default 0777), which is modified by the process umask before being applied. If no arguments are given, uses $_ as the directory name.

Returns 1 on success, 0 on failure. On failure, sets $! to the OS error code (e.g. EEXIST, EACCES).

Synopsis

mkdir("newdir", 0755) or die "Cannot mkdir: $!";
mkdir("newdir");         # default mode 0777 (modified by umask)
mkdir $_;                # uses $_ if no argument

See Also

rmdir, chmod, umask

oct

Interprets a string as an octal number, with automatic prefix dispatch.

Dispatches on prefix: 0x/x for hexadecimal, 0b/b for binary, 0o for explicit octal (Perl 5.34+), otherwise plain octal. Underscores are allowed as digit separators. Leading whitespace is stripped.

Synopsis

$val = oct("77");       # 63
$val = oct("0xff");     # 255 (hex)
$val = oct("0b1010");   # 10  (binary)
$val = oct("0o77");     # 63  (explicit octal)

See Also

hex

open

Opens a file, pipe, or duplicates a file descriptor, associating it with a filehandle.

Supports both the 2-argument form (mode embedded in the filename string) and the 3-argument form (separate mode and filename). The 3-argument form is preferred as it avoids shell metacharacter issues.

ModeMeaning
<Read
>Write (truncate)
>>Append
+<Read/write (existing file)
+>Write/read (truncate)
|-Pipe to command
-|Pipe from command
>& / <&Dup filehandle
>&= / <&=Dup by file descriptor number

In-memory I/O is supported via scalar references: open $fh, '>', \$buf.

Returns 1 on success, undef on failure. Sets $! on error.

Synopsis

open(my $fh, '<', $filename);       # read
open(my $fh, '>', $filename);       # write (truncate)
open(my $fh, '>>', $filename);      # append
open(my $fh, '+<', $filename);      # read/write
open(my $fh, '|-', @cmd);           # pipe to command
open(my $fh, '-|', @cmd);           # pipe from command
open(my $fh, '>&', $other_fh);      # dup for writing
open(my $fh, '<', \$scalar);        # in-memory I/O
open(FH, "<$filename");             # 2-arg form

See Also

close, binmode, print, read, sysopen, perlopentut

opendir

Opens a directory for reading via a directory handle.

Associates a directory handle with the named directory. The handle can then be used with readdir, rewinddir, seekdir, telldir, and closedir. All directory entries (including . and ..) are loaded eagerly into a Vec at open time.

The directory handle variable receives a numeric ID stored in the global DIRHANDLE_TABLE. Both lexical (my $dh) and bareword (DH) forms are supported.

Returns 1 on success, undef on failure.

Synopsis

opendir(my $dh, $dir) or die "Cannot open $dir: $!";
opendir(DH, "/tmp");

See Also

readdir, closedir, rewinddir, File::Find

ord

Returns the Unicode code point of the first character of a string.

Returns the code point of the first character. For an empty string, returns 0. Multi-character strings return only the first character’s code point.

Synopsis

$code = ord("A");       # 65
$code = ord("☺");      # 9786

See Also

chr

pack

Converts a list of values into a binary string according to a template.

Takes a TEMPLATE string and a LIST of values and packs them into a binary string. The template is a sequence of format characters, each optionally followed by a repeat count or * (consume all remaining).

Common format characters:

  • a, A, Z – ASCII string (null-padded, space-padded, null-terminated)
  • c, C – signed/unsigned char (8-bit)
  • s, S – signed/unsigned short (16-bit native)
  • l, L – signed/unsigned long (32-bit native)
  • q, Q – signed/unsigned quad (64-bit native)
  • n, N – unsigned short/long in network (big-endian) byte order
  • v, V – unsigned short/long in VAX (little-endian) byte order
  • f, d – single/double-precision float (native)
  • H, h – hex string (high/low nybble first)
  • w – BER compressed integer
  • U – Unicode code point
  • x – null byte padding
  • X – back up one byte
  • @ – null-pad to absolute position
  • (...)N – group with repeat count

Whitespace and #-comments in templates are ignored.

Synopsis

my $bin = pack("A10", "hello");         # space-padded string
my $bin = pack("NnA*", $long, $short, $str);
my $bin = pack("(A2)*", @pairs);        # group repeat

See Also

unpack, vec

pipe

Creates a unidirectional pipe pair (read end + write end).

Calls the POSIX pipe(2) system call to create a connected pair of file descriptors. The first argument receives the read end and the second receives the write end, both as filehandles.

Typically used together with fork to set up inter-process communication. Returns 1 on success or undef on failure (with $! set to the system error).

Synopsis

pipe(my $read_fh, my $write_fh) or die "pipe: $!";

See Also

open, close

pop

Removes and returns the last element of an array.

Removes the last element from the array and returns it. If the array is empty, returns undef. The array shrinks by one element.

Synopsis

my $val = pop @array;
pop @array;              # discard removed element

See Also

push, shift, unshift, splice

pos

Gets or sets the position of the last m//g match on a string.

As an rvalue, returns the character offset where the most recent global (/g) match left off, or undef if no match has been performed on that string. As an lvalue, sets the starting position for the next m//g match.

Without an argument, operates on $_. Positions are stored in a side table (pos_table) keyed by string content.

Synopsis

$str =~ /pattern/g;
my $p = pos($str);       # position after last /g match
pos($str) = 0;           # reset match position
my $p = pos;             # defaults to $_

See Also

m//g, reset

print

Outputs a list of values to a filehandle.

Pops values from the stack and writes them to the target filehandle. When the STACKED flag is set, the first value after the mark is the filehandle; otherwise, STDOUT is used.

The output field separator $, ($OFS) is inserted between values, and the output record separator $\ ($ORS) is appended after all values. For say (which shares this implementation), a newline is always appended after the output.

Supports Gv (typeglob), bareword string, and lexical (integer ID) filehandle forms. In-memory filehandles (open my $fh, '>', \$buf) write to the backing buffer.

Returns 1 on success (Perl convention).

Synopsis

print "hello world\n";
print STDERR "error message\n";
print $fh @data;
print @array;                # uses $, and $\

See Also

say, printf, write, perlvar

printf

Formats and prints a list of values to a filehandle using a format string.

Equivalent to print FILEHANDLE sprintf(FORMAT, LIST). Formats the list of values according to FORMAT (see sprintf for format specifiers) and writes the result to the specified filehandle (or STDOUT if none is given).

Like print, honors $\ (output record separator), which is appended after the formatted output. The filehandle is determined by the stacked flag on the op, exactly as in pp_print.

Internally delegates to pp_sprintf for formatting, then writes the resulting string to the filehandle.

Synopsis

printf FORMAT, LIST;
printf FILEHANDLE FORMAT, LIST;

See Also

sprintf, print, say

prototype

Returns the prototype string of a subroutine, or undef if none.

Accepts a code reference, a reference to a CV, or a string naming the subroutine. For CORE:: builtins, returns the well-known prototype from a built-in table (e.g. \@@ for push, ;$ for defined). For user-defined subs, looks up the CV in the stash and returns its prototype. Unqualified names are resolved in the current package. Returns undef when the subroutine has no prototype or cannot be found.

Synopsis

my $proto = prototype(\&mysub);         # "$$"
my $proto = prototype('CORE::push');    # "\@@"
my $proto = prototype('main::func');    # look up by name

See Also

sub, CORE::

push

Appends one or more values to the end of an array, growing it as needed.

Treats the first argument as an array and appends all subsequent arguments to it. Array arguments are flattened (their elements are pushed individually, not as nested arrays). Returns the new number of elements in the array.

Synopsis

push @array, $value;
push @array, @more_values;
push @array, $v1, $v2, $v3;
my $new_len = push @array, $value;

See Also

pop, unshift, shift, splice

qr

Creates a precompiled regular expression object.

Compiles the pattern (from OpData::Pm or a runtime-interpolated string in runtime_regex_pattern) into a regex and wraps it as a blessed reference in the Regexp class. The stringified form is (?^flags:pattern), matching perl5 behaviour.

The returned object can be used directly in pattern matches, interpolated into other patterns, or stored in variables, arrays, and hashes for later use.

Synopsis

my $re = qr/pattern/flags;
$str =~ $re;             # use in match
$str =~ /${re}suffix/;   # interpolate into pattern

See Also

m//, s///, perlre, perlop

quotemeta

Escapes all non-“word” characters with a preceding backslash.

Returns a version of the string where every non-word character ([^A-Za-z0-9_]) is preceded by \. Useful for interpolating literal strings into regular expressions. If all characters are already word characters, the input is returned unchanged with no allocation.

Synopsis

my $safe = quotemeta($string);
$string =~ /\Q$pattern\E/;     # \Q invokes quotemeta

See Also

m//, s///

rand

Returns a pseudo-random floating-point number.

Returns a pseudo-random floating-point number greater than or equal to 0 and less than EXPR (or 1 if omitted). If EXPR evaluates to zero, 1.0 is used instead. Uses the interpreter’s StdRng state, which can be seeded via srand. Not suitable for cryptographic purposes.

Synopsis

my $r = rand;          # 0 <= $r < 1
my $r = rand(10);      # 0 <= $r < 10
my $r = rand($n);

See Also

srand, int

read

Reads a fixed number of bytes from a filehandle into a scalar buffer.

Attempts to read LENGTH bytes from the filehandle FH into the scalar BUFFER. When OFFSET is supplied, data is placed at that byte position within the buffer (the buffer is null-padded if OFFSET exceeds its current length).

Returns the number of bytes actually read (which may be less than LENGTH), 0 at end-of-file, or undef on error. The target buffer is written back through lvalue targets (pad slot or stash variable).

Synopsis

$bytes = read($fh, $buf, $length);
$bytes = read($fh, $buf, $length, $offset);

See Also

sysread, readline, eof

readdir

Reads entries from an open directory handle.

In scalar context, returns the next directory entry name as a string, or undef when the directory is exhausted. In list context, returns all remaining entries as a list of strings.

Entry names are bare filenames (no path prefix). The entries . and .. are included, matching perl5 behavior.

Synopsis

my $entry = readdir($dh);      # scalar: one entry
my @entries = readdir($dh);     # list: all remaining entries

See Also

opendir, closedir, rewinddir, glob

readline

PP function for readline

Reads one or more records from a filehandle.

In scalar context, returns the next record from the filehandle (including the record separator, typically a newline). Returns undef at end-of-file. In list context, returns all remaining records.

The record separator is controlled by $/:

  • "\n" (default) – line-at-a-time mode
  • "" – paragraph mode (reads until a blank line)
  • undef – slurp mode (reads the entire file)
  • \N (reference to integer) – fixed-length record mode (reads N bytes)
  • arbitrary string – reads until that separator is found

When the STACKED flag is set, the result is assigned to the lvalue target on the stack (e.g. my $line = <$fh>).

Supports both lexical ($fh) and bareword (FH) filehandles, as well as IO::Handle / IO::File objects.

Synopsis

$line  = <$fh>;           # scalar: next line
@lines = <$fh>;           # list: all remaining lines
$line  = readline($fh);   # explicit function form

See Also

eof, read, getc, chomp

readlink

Returns the target of a symbolic link.

Returns the filename pointed to by the symbolic link EXPR, or $_ if omitted. Returns undef on error (e.g., not a symlink, file not found) and sets $!. Uses std::fs::read_link and converts the result to a string via to_string_lossy.

Synopsis

my $target = readlink $symlink;
my $target = readlink '/usr/bin/perl';
my $target = readlink;     # readlink($_)

See Also

symlink, lstat, stat

ref

Returns the reference type or blessed class name of a value.

Pops one value and pushes a string describing its reference type. For blessed references, returns the class name (e.g. "MyClass"). For unblessed references, returns the underlying type: "SCALAR", "ARRAY", "HASH", "CODE", "REF", "GLOB", or "Regexp". For non-references, returns the empty string "".

Delegates to Sv::ref_type() for the actual type determination.

Synopsis

my $type = ref $ref;           # "HASH", "ARRAY", "SCALAR", "CODE", etc.
my $class = ref $object;       # "MyClass" for blessed references
if (ref $x) { ... }            # true if $x is a reference

See Also

bless, Scalar::Util

rename

Renames or moves a file.

Renames the file OLDNAME to NEWNAME. If NEWNAME already exists, it is replaced atomically (on the same filesystem). Cannot move files across filesystem boundaries. Uses std::fs::rename. Returns 1 on success, 0 on failure (and sets $!).

Synopsis

rename $old, $new or die "Cannot rename: $!";
rename 'file.tmp', 'file.dat';

See Also

unlink, link, File::Copy

require

Loads and executes a Perl file or module, caching it in %INC so that subsequent calls for the same module are no-ops.

Behaviour depends on the argument type:

  1. Version number (numeric or v-string): asserts that the Perl version is at least that value. pperl claims 5.42 compatibility, so any 5.x requirement succeeds; 6+ dies.

  2. Bareword / string: converts Foo::Bar to Foo/Bar.pm, then searches @INC for the file. If already present in %INC, the call is a no-op. Otherwise the file is compiled and executed in a sub-interpreter, subs are registered, and %INC is updated.

Dies with "Can't locate ..." if the file is not found. Returns 1 on success.

Synopsis

require 5.036;              # version check
require Carp;               # load Carp.pm via @INC
require "config.pl";        # load a file by path

See Also

use, do, eval, @INC, %INC

reset

Resets ?? (one-shot match) operators so they can match again.

The ?? operator matches only once between calls to reset. Without an argument, resets all one-shot matches in the current package. With a character-range argument, resets only those in variables whose names fall within the range.

This is a deprecated and rarely used feature. Returns 1.

Implementation status: Stub (no-op). PetaPerl does not currently track ?? match state.

Synopsis

reset;            # reset all ?? in current package
reset 'a-z';      # reset ?? in variables matching range

See Also

m//g, pos

reverse

Reverses a list of values or the characters of a string, depending on context.

In list context, collects all items between the stack mark and the current position (flattening any intermediate arrays), reverses their order, and pushes them back. In scalar context, concatenates all items into a single string and reverses the characters. An ASCII fast path reverses bytes in-place; non-ASCII strings are reversed by Unicode code point.

Synopsis

my @rev = reverse @list;
my @rev = reverse 1, 2, 3;       # (3, 2, 1)
my $rev = reverse "Hello";        # "olleH"

See Also

sort

rewinddir

Resets the position of a directory handle back to the beginning.

Sets the current position of DIRHANDLE back to the beginning of the directory, so that subsequent readdir calls will re-read the directory from the start. The directory must have been previously opened with opendir.

Returns 1 on success, undef on failure.

Synopsis

rewinddir(DIRHANDLE);

See Also

opendir, readdir, closedir

rindex

Finds the last occurrence of a substring within a string.

Searches STRING for the last occurrence of SUBSTR, considering only matches that start at or before character position END (default: end of string). Returns the zero-based character position of the match, or -1 if the substring is not found.

For an empty SUBSTR, returns min(END, length(STRING)) to match perl5 behaviour.

Positions are in characters (not bytes), so this works correctly with multi-byte UTF-8 strings. An ASCII fast path uses rfind directly when both operands are pure ASCII.

When the OPpTARGET_MY flag (0x10) is set, the result is stored directly into the pad slot rather than pushed to the stack.

Synopsis

$pos = rindex($string, $substr);
$pos = rindex($string, $substr, $end);

See Also

index, substr, pos

rmdir

Removes an empty directory.

Deletes the named directory, which must be empty. If no argument is given, uses $_. Returns 1 on success, 0 on failure. On failure, sets $! to the OS error code (e.g. ENOTEMPTY, ENOENT, EACCES).

Only empty directories can be removed; use File::Path::rmtree for recursive deletion.

Synopsis

rmdir("olddir") or die "Cannot rmdir: $!";
rmdir $_;        # uses $_ if no argument

See Also

mkdir, unlink, File::Path

s///

Searches a string for a pattern and replaces matched text.

Compiles the pattern from OpData::Pm, finds matches in the target string, performs the substitution, and updates the source variable in place. Pushes the number of successful substitutions to the stack (0 if no match, which is false in boolean context).

The target string is resolved from:

  • A PadSv op (lexical variable $str =~ s/.../.../)
  • A GvSv op (package variable)
  • A Sassign op (compound (my $x = $y) =~ s/.../.../)

Supports replacement templates with backreferences ($1, $&, etc.) and replacement ops for the /e flag (evaluate replacement as Perl code).

Flags: /g (global), /i (case-insensitive), /m (multiline), /s (single-line), /e (eval replacement), /r (non-destructive, returns modified copy).

Synopsis

$str =~ s/pattern/replacement/;
$str =~ s/pattern/replacement/g;     # global
$str =~ s/pattern/replacement/gi;    # global, case-insensitive
($copy = $orig) =~ s/old/new/g;      # compound assign+subst
s/pattern/replacement/;               # operates on $_

See Also

m//, tr///, perlre, perlop

say

Like print, but automatically appends a newline after the output.

Behaves identically to print except that a newline character is appended after all output (including any $\ record separator). Requires use feature 'say' or use v5.10 or later in perl5.

Internally delegates to pp_print, which detects OpCode::Say and appends the newline to the correct filehandle.

Synopsis

say "hello world";           # prints "hello world\n"
say STDERR "debug info";
say $fh @data;

See Also

print, printf

seek

Sets the read/write position in a filehandle.

Repositions the file pointer for the given filehandle. The WHENCE argument selects the reference point: 0 (SEEK_SET) for the beginning of the file, 1 (SEEK_CUR) for the current position, and 2 (SEEK_END) for the end of the file. Returns 1 on success, 0 on failure.

Also accepts IO::Handle / IO::File objects (blessed hashrefs with __fh_id).

Synopsis

seek($fh, $offset, 0);   # SEEK_SET — absolute position
seek($fh, $offset, 1);   # SEEK_CUR — relative to current
seek($fh, $offset, 2);   # SEEK_END — relative to end

See Also

tell, sysseek, Fcntl

select

Sets the default output filehandle or performs I/O multiplexing.

1-argument form: Sets the default output filehandle (used by print and write when no explicit handle is given) and returns the previously selected handle. With no arguments, returns the current default output handle name without changing it.

4-argument form: Wraps the POSIX select(2) system call. Waits for I/O readiness on the file descriptors encoded in the bit vectors RBITS (readable), WBITS (writable), and EBITS (exceptional). TIMEOUT is in seconds (may be fractional). Returns the number of ready descriptors, or -1 on error.

Synopsis

$prev = select($fh);                                # 1-arg form
$nfound = select($rbits, $wbits, $ebits, $timeout); # 4-arg form

See Also

print, IO::Select, fileno

shift

Removes and returns the first element of an array, shifting all remaining elements down by one index.

Removes the first element from the array, shifts all remaining elements down by one, and returns the removed element. Returns undef if the array is empty.

When the SPECIAL flag is set (bare shift without an explicit array), the implicit target depends on call context: inside a subroutine it reads from @_ (pad slot 0); at file scope it reads from @ARGV. Handles both regular Av and AliasedAv (the typical representation of @_).

Synopsis

my $val = shift @array;
my $arg = shift;          # shifts from @_ inside a sub, @ARGV at top level

See Also

unshift, pop, push, splice

sleep

Suspends execution for the specified number of seconds.

Causes the process to sleep for the given number of whole seconds. Returns the number of seconds actually slept, which may be less than requested if a signal interrupted the sleep.

If Time::HiRes::sleep has been imported into the current package, fractional seconds are supported and the override is used instead. Time::HiRes::sleep dies on negative values.

Without arguments, sleeps indefinitely (until a signal arrives). Negative values are clamped to 0.

Uses libc::sleep() (not std::thread::sleep) so that signals like SIGALRM can interrupt the sleep. If interrupted, any pending $SIG{...} handler is dispatched synchronously before returning.

Synopsis

my $slept = sleep(5);    # pause 5 seconds, returns 5
sleep;                   # sleep forever (until signal)
# with: use Time::HiRes qw(sleep);
sleep(0.5);              # fractional seconds

See Also

alarm, Time::HiRes, select

sort

Sorts a list of values and returns them in sorted order.

Sorts the input list according to the comparison mode determined by the op’s private flags:

  • Default (string): Lexicographic comparison via cmp.
  • Numeric (OPpSORT_NUMERIC, 0x01): Floating-point comparison.
  • Custom block (OPpSORT_BLOCK, 0x20): A CV on the stack is used as the comparator. $a and $b are set in the current package via cached cells and the CV is executed using the multicall pattern (pad + call frame set up once, only $a/$b updates per comparison) with a merge-sort algorithm.
  • Descend (OPpSORT_DESCEND, 0x08): Reverses the final result.

Array arguments are flattened before sorting.

Synopsis

my @sorted = sort @list;                    # lexicographic
my @sorted = sort { $a <=> $b } @list;      # numeric
my @sorted = sort { $b cmp $a } @list;      # reverse lexicographic
my @sorted = sort \&compare, @list;          # named comparator

See Also

reverse, map, grep

splice

Removes and/or replaces elements in an array, returning the removed elements.

Removes $length elements starting at $offset from @array, optionally replacing them with @replacements, and returns the removed elements. Negative $offset counts from the end of the array. If $length is omitted, removes everything from $offset onward. A negative $length leaves that many elements at the end. Replacement arrays are flattened. The array is rebuilt in place.

Synopsis

my @removed = splice @array, $offset;
my @removed = splice @array, $offset, $length;
my @removed = splice @array, $offset, $length, @replacements;
splice @array, 0, 0, @prepend;    # insert at beginning

See Also

push, pop, shift, unshift, delete

split

Splits a string into a list of substrings using a pattern.

Splits EXPR (or $_ if omitted) into substrings at each match of PATTERN. If LIMIT is positive, returns at most LIMIT fields. If LIMIT is negative, behaves as if arbitrarily large. If LIMIT is zero or omitted (default), trailing empty fields are stripped.

Special case: split ' ' (with pmflags & 0x800) uses awk-style semantics – splits on runs of whitespace and ignores leading whitespace. An empty pattern (split //, $str) splits every character.

If a split_targ is set in the op, results are stored directly into the target array (pad slot) instead of the stack. In scalar context, returns the number of fields.

Synopsis

@fields = split /PATTERN/, EXPR, LIMIT;
@fields = split /PATTERN/, EXPR;
@fields = split /PATTERN/;           # splits $_
@fields = split ' ', $string;        # awk-style split
my $count = split /,/, $csv;         # scalar context: count

See Also

join, m//g, perlre

sprintf

Formats a string according to a printf-style format specification.

Takes a format string followed by a list of arguments (from a pushmark-delimited argument list on the stack) and returns the formatted result string. Supports the standard C-style conversion specifiers:

  • %s string, %d/%i signed decimal, %u unsigned decimal
  • %o octal, %x/%X hex, %b/%B binary
  • %e/%E scientific, %f/%F fixed, %g/%G general float
  • %c character (by codepoint), %p pointer, %% literal percent

Flags: - (left-justify), + (force sign), (space for sign), 0 (zero-pad), # (alternate form). Width and precision may be literal or * (consumed from the argument list). Indexed arguments via %N$ are supported. The %vd vector flag formats a string as dot-separated byte values (e.g. version strings).

Uses a zero-allocation fast path: arguments are peeked from the stack as &[Sv] without cloning, and integer formatting uses stack buffers.

Synopsis

$s = sprintf('%d items at $%.2f each', $count, $price);
$s = sprintf('%05x', $hex_val);
$s = sprintf('%-20s', $name);

See Also

printf, join

srand

Seeds the pseudo-random number generator used by rand.

Sets the seed for the random number generator used by rand. If EXPR is provided and defined, its integer value is used. Otherwise, a system-derived seed (nanosecond timestamp XOR PID) is used. Creates a new StdRng instance via seed_from_u64. Returns the seed that was actually used as an integer.

Synopsis

srand(42);             # deterministic sequence
srand;                 # seed from system entropy
my $seed = srand;      # returns the seed used

See Also

rand

stat

Returns file metadata as a 13-element list.

Returns a 13-element list giving the status info for a file. The elements are: dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, blocks. Follows symbolic links (use lstat to stat the link itself). Returns an empty list on failure.

When Time::HiRes::stat has been imported into the caller’s namespace, the atime/mtime/ctime fields are returned as floating-point seconds with nanosecond precision.

Synopsis

my @s = stat($file);
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
    $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
stat $fh;                  # stat on a filehandle
stat _;                    # reuse last stat buffer

See Also

lstat, chmod, chown

stringify

Converts a value to its string representation, respecting overload.

If the value is already a string, passes it through unchanged. If it is a blessed reference with a "" (stringify) overload handler, invokes that handler. Otherwise converts via default stringification (Sv::to_str_cow).

Synopsis

my $s = "$value";    # stringify via interpolation

See Also

ref, overload

substr

and 3-argument forms)

Extracts a substring from a string.

Returns the portion of STRING starting at character position OFFSET. If OFFSET is negative, it counts backwards from the end of the string. When LENGTH is given, at most that many characters are extracted; a negative LENGTH leaves that many characters at the end.

The argument count is encoded in the lower 4 bits of the op’s private flags (set by codegen). When those bits are zero (e.g. bytecode/JSON fallback path), the stack length is used instead.

Synopsis

$part = substr($string, $offset);
$part = substr($string, $offset, $length);

See Also

index, rindex, substr

symlink

Creates a symbolic (soft) link.

Creates a symbolic link LINKNAME that points to TARGET. Unlike hard links, symbolic links can span filesystems and can point to directories. Uses std::os::unix::fs::symlink. Returns 1 on success, 0 on failure (and sets $!).

Synopsis

symlink $target, $linkname or die "Cannot symlink: $!";
symlink '/usr/bin/perl', '/usr/local/bin/perl';

See Also

link, readlink, unlink

sysopen

Opens a file using POSIX open(2) flags for precise mode control.

Unlike the high-level open, sysopen takes explicit POSIX mode flags (from Fcntl) and an optional permissions mask. The flags are passed directly to the underlying open(2) system call.

Arguments on the stack (via mark): filehandle target, filename, mode flags, and optional permissions (defaults to 0666, modified by umask).

Returns 1 on success (the filehandle is stored in the first argument) or undef on failure.

Synopsis

use Fcntl;
sysopen(my $fh, $filename, O_RDWR | O_CREAT, 0644);
sysopen(FH, $path, O_WRONLY | O_TRUNC);

See Also

open, close, Fcntl

sysread

Performs an unbuffered read directly from the OS file descriptor.

Reads up to LENGTH bytes from the filehandle into BUFFER using the underlying read(2) system call, bypassing Perl’s buffered I/O layer. When OFFSET is given, data is placed starting at that byte position within the buffer.

Returns the number of bytes actually read (which may be less than LENGTH), 0 at end-of-file, or undef on error (with $! set to the OS errno).

Warning: Do not mix sysread with buffered I/O (read, readline, <>) on the same filehandle.

Synopsis

$n = sysread($fh, $buf, $length);
$n = sysread($fh, $buf, $length, $offset);

See Also

read, syswrite, sysseek

sysseek

Repositions the OS file pointer, bypassing Perl’s buffered I/O layer.

Calls lseek(2) on the underlying file descriptor to reposition the read/write offset. Unlike seek, this operates at the system-call level and does not interact with Perl’s I/O buffering.

Returns the resulting absolute position (as a “0 but true” string when the position is zero, per perl5 convention), or undef on error (with $! set).

Warning: Do not mix sysseek with buffered I/O (seek, tell) on the same filehandle.

Synopsis

$pos = sysseek($fh, $offset, SEEK_SET);  # absolute
$pos = sysseek($fh, 0, SEEK_CUR);        # query position
$pos = sysseek($fh, 0, SEEK_END);        # query file size

See Also

seek, tell, sysread, syswrite

system

Executes an external command and waits for it to complete.

In the single-argument form, the command string is passed to /bin/sh -c for shell interpretation (pipes, redirects, etc.). In the multi-argument form (list form), the program is executed directly via execvp without a shell, avoiding shell metacharacter interpretation.

Returns the full wait status (same encoding as $?):

  • 0 on success (exit code 0)
  • $? >> 8 extracts the exit code
  • Lower 8 bits contain signal information
  • -1 on failure to execute

The child process inherits Perl’s %ENV (not the OS environment).

Synopsis

my $status = system("ls -la");
my $status = system("program", "arg1", "arg2");
system("echo hello") == 0 or die "system failed: $?";

See Also

exec, fork, waitpid, backticks, $?

syswrite

Performs an unbuffered write directly to the OS file descriptor.

Writes data from SCALAR to the filehandle using the underlying write(2) system call, bypassing Perl’s buffered I/O layer. When LENGTH is specified, at most that many bytes are written. When OFFSET is given, writing starts from that byte position within the scalar.

Returns the number of bytes actually written, or undef on error (with $! set to the OS errno).

Warning: Do not mix syswrite with buffered I/O (print, say) on the same filehandle.

Synopsis

$n = syswrite($fh, $scalar);
$n = syswrite($fh, $scalar, $length);
$n = syswrite($fh, $scalar, $length, $offset);

See Also

sysread, sysseek, print

tell

Returns the current byte position in a filehandle.

Returns the current read/write position (in bytes) for the given filehandle, or for the most recently read filehandle when called without arguments. Returns -1 on error or when no filehandle is available.

The returned value is suitable as the POSITION argument to seek.

Synopsis

$pos = tell($fh);    # position in $fh
$pos = tell();       # position in last-read filehandle

See Also

seek, eof, sysseek

tie

Binds a variable to an object class, enabling magical access through method dispatch.

Associates a variable with a class by calling the class’s TIEHASH, TIEARRAY, TIESCALAR, or TIEHANDLE constructor. After tying, all accesses to the variable are redirected through FETCH/STORE/etc. methods on the tie object. Returns the tie object on success.

Implementation status: Stub. Returns undef (simulating a graceful failure) because full tie support requires deep integration with the value layer to intercept variable access.

Synopsis

tie my %hash, 'DB_File', 'filename';
tie my @array, 'Tie::File', 'data.txt';
tie my $scalar, 'Tie::StdScalar';
my $obj = tie %hash, 'MyTieClass', @args;

See Also

tied, untie

tied

Returns the underlying object of a tied variable.

Returns the object that was associated with the variable by a previous tie call. Returns undef if the variable is not tied. Commonly used to access the tie object’s methods directly.

Implementation status: Stub. Always returns undef because tie is not yet implemented.

Synopsis

my $obj = tied %hash;
if (tied $scalar) { ... }   # check if variable is tied

See Also

tie, untie

time

Returns the number of seconds since the Unix epoch.

Returns the current time as a non-negative integer representing whole seconds elapsed since 1970-01-01 00:00:00 UTC (the Unix epoch).

If Time::HiRes::time has been imported into the current package (overriding the builtin), this op dispatches to the high-resolution version which returns a floating-point value with microsecond precision. The CORE::time form (SPECIAL flag set) always uses the integer builtin, bypassing any override.

Synopsis

my $now = time();             # e.g. 1704067200
my $core = CORE::time;        # always integer, bypasses overrides
# with: use Time::HiRes qw(time);
my $hires = time();           # e.g. 1704067200.123456

See Also

localtime, gmtime, Time::HiRes

tr

Performs character-by-character transliteration on a string.

Transliterates characters in the target string according to a precompiled translation table. Each character found in the search list is replaced by the corresponding character in the replacement list.

The target string is obtained from the targ pad slot, the stack, or $_. For tr/// (OP_TRANS), the string is modified in place and the count of translated characters is pushed. For tr///r (OP_TRANSR), a new modified copy is pushed instead.

Modifiers (encoded in op_private):

  • /c (0x20): Complement the search list
  • /d (0x80): Delete found but unreplaced characters
  • /s (0x08): Squash duplicate replaced characters

Synopsis

$str =~ tr/searchlist/replacementlist/;
$str =~ y/a-z/A-Z/;          # y is a synonym
my $count = ($s =~ tr/a//);   # count occurrences
my $new   = $s =~ tr/a-z/A-Z/r;  # /r returns copy

See Also

s///, m//, perlop

truncate

Truncates a file to a specified length.

Truncates the file identified by the first argument to at most LENGTH bytes. The first argument may be either an open filehandle or a filename string. When given a filename, the file is opened for writing just long enough to perform the truncation.

Returns 1 on success, undef on failure (e.g. permissions, invalid filehandle).

Synopsis

truncate($fh, $length);          # by open filehandle
truncate('/path/to/file', $len);  # by filename

See Also

open, seek, tell

uc

Converts a string to uppercase.

Returns an uppercased copy of STRING. For ASCII input, an in-place byte mutation fast path avoids allocation when a TARG pad slot is available. For Unicode input, Rust’s to_uppercase() is used, which handles case mappings that change byte length (e.g. the German sharp-s \u{00DF} becomes "SS").

Synopsis

$upper = uc($string);
$upper = uc('hello');   # "HELLO"

See Also

lc, ucfirst, lcfirst

ucfirst

Uppercases the first character of a string.

Returns a copy of STRING with the first character converted to uppercase and all other characters left unchanged. For ASCII first characters, a single-byte in-place mutation is used. For Unicode first characters, to_uppercase() may produce multiple characters (e.g. the ffi ligature U+FB03).

Returns an empty string when given an empty string.

Synopsis

$result = ucfirst($string);
$result = ucfirst('hello');   # "Hello"

See Also

lcfirst, uc, lc

umask

Gets or sets the process file-creation permission mask.

With an argument, sets the umask to the given value and returns the previous mask. Without an argument (or with undef), queries the current mask by temporarily setting it to 0 and restoring it, then returns the value.

The umask affects the default permissions of newly created files and directories. The actual permissions are requested & ~umask.

Returns the previous umask as an integer (typically displayed in octal).

Synopsis

my $old_mask = umask(0022);  # Set new mask, return old
my $current  = umask();      # Query current mask

See Also

chmod, mkdir, open

undef

Produces the undefined value, or undefines an existing variable.

Operates in three modes determined by op flags:

  • MOD (lvalue placeholder): Used in list assignment patterns like my (undef, $x) = @list. Adds a sentinel to lvalue_targets so pp_aassign skips that position.
  • STACKED (function call undef $x): Pops the target from the stack and undefines it. For arrays and hashes this clears the container; for hash/array element targets it sets the element to undef; for scalars it writes undef through the alias.
  • Default: If targ is set, sets the corresponding pad slot to undef. Always pushes undef onto the stack as the return value.

Synopsis

my $x = undef;
undef $x;                    # set $x to undef
undef @array;                # clear the array
undef %hash;                 # clear the hash
my (undef, @rest) = @list;   # skip first element in list assignment

See Also

defined

unlink

Deletes one or more files from the filesystem.

Deletes a list of files. If called with no arguments (empty list after pushmark, or no mark at all), defaults to $_. Returns the number of files successfully deleted. Does not delete directories (use rmdir for that). Uses std::fs::remove_file.

Synopsis

unlink $file;
my $count = unlink @files;
unlink or die "Cannot unlink $_: $!";   # uses $_
unlink glob("*.bak");

See Also

rename, link, rmdir

unpack

Extracts values from a binary string according to a template.

The inverse of pack: takes a TEMPLATE and a binary STRING (or $_ if omitted) and returns a list of values extracted according to the template format characters. The template syntax is the same as for pack, including group syntax (...)N, repeat counts, and *.

String data is decoded using Latin-1 encoding (each byte maps 1:1 to a char code point), which correctly round-trips with pack’s output. The U format uses character offsets instead of byte offsets.

Synopsis

my @vals = unpack("A10", $data);
my ($long, $short, $str) = unpack("NnA*", $packet);
my @chars = unpack("C*", $bytes);
my @vals = unpack("A5", $_);   # $_ when string omitted

See Also

pack, vec, ord

unshift

Prepends one or more values to the beginning of an array.

Prepends the listed values to the front of the array, preserving argument order: unshift(@a, 1, 2, 3) yields (1, 2, 3, @original). Array arguments are flattened. Returns the new number of elements. Internally, values are inserted in reverse order to achieve the correct final ordering.

Synopsis

unshift @array, $value;
unshift @array, $v1, $v2, $v3;
unshift @array, @more_values;
my $new_len = unshift @array, $value;

See Also

shift, push, pop, splice

untie

Removes the tie binding from a variable, restoring normal access.

Breaks the association between a variable and its tie class established by tie. Subsequent accesses to the variable bypass the FETCH/STORE methods and operate on the variable directly. Returns true on success.

Implementation status: Stub. Always returns 1 (success) since tie is not yet implemented.

Synopsis

untie %hash;
untie @array;
untie $scalar;

See Also

tie, tied

utime

Sets the access and modification timestamps of one or more files.

Takes an access time, a modification time (both as Unix epoch seconds, or undef for “now”), followed by a list of file paths. Each file’s timestamps are updated via the POSIX utimes(2) call. When Time::HiRes::utime is available, nanosecond precision is used via utimensat(2).

Passing undef for either timestamp sets it to the current time.

Returns the number of files successfully modified.

Synopsis

utime $atime, $mtime, @files;
utime undef, undef, @files;    # set to current time

See Also

stat, lstat

values

Returns all values of a hash, or the number of values in scalar context.

In list context, pushes all values of the hash onto the stack. In scalar context, returns the number of key-value pairs (same as keys in scalar context). Accepts both %hash and $hashref. Resets the hash’s internal iterator as a side effect.

When the REF flag is set (foreach aliasing context from the native parser), values are pushed onto the stack and the hash plus its keys are recorded for write-back of $_ modifications during iteration.

Synopsis

my @v = values %hash;
my $count = values %hash;       # scalar context: number of values
for my $val (values %hash) { ... }

See Also

keys, each, exists, delete

vec

Treats a string as a bit vector and accesses individual bitfields.

Treats EXPR as a bit vector made up of elements of width BITS and returns the unsigned integer value of the element at OFFSET. BITS must be a power of 2 from 1 to 32.

As an lvalue, vec stores values into the specified bitfield, growing the string as needed.

my $flags = "";
vec($flags, 0, 8) = 255;     # Set byte 0 to 255
vec($flags, 1, 8) = 10;      # Set byte 1 to 10
my $val = vec($flags, 0, 8); # Get byte 0 (255)
my $bit = vec($flags, 5, 1); # Get bit 5 of byte 0

Synopsis

my $val = vec($string, $offset, $bits);
vec($string, $offset, $bits) = $value;

See Also

pack, unpack

wait

Waits for any child process to terminate.

Suspends the current process until any child process terminates. Returns the PID of the deceased child, or -1 if there are no child processes. Sets $? to the wait status of the child (encoding exit code and signal information).

Synopsis

my $pid = wait();
print "Child $pid exited with status $?\n";

See Also

waitpid, fork, $?

waitpid

Waits for a specific child process to change state.

Suspends the current process until the child identified by $pid terminates (or changes state, depending on flags). Sets $? to the wait status.

Returns:

  • The PID of the terminated child on success
  • 0 if WNOHANG was specified and no child has exited yet
  • -1 on error (e.g. no such child)

When $pid is -1, waits for any child (equivalent to wait()).

Synopsis

my $result = waitpid($pid, 0);        # blocking wait
my $result = waitpid($pid, WNOHANG);  # non-blocking
my $result = waitpid(-1, 0);           # any child (like wait)

See Also

wait, fork, POSIX

warn

Issues a warning message to STDERR without terminating the program.

Prints the argument to STDERR. If the string does not end with a newline, Perl appends at FILE line LINE.

If $SIG{__WARN__} is set to a code reference, that handler is called with the warning message as its sole argument instead of printing to STDERR. The handler runs in void context and its return value is discarded.

Synopsis

warn "something looks wrong";
warn "problem at $file line $line\n";
warn $object;

See Also

die, Carp

x

Repeats a string a given number of times.

Pops the repeat count (right operand) and the string (left operand) from the stack. Returns a new string consisting of the left operand concatenated with itself COUNT times. A count of zero or less produces an empty string. Inf and NaN counts are treated as zero (matching perl5).

Synopsis

$line = '-' x 80;          # 80 dashes
$pad  = ' ' x $width;
$s    = 'ab' x 3;          # "ababab"

See Also

x, perlop

Native Modules

pperl PP runtime native (Rust) reimplementations of CPAN/core XS modules. These provide identical Perl-level APIs without requiring XS compilation.

  • B — Native implementation of the B module (Perl compiler backend) (134 functions)
  • Config — pp runtime adapter for Config.
  • Cwd — Provides current working directory operations. (8 functions)
  • Data::Dumper — Provides data structure serialization to Perl syntax. (2 functions)
  • Digest::MD5 — Provides MD5 hashing functionality compatible with the Digest::MD5 Perl module. (14 functions)
  • Digest::SHA — Provides SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, and (54 functions)
  • Encode — Provides character encoding/decoding functions compatible with Perl’s (9 functions)
  • Errno — Provides system errno constants for Linux (6 functions)
  • Fcntl — Provides file control constants and file-type test functions matching (55 functions)
  • File::Basename — Provides path parsing functions: basename, dirname, fileparse. (4 functions)
  • File::Copy — Provides file copy and move operations. (4 functions)
  • File::Find — Provides recursive directory traversal: find() and finddepth(). (2 functions)
  • File::Glob — Provides BSD glob pattern matching via libc’s glob(). (3 functions)
  • File::Path — Provides directory tree creation and removal operations. (4 functions)
  • File::Spec — Provides portable file path operations using Unix semantics. (18 functions)
  • File::Temp — Provides tempfile() and tempdir() with automatic cleanup on process exit. (15 functions)
  • File::stat — Provides by-name interface to Perl’s stat() builtin. (3 functions)
  • FileHandle — FileHandle is a compatibility module that wraps IO::File and IO::Handle.
  • Hash::Util — Provides hash introspection and manipulation utilities. (38 functions)
  • I18N::Langinfo — Provides locale information querying via nl_langinfo(). (56 functions)
  • IO::Dir — Provides object-oriented directory operations compatible with Perl’s IO::Dir. (9 functions)
  • IO::File — Provides object-oriented file operations compatible with Perl’s IO::File. (13 functions)
  • IO::Handle — Provides object-oriented filehandle methods compatible with Perl’s IO::Handle. (47 functions)
  • IO::Pipe — Provides object-oriented pipe operations compatible with Perl’s IO::Pipe. (4 functions)
  • IO::Seekable — Provides seek/tell operations on filehandles compatible with Perl’s IO::Seekable. (4 functions)
  • IO::Select — Provides multiplexed I/O via the POSIX select() system call, compatible (11 functions)
  • IO::Socket — Provides object-oriented socket operations compatible with Perl’s IO::Socket. (21 functions)
  • Internals — Provides internal Perl functions that manipulate SV flags. (3 functions)
  • List::Util — Provides list utility functions (first, sum, min, max, reduce, etc.) (35 functions)
  • MIME::Base64 — Provides base64 encoding and decoding functions compatible with Perl’s (6 functions)
  • MIME::QuotedPrint — Provides quoted-printable encoding and decoding functions compatible with (2 functions)
  • Math::GMP — Math::GMP — High-speed arbitrary precision integers (1 functions)
  • POSIX — Native implementation of Perl’s POSIX module (214 functions)
  • PadWalker — PadWalker provides introspective access to lexical variables in Perl subroutine
  • Peta::FFI — Peta::FFI — Dynamic FFI for calling C library functions from Perl (8 functions)
  • Peta::FFI::GMP — Peta::FFI::GMP — Pre-baked libgmp bindings (Layer 1) (50 functions)
  • Peta::FFI::Libc — Peta::FFI::Libc — Pre-baked libc bindings (Layer 1) (29 functions)
  • Peta::FFI::UUID — Peta::FFI::UUID — Pre-baked libuuid bindings (Layer 1) (6 functions)
  • Scalar::Util — Provides scalar utility functions (blessed, reftype, etc.) (14 functions)
  • Socket — Native implementation of Perl’s Socket module (15 functions)
  • Storable — Provides serialization/deserialization functions for Perl data structures. (19 functions)
  • Sub::Util — Provides subroutine utility functions for introspection and manipulation. (4 functions)
  • Sys::Hostname — Provides hostname retrieval functionality. (1 functions)
  • Time::HiRes — pp runtime adapter for Time::HiRes.
  • mro — MRO (Method Resolution Order) implementation (9 functions)
  • sdl2 — Native SDL2 module — dlopen-based SDL2 bindings with direct SDL2-style API. (33 functions)
  • version — Native implementation of the version module (17 functions)

B

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Native implementation of the B module (Perl compiler backend)

Provides introspection into Perl’s internal representation of code. Supports svref_2object for all SV types (IV, NV, PV, CV, GV, AV, HV, NULL), class(), ppname(), opnumber(), special SVs, utility functions, and basic op tree introspection via B::CV::ROOT/START.

Functions

AV

CUR

CV

CvFLAGS

DEPTH

EGV

FILE

FLAGS

GV

GvREFCNT

HV

IV

IVX

LEN

LINE

NAME

NAME_HEK

NV

NVX

OUTSIDE

PADLIST

PV

PVX

REFCNT

ROOT

SAFENAME

START

STASH

SV

SvTYPE

UVX

amagic_generation

Returns the current overload magic generation counter (always 0 in pperl).

use B qw(amagic_generation);
my $gen = amagic_generation();

See also: sv_undef

can

cast_I32

cast_i32

Casts a value to a signed 32-bit integer, truncating as needed.

use B qw(cast_I32);
my $i = cast_I32(0xFFFFFFFF);  # -1

See also: hash

children

class

Returns the B:: class name of a B object (e.g. “CV”, “GV”, “SV”).

use B qw(class);
my $type = class($b_obj);

See also: svref_2object

cop_file

Returns the filename recorded in this COP (control op).

use B qw(main_root);
# walk to a COP node, then:
my $file = $cop->file;

See also: B::COP::line, B::COP::stash

cop_label

Returns the label associated with this COP (e.g. loop labels like “LOOP:”).

use B qw(main_root);
my $label = $cop->label;

See also: B::COP::file, B::COP::line

cop_line

Returns the line number recorded in this COP.

use B qw(main_root);
my $line = $cop->line;

See also: B::COP::file, B::COP::stash

cop_stash

Returns the stash (package) active at this COP as a B::HV object.

use B qw(main_root);
my $stash = $cop->stash;

See also: B::COP::stashpv, B::COP::file

cop_stashpv

Returns the stash name as a plain string (e.g. “main”).

use B qw(main_root);
my $pkg = $cop->stashpv;

See also: B::COP::stash, B::COP::file

cstring

Returns the C-escaped double-quoted representation of a string.

use B qw(cstring);
my $c = cstring("hello\nworld");  # "hello\\nworld"

See also: perlstring

cv_cvflags

Returns the CV flags bitmask (always 0 in pperl).

use B qw(svref_2object);
my $flags = svref_2object(\&foo)->CvFLAGS;

See also: B::CV::DEPTH, B::SV::FLAGS

cv_depth

Returns the recursion depth of this CV (always 0 in pperl).

use B qw(svref_2object);
my $depth = svref_2object(\&foo)->DEPTH;

See also: B::CV::CvFLAGS

cv_file

Returns the filename where this CV was defined.

use B qw(svref_2object);
my $file = svref_2object(\&foo)->FILE;

See also: B::CV::GV, B::CV::START

cv_gv

Returns the GV (glob value) associated with this CV as a B::GV object.

use B qw(svref_2object);
my $gv = svref_2object(\&foo)->GV;

See also: B::GV::NAME, B::CV::FILE

cv_name_hek

Returns the name of the CV from its HEK (hash entry key), or undef for anonymous/global subs.

use B qw(svref_2object);
my $name = svref_2object(\&foo)->NAME_HEK;

See also: B::CV::GV, B::GV::NAME

cv_outside

Returns the lexically enclosing CV as a B::CV object.

use B qw(svref_2object);
my $outer = svref_2object(\&foo)->OUTSIDE;

See also: B::CV::ROOT, B::CV::PADLIST

cv_padlist

Returns the padlist for this CV (currently undef in pperl).

use B qw(svref_2object);
my $padlist = svref_2object(\&foo)->PADLIST;

See also: B::CV::OUTSIDE, B::CV::DEPTH

cv_root

Returns the root op of this CV’s op tree as a B::OP object.

use B qw(svref_2object);
my $root = svref_2object(\&foo)->ROOT;

See also: B::CV::START, B::CV::GV

cv_start

Returns the first op in execution order for this CV as a B::OP object.

use B qw(svref_2object);
my $start = svref_2object(\&foo)->START;

See also: B::CV::ROOT, B::CV::GV

cv_stash

Returns the stash (package hash) associated with this CV as a B::HV object.

use B qw(svref_2object);
my $stash = svref_2object(\&foo)->STASH;

See also: B::CV::GV, B::HV::NAME

desc

file

first

flags

generic_isa

Checks whether a B object is a member of the given class via the @ISA chain.

use B qw(svref_2object);
if (svref_2object(\&foo)->isa("B::CV")) { ... }

See also: class, svref_2object

gv_av

Returns the array slot of this GV as a B::AV object.

use B qw(svref_2object);
my $av = svref_2object(\*foo)->AV;

See also: B::GV::SV, B::GV::HV, B::GV::CV

gv_cv

Returns the code slot of this GV as a B::CV object.

use B qw(svref_2object);
my $cv = svref_2object(\*foo)->CV;

See also: B::GV::SV, B::GV::AV, B::GV::HV

gv_egv

Returns the effective GV (usually the same as the GV itself).

use B qw(svref_2object);
my $egv = svref_2object(\*foo)->EGV;

See also: B::GV::NAME, B::GV::STASH

gv_file

Returns the filename where this GV was first defined.

use B qw(svref_2object);
my $file = svref_2object(\*foo)->FILE;

See also: B::GV::LINE, B::GV::NAME

gv_hv

Returns the hash slot of this GV as a B::HV object.

use B qw(svref_2object);
my $hv = svref_2object(\*foo)->HV;

See also: B::GV::SV, B::GV::AV, B::GV::CV

gv_is_empty

Returns whether this GV is empty (has no assigned slots).

use B qw(svref_2object);
my $empty = svref_2object(\*foo)->is_empty;

See also: B::GV::isGV_with_GP, B::GV::NAME

gv_isgv_with_gp

Returns true if this GV has a GP (glob pointer) structure (always 1 in pperl).

use B qw(svref_2object);
my $has_gp = svref_2object(\*foo)->isGV_with_GP;

See also: B::GV::NAME, B::GV::is_empty

gv_line

Returns the line number where this GV was first defined (always 0 in pperl).

use B qw(svref_2object);
my $line = svref_2object(\*foo)->LINE;

See also: B::GV::FILE, B::GV::NAME

gv_name

Returns the name of this GV (glob value), e.g. “foo” for *main::foo.

use B qw(svref_2object);
my $name = svref_2object(\*foo)->NAME;

See also: B::GV::SAFENAME, B::GV::STASH

gv_refcnt

Returns the GV-specific reference count (always 1 in pperl).

use B qw(svref_2object);
my $rc = svref_2object(\*foo)->GvREFCNT;

See also: B::SV::REFCNT, B::GV::NAME

gv_safename

Returns the name of this GV with control characters converted to ^X notation.

use B qw(svref_2object);
my $safe = svref_2object(\*foo)->SAFENAME;

See also: B::GV::NAME, safename

gv_stash

Returns the stash (package) this GV belongs to as a B::HV object.

use B qw(svref_2object);
my $stash = svref_2object(\*foo)->STASH;

See also: B::GV::NAME, B::HV::NAME

gv_sv

Returns the scalar slot of this GV as a B::SV object.

use B qw(svref_2object);
my $sv = svref_2object(\*foo)->SV;

See also: B::GV::AV, B::GV::HV, B::GV::CV

hash

Returns the hash value of a string as a hex string (e.g. “0x1a2b3c”).

use B qw(hash);
my $h = hash("foo");

See also: cstring, perlstring

hv_name

Returns the name of this HV (stash name), e.g. “main” for %main::.

use B qw(svref_2object);
my $name = svref_2object(\%Foo::)->NAME;

See also: B::GV::STASH, B::CV::STASH

import

Handles use B qw(...) by exporting requested functions into the caller’s namespace.

use B qw(svref_2object class ppname);

See also: svref_2object, class, ppname

int_value

isGV_with_GP

is_empty

isa

iv_iv

Returns the integer value stored in this B::IV object.

use B qw(svref_2object);
my $val = svref_2object(\$n)->IV;

See also: B::IV::UVX, B::NV::NV

iv_uvx

Returns the unsigned integer interpretation of this B::IV object’s value.

use B qw(svref_2object);
my $uval = svref_2object(\$n)->UVX;

See also: B::IV::IV, B::NV::NV

label

last

line

main_cv

Returns the main program’s CV (code value) as a B::CV object.

use B qw(main_cv);
my $cv = main_cv();

See also: main_root, main_start

main_root

Returns the root op of the main program as a B::OP object.

use B qw(main_root);
my $root = main_root();

See also: main_start, main_cv

main_start

Returns the starting op of the main program as a B::OP object.

use B qw(main_start);
my $start = main_start();

See also: main_root, main_cv

moresib

name

next

nv_nv

Returns the floating-point value stored in this B::NV object.

use B qw(svref_2object);
my $val = svref_2object(\$f)->NV;

See also: B::IV::IV, B::PV::PV

object_2svref

op_can

Returns true if the B::OP object supports the named method.

use B qw(main_root);
if (main_root()->can("first")) { ... }

See also: B::OP::isa, B::OP::name

op_children

Returns the number of child ops by walking the first->sibling chain.

use B qw(main_root);
my $count = main_root()->children;

See also: B::OP::first, B::OP::last, B::OP::sibling

op_desc

Returns a human-readable description of this op.

use B qw(main_root);
my $desc = main_root()->desc;

See also: B::OP::name, B::OP::type

op_first

Returns the first child op of a UNOP/BINOP/LISTOP as a B::OP object.

use B qw(main_root);
my $first = main_root()->first;

See also: B::OP::last, B::OP::sibling

op_flags

Returns the op_flags bitmask for this op (OPf_WANT, OPf_KIDS, etc.).

use B qw(main_root);
my $flags = main_root()->flags;

See also: B::OP::private, B::OP::targ

op_last

Returns the last child op of a BINOP/LISTOP as a B::OP object.

use B qw(main_root);
my $last = main_root()->last;

See also: B::OP::first, B::OP::children

op_moresib

Returns true (1) if this op has more siblings, false (0) otherwise.

use B qw(main_root);
my $has_sib = main_root()->first->moresib;

See also: B::OP::sibling, B::OP::children

op_name

Returns the name of this op (e.g. “add”, “const”, “null”).

use B qw(main_root);
my $name = main_root()->name;

See also: B::OP::type, B::OP::desc

op_next

Returns the next op in execution order as a B::OP object.

use B qw(main_start);
my $next = main_start()->next;

See also: B::OP::sibling, B::OP::first

op_opt

Returns whether this op has been optimized (always 0 in pperl).

use B qw(main_root);
my $optimized = main_root()->opt;

See also: B::OP::flags, B::OP::name

op_parent

Returns the parent op in the op tree (always B::NULL in pperl, parent tracking not implemented).

use B qw(main_root);
my $parent = main_root()->first->parent;

See also: B::OP::first, B::OP::sibling

op_ppaddr

Returns the pp function address as a string like “PL_ppaddr[OP_ADD]”.

use B qw(main_root);
my $addr = main_root()->ppaddr;

See also: B::OP::name, ppname

op_private

Returns the op_private flags byte for this op.

use B qw(main_root);
my $priv = main_root()->private;

See also: B::OP::flags, B::OP::targ

op_sibling

Returns the next sibling op in the op tree as a B::OP object.

use B qw(main_root);
my $sib = main_root()->first->sibling;

See also: B::OP::next, B::OP::moresib

op_targ

Returns the op_targ pad offset for this op.

use B qw(main_root);
my $targ = main_root()->targ;

See also: B::OP::flags, B::OP::private

op_type

Returns the numeric op type for this op.

use B qw(main_root);
my $type = main_root()->type;

See also: B::OP::name, B::OP::desc

opnumber

Returns the op number for a given op name (e.g. “add” returns 42).

use B qw(opnumber);
my $num = opnumber("add");

See also: ppname

opt

parent

parents

Returns the parent ops collected during the last walkoptree traversal (currently always empty).

use B qw(parents);
my @parents = parents();

See also: walkoptree, walkoptree_debug

perlstring

Returns the Perl-escaped double-quoted representation of a string, escaping $, @, and using \x{} notation.

use B qw(perlstring);
my $p = perlstring('$foo');  # "\\$foo"

See also: cstring

ppaddr

ppname

Returns the PP function name for a given op number (e.g. “pp_add”).

use B qw(ppname);
my $name = ppname(42);

See also: opnumber

private

pv_cur

Returns the current length (SvCUR) of the string in this B::PV object.

use B qw(svref_2object);
my $len = svref_2object(\$s)->CUR;

See also: B::PV::PV, B::PV::LEN

pv_len

Returns the allocated buffer length (SvLEN) of this B::PV object.

use B qw(svref_2object);
my $alloc = svref_2object(\$s)->LEN;

See also: B::PV::PV, B::PV::CUR

pv_pv

Returns the string value stored in this B::PV object.

use B qw(svref_2object);
my $str = svref_2object(\$s)->PV;

See also: B::PV::CUR, B::PV::LEN

safename

Converts control characters in a name to ^X notation for safe display.

use B qw(safename);
my $safe = safename("\x01foo");  # "^Afoo"

See also: cstring, perlstring

sibling

stash

stashpv

sv_flags

Returns the SV flags bitmask (IOK, NOK, POK, etc.).

use B qw(svref_2object);
my $flags = svref_2object(\$x)->FLAGS;

See also: B::SV::REFCNT, B::SV::SvTYPE

sv_no

Returns a B::SPECIAL object representing PL_sv_no.

use B qw(sv_no);
my $no = sv_no();

See also: sv_undef, sv_yes

sv_object_2svref

Converts a B object back to a reference to the original Perl value.

use B qw(svref_2object);
my $ref = svref_2object(\$x)->object_2svref;

See also: svref_2object

sv_refcnt

Returns the reference count of this SV.

use B qw(svref_2object);
my $rc = svref_2object(\$x)->REFCNT;

See also: B::SV::FLAGS, B::SV::SvTYPE

sv_svtype

Returns the SV type number (SVt_NULL=0, SVt_IV=1, SVt_NV=2, SVt_PV=4, etc.).

use B qw(svref_2object);
my $type = svref_2object(\$x)->SvTYPE;

See also: B::SV::FLAGS, B::SV::REFCNT

sv_undef

Returns a B::SPECIAL object representing PL_sv_undef.

use B qw(sv_undef);
my $undef = sv_undef();

See also: sv_yes, sv_no

sv_yes

Returns a B::SPECIAL object representing PL_sv_yes.

use B qw(sv_yes);
my $yes = sv_yes();

See also: sv_undef, sv_no

svref_2object

Converts a Perl reference to its corresponding B:: object representation.

use B qw(svref_2object);
my $b_obj = svref_2object(\$scalar);

See also: class, sv_undef, sv_yes, sv_no

targ

type

walkoptree

Walks the op tree starting from $op, calling $op->$method() on each node in tree order.

use B qw(walkoptree main_root);
walkoptree(main_root(), "print_name");

See also: walkoptree_debug, parents, main_root

walkoptree_debug

Gets or sets the debug flag for walkoptree output.

use B qw(walkoptree_debug);
walkoptree_debug(1);
my $dbg = walkoptree_debug();

See also: walkoptree

Config

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

pp runtime adapter for Config.

Thin wrapper: delegates config data and formatting to core.rs, handles only pp-runtime value type conversions (Sv, Hv, Av, Cv).

Cwd

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides current working directory operations.

Synopsis

use Cwd;
my $dir = cwd();
use Cwd 'abs_path';
my $abs = abs_path('relative/path');
my $abs = abs_path('/symlinked/../path');

Perl Equivalent

Cwd

Functions

abs_path

Returns the canonicalized absolute pathname of the argument. Resolves symlinks. Returns undef if the path cannot be resolved. If no argument is given, resolves the current directory.

use Cwd 'abs_path';
my $abs = abs_path('relative/path');
my $abs = abs_path();   # current directory

chdir

Changes the working directory and updates $ENV{PWD}. Unlike CORE::chdir, does NOT default to $ENV{HOME} when called without arguments. Returns 1 on success, 0 on failure.

use Cwd 'chdir';
chdir('/tmp') or die "Cannot chdir: $!";

cwd

Returns the current working directory as a string. All four names are synonyms in PetaPerl (no distinction between XS and pure-Perl implementations).

use Cwd;
my $dir = cwd();

fast_abs_path

Returns the canonicalized absolute pathname of the argument. Resolves symlinks. Returns undef if the path cannot be resolved. If no argument is given, resolves the current directory.

use Cwd 'abs_path';
my $abs = abs_path('relative/path');
my $abs = abs_path();   # current directory

fastcwd

Returns the current working directory as a string. All four names are synonyms in PetaPerl (no distinction between XS and pure-Perl implementations).

use Cwd;
my $dir = cwd();

fastgetcwd

Returns the current working directory as a string. All four names are synonyms in PetaPerl (no distinction between XS and pure-Perl implementations).

use Cwd;
my $dir = cwd();

getcwd

Returns the current working directory as a string. All four names are synonyms in PetaPerl (no distinction between XS and pure-Perl implementations).

use Cwd;
my $dir = cwd();

realpath

Returns the canonicalized absolute pathname of the argument. Resolves symlinks. Returns undef if the path cannot be resolved. If no argument is given, resolves the current directory.

use Cwd 'abs_path';
my $abs = abs_path('relative/path');
my $abs = abs_path();   # current directory

Data::Dumper

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides data structure serialization to Perl syntax.

Perl Equivalent

Data::Dumper

Synopsis

use Data::Dumper;
print Dumper(\%hash, \@array);
my $d = Data::Dumper->new([$ref], ['name']);
$d->Indent(1);
$d->Sortkeys(1);
print $d->Dump();

Functions

Dumper

Dumps one or more references to Perl syntax. Returns a string containing the serialized representation.

use Data::Dumper;
print Dumper($hashref, $arrayref);

### DumperX

Digest::MD5

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides MD5 hashing functionality compatible with the Digest::MD5 Perl module.

Synopsis

use Digest::MD5 qw(md5_hex md5_base64);
my $hex    = md5_hex("some data");
my $base64 = md5_base64("some data");
# OO interface for incremental hashing
my $ctx = Digest::MD5->new;
$ctx->add("first chunk");
$ctx->add("second chunk");
my $digest = $ctx->hexdigest;

Functional Interface

  • md5($data) - Returns binary MD5 digest (16 bytes)
  • md5_hex($data) - Returns hexadecimal MD5 digest (32 characters)
  • md5_base64($data) - Returns base64-encoded MD5 digest (22 characters, no padding)

Object-Oriented Interface

  • Digest::MD5->new() - Creates a new MD5 context object
  • $ctx->add($data, ...) - Adds data to the context
  • $ctx->addfile($fh) - Adds data from a filehandle (not yet implemented)
  • $ctx->digest() - Returns binary digest and resets the context
  • $ctx->hexdigest() - Returns hex digest and resets the context
  • $ctx->b64digest() - Returns base64 digest (no padding) and resets the context
  • $ctx->reset() - Resets the context
  • $ctx->clone() - Clones the context

Implementation Notes

The OO interface stores accumulated data in a blessed hash reference. The hash uses a special key _data to store the accumulated bytes. Computing the actual MD5 hash is done when digest(), hexdigest(), or b64digest() is called.

This approach avoids the complexity of storing Rust state (Md5 hasher) in Perl values while maintaining compatibility with the Perl interface.

Functions

Digest

add

Appends data to the MD5 context for incremental digesting. Returns the context for method chaining.

See also: addfile, digest, hexdigest

add_bits

Adds bits to the MD5 context. The argument is a string of ‘0’ and ‘1’ characters whose length must be a multiple of 8.

See also: add, addfile

addfile

Reads all data from the given filehandle and adds it to the MD5 context. Returns the context for method chaining.

See also: add, digest

b64digest

Returns the base64-encoded MD5 digest (22 characters, no padding) of the accumulated data and resets the context.

See also: digest, hexdigest

clone

Creates and returns a copy of the MD5 context, preserving accumulated data and blessed class.

See also: new, reset

context

Gets or sets the internal MD5 computation state. In get mode returns ($block_count, $state_buffer[, $unprocessed]). In set mode restores state from arguments.

See also: digest, reset, clone

digest

Returns the binary MD5 digest (16 bytes) of the accumulated data and resets the context.

See also: hexdigest, b64digest

hexdigest

Returns the hexadecimal MD5 digest (32 characters) of the accumulated data and resets the context.

See also: digest, b64digest

md5

Computes the MD5 digest of the concatenated arguments and returns the binary digest (16 bytes).

See also: md5_hex, md5_base64

md5_base64

Computes the MD5 digest of the concatenated arguments and returns the base64-encoded digest (22 characters, no padding).

See also: md5, md5_hex

md5_hex

Computes the MD5 digest of the concatenated arguments and returns the hexadecimal digest (32 characters).

See also: md5, md5_base64

new

Creates and returns a new Digest::MD5 context object, blessed into the given class.

See also: reset, clone

reset

Resets the MD5 context, discarding all accumulated data. Returns the context for method chaining.

See also: new, clone

Digest::SHA

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, and SHA-512/256 digest functions via both functional and object-oriented interfaces.

Synopsis

use Digest::SHA qw(sha256_hex sha512_hex);
my $hex256 = sha256_hex("some data");
my $hex512 = sha512_hex("some data");
# OO interface for incremental hashing
my $ctx = Digest::SHA->new(256);
$ctx->add("first chunk");
$ctx->add("second chunk");
my $digest = $ctx->hexdigest;
# HMAC
use Digest::SHA 'hmac_sha256_hex';
my $mac = hmac_sha256_hex($data, $key);

Functions

Functional Interface – HMAC

Functional Interface – One-shot Hashing

OO Interface

add

Append data to the digest context. Can be called multiple times for incremental hashing.

$ctx->add("hello");
$ctx->add(" world");

add_bits

Add partial-byte data to the digest context (bit-level granularity).

addfile

Read a file and add its contents to the digest context.

algorithm

Return the algorithm number of this context.

b64digest

Finalize and return the base64 digest string (no padding). Resets the context.

clone

Clone the current digest context (for computing intermediate digests).

digest

Finalize and return the binary digest. Resets the context.

dump

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

getstate

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

hashsize

Return the digest size in bits for this context’s algorithm.

hexdigest

Finalize and return the hexadecimal digest string. Resets the context.

hmac_sha1

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha1_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha1_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha224

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha224_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha224_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha256

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha256_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha256_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha384

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha384_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha384_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

hmac_sha512

Compute an HMAC-SHA keyed hash and return the binary digest.

use Digest::SHA 'hmac_sha256';
my $mac = hmac_sha256($data, $key);

hmac_sha512_base64

Compute an HMAC-SHA keyed hash and return the base64 digest (no padding).

hmac_sha512_hex

Compute an HMAC-SHA keyed hash and return the hex digest.

load

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

new

Create a new Digest::SHA context. Accepts an algorithm number (1, 224, 256, 384, 512).

use Digest::SHA;
my $ctx = Digest::SHA->new(256);

putstate

Serialize and restore the internal state of a digest context.

Perl equivalent: Digest::SHA

reset

Reset the context for reuse with the same algorithm.

sha1

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha1_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha1_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha224

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha224_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha224_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha256

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha256_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha256_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha384

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha384_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha384_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha512

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha512224

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha512224_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha512224_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha512256

Compute a SHA digest of the given data and return the result as a binary string.

use Digest::SHA 'sha256';
my $digest = sha256("hello");

sha512256_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha512256_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

sha512_base64

Compute a SHA digest and return the result as a base64 string (without padding, matching Perl’s Digest::SHA convention).

use Digest::SHA 'sha256_base64';
my $b64 = sha256_base64("hello");

sha512_hex

Compute a SHA digest and return the result as a lowercase hexadecimal string.

use Digest::SHA 'sha256_hex';
my $hex = sha256_hex("hello");

Encode

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides character encoding/decoding functions compatible with Perl’s Encode module. Since PetaPerl does not support XS, this module is implemented directly in Rust.

Synopsis

use Encode qw(encode decode find_encoding);
my $bytes  = encode('UTF-8', $string);
my $string = decode('UTF-8', $bytes);
# Encoding object interface
my $enc = find_encoding('iso-8859-1');
my $bytes = $enc->encode($string);
# Shorthand for UTF-8
use Encode qw(encode_utf8 decode_utf8);
my $bytes  = encode_utf8($string);
my $string = decode_utf8($bytes);

Perl Equivalent

Encode

Functions

decode

Decodes a byte string from the specified encoding into a Unicode string.

use Encode 'decode';
my $string = decode('UTF-8', $bytes);

decode_utf8

Shorthand for decode("UTF-8", $bytes).

use Encode 'decode_utf8';
my $string = decode_utf8($bytes);

encode

Encodes a Unicode string into the specified encoding, returning a byte string.

use Encode 'encode';
my $bytes = encode('UTF-8', $string);
my $bytes = encode('iso-8859-1', $string);

encode_utf8

Shorthand for encode("UTF-8", $string).

use Encode 'encode_utf8';
my $bytes = encode_utf8($string);

encodings

Returns a list of available encoding names, optionally filtered by type.

find_encoding

Returns an encoding object for the given name, or undef if the encoding is not recognized. The object supports ->encode() and ->decode() methods.

use Encode 'find_encoding';
my $enc = find_encoding('UTF-8');
my $bytes = $enc->encode($string);

from_to

Converts a string in-place from one encoding to another. Returns the length of the converted string, or undef on failure.

use Encode 'from_to';
from_to($data, 'iso-8859-1', 'UTF-8');

is_utf8

Returns true if the string has the internal UTF-8 flag set. This is a Perl internals function, not a validity check.

resolve_alias

Resolves an encoding alias to its canonical name. Returns undef if the encoding is not recognized.

use Encode 'resolve_alias';
my $name = resolve_alias('latin1');   # 'iso-8859-1'

Errno

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides system errno constants for Linux. In real Perl, Errno.pm is auto-generated from system headers. We provide the same constants via native Rust implementation using the libc crate.

The module exports constant subs like ENOENT(), EACCES(), etc. that return the errno integer value. It also provides TIEHASH, FETCH, STORE, EXISTS, FIRSTKEY, NEXTKEY methods for the %! tied hash.

Functions

EPERM, ENOENT, ESRCH, … (errno constants)

Each errno name is a constant sub returning its integer value. Over 100 Linux errno constants are provided (see ERRNO_CONSTANTS).

use Errno qw(ENOENT EACCES);
if ($! == ENOENT) { ... }

EXISTS

Checks whether a given name is a known errno constant.

if (exists $!{ENOENT}) { ... }

FETCH

Looks up an errno constant by name through the tied %! hash.

my $val = $!{ENOENT};  # returns 2

FIRSTKEY

Returns the first errno constant name. Used by each %! and keys %!.

my $first = (keys %!)[0];  # "EPERM"

NEXTKEY

Returns the next errno constant name after a given key. Drives hash iteration over %!.

while (my ($k, $v) = each %!) { ... }

TIEHASH

Ties the %! hash to the Errno module. Called implicitly by the runtime.

tie %!, 'Errno';

Fcntl

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides file control constants and file-type test functions matching the POSIX <fcntl.h> and <sys/stat.h> headers. All values are sourced from the libc crate so they match the build platform.

Constants are registered as constant CVs (inlined at compile time). Functions that take arguments (S_ISREG, S_IFMT, etc.) go through stub dispatch.

Functions

FD_CLOEXEC

F_DUPFD

F_GETFD

F_GETFL

F_SETFD

F_SETFL

LOCK_EX

LOCK_NB

LOCK_SH

LOCK_UN

O_APPEND

O_CREAT

O_EXCL

O_NOCTTY

O_NOFOLLOW

O_NONBLOCK

O_RDONLY

O_RDWR

O_TRUNC

O_WRONLY

SEEK_CUR

SEEK_END

SEEK_SET

S_IFBLK

S_IFCHR

S_IFDIR

S_IFIFO

S_IFLNK

S_IFMT

S_IFREG

S_IFSOCK

S_IMODE

S_IRGRP

S_IROTH

S_IRUSR

S_IRWXG

S_IRWXO

S_IRWXU

S_ISBLK

S_ISCHR

S_ISDIR

S_ISFIFO

S_ISGID

S_ISLNK

S_ISREG

S_ISSOCK

S_ISUID

S_ISVTX

S_IWGRP

S_IWOTH

S_IWUSR

S_IXGRP

S_IXOTH

S_IXUSR

fcntl_autoload

Resolves unknown Fcntl constant names at runtime via the AUTOLOAD mechanism.

use Fcntl;
my $val = Fcntl::SOME_CONSTANT();

See also: get_constant_value, dispatch

File::Basename

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides path parsing functions: basename, dirname, fileparse.

Synopsis

use File::Basename qw(basename dirname fileparse);
my $base = basename("/usr/local/bin/perl");   # "perl"
my $dir  = dirname("/usr/local/bin/perl");    # "/usr/local/bin"
my ($name, $path, $suffix) = fileparse(
    "/usr/local/lib/libperl.so", qr/\.[^.]*/
);
# $name="libperl", $path="/usr/local/lib/", $suffix=".so"

Functions

basename

Returns the last component of a path, optionally removing a suffix. Trailing slashes are removed before extracting the last component.

See also: dirname, fileparse

dirname

Returns the directory portion of a path. Returns "." if the path contains no directory component.

See also: basename, fileparse

fileparse

Splits a path into (name, directory, suffix). This is the underlying function for basename and dirname. Suffixes can be strings or regex patterns.

See also: basename, dirname

fileparse_set_fstype

Set the filesystem type for path parsing. Always returns “Unix”; the argument is accepted but ignored.

Synopsis

use File::Basename 'fileparse_set_fstype';
fileparse_set_fstype('Unix');

File::Copy

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides file copy and move operations.

Perl’s File::Copy supports passing open filehandles as either the source or destination argument. When a filehandle is given as destination, bytes are written to the already-open handle rather than treating the stringified handle id as a filename. Similarly, a filehandle as source is read from the open handle rather than opening a file whose name is the numeric id.

Synopsis

use File::Copy;
copy("source.txt", "dest.txt")  or die "Copy failed: $!";
move("old.txt", "new.txt")      or die "Move failed: $!";
use File::Copy qw(cp mv);
cp("src", "dst")  or die "cp failed: $!";
mv("old", "new")  or die "mv failed: $!";

Functions

copy

Copy a file from source to destination. Returns 1 on success, 0 on failure. Accepts filenames or open filehandles for either argument.

use File::Copy;
copy("src.txt", "dst.txt") or die "Copy failed: $!";
copy($fh_in, "dst.txt");

cp

Alias for copy (available via use File::Copy 'cp').

move

Move a file from source to destination (rename, or copy+unlink if cross-device). Returns 1 on success, 0 on failure.

use File::Copy;
move("old.txt", "new.txt") or die "Move failed: $!";

mv

Alias for move (available via use File::Copy 'mv').

File::Find

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides recursive directory traversal: find() and finddepth().

The find() function takes a hash ref of options (or a code ref) and a list of directories to search. It recursively visits each file/directory and calls the wanted() callback.

Synopsis

use File::Find;
find(sub { print "$File::Find::name\n" }, '/home');
find({
    wanted   => sub { print "$_\n" if -f },
    no_chdir => 1,
}, @directories);
finddepth(sub { rmdir $_ if -d }, '/tmp/tree');

Functions

find

Traverse a directory tree in top-down order, calling the wanted() callback for each file and directory. Sets $File::Find::name, $File::Find::dir, and $_ for each visited entry.

use File::Find;
find(sub { print "$File::Find::name\n" }, '/tmp');
find({ wanted => \&process, no_chdir => 1 }, @dirs);

finddepth

Traverse a directory tree in bottom-up (depth-first) order. Equivalent to calling find() with the bydepth => 1 option: directories are processed after their contents.

use File::Find;
finddepth(sub { print "$File::Find::name\n" }, '/tmp');

File::Glob

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides BSD glob pattern matching via libc’s glob().

Functions

GLOB_ERROR

Return the error code from the most recent bsd_glob or csh_glob call. Returns 0 if the last call succeeded.

use File::Glob ':bsd_glob';
my @f = bsd_glob("/no/such/*");
warn "glob error" if GLOB_ERROR;

Perl equivalent: File::Glob (core module, XS-based)

bsd_glob

Expand a glob pattern into matching filenames using BSD-style semantics. Accepts an optional flags argument to control matching behavior.

use File::Glob ':bsd_glob';
my @files = bsd_glob("*.pl");
my @files = bsd_glob("~/src/*.rs", GLOB_TILDE | GLOB_BRACE);

csh_glob

Expand a glob pattern using csh-style defaults (GLOB_BRACE | GLOB_NOMAGIC | GLOB_QUOTE | GLOB_TILDE | GLOB_ALPHASORT). This is the function called internally by Perl’s <*.pl> glob operator.

use File::Glob ':glob';
my @files = csh_glob("*.{pl,pm}");

File::Path

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides directory tree creation and removal operations.

Synopsis

use File::Path qw(make_path remove_tree);
make_path("foo/bar/baz", "quux/blort");
make_path("web/cgi", { mode => 0755 });
remove_tree("foo/bar", { keep_root => 1 });
# Legacy interface
use File::Path;
mkpath(["/tmp/a/b/c"], 0, 0755);
rmtree(["/tmp/scratch"]);

Functions

make_path

Create one or more directory trees, including intermediate directories (modern API). Accepts a list of paths and an optional trailing hashref of options (mode, error, verbose).

use File::Path 'make_path';
make_path("/tmp/a/b/c", { mode => 0755 });

mkpath

Legacy interface for make_path. Accepts mkpath(\@dirs, $verbose, $mode).

use File::Path;
mkpath(["/tmp/a/b/c"], 0, 0755);

remove_tree

Remove one or more directory trees recursively (modern API). Accepts a list of paths and an optional trailing hashref of options (keep_root, error).

use File::Path 'remove_tree';
remove_tree("/tmp/a", { keep_root => 1 });

rmtree

Legacy interface for remove_tree. Accepts rmtree(\@dirs, $verbose, $safe).

use File::Path;
rmtree(["/tmp/a"]);

Perl equivalent: File::Path (make_path, remove_tree, mkpath, rmtree)

File::Spec

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides portable file path operations using Unix semantics.

Synopsis

use File::Spec;
my $path = File::Spec->catfile("usr", "local", "bin", "perl");
my $dir  = File::Spec->catdir("usr", "local", "lib");
my ($vol, $dirs, $file) = File::Spec->splitpath($path);
my @dirs = File::Spec->splitdir($dirs);
my $abs = File::Spec->rel2abs("lib/Foo.pm");
my $rel = File::Spec->abs2rel("/usr/local/bin", "/usr");

Functions

abs2rel

Return a relative path from the base to the given path.

use File::Spec;
my $rel = File::Spec->abs2rel("/usr/local/bin", "/usr");  # "local/bin"

canonpath

Clean up a path by removing redundant separators and up-level references.

use File::Spec;
my $clean = File::Spec->canonpath("/foo//bar/../baz");  # "/foo/baz"

case_tolerant

Return false (0) on Unix, where the filesystem is case-sensitive.

catdir

Concatenate directory names to form a complete path.

use File::Spec;
my $dir = File::Spec->catdir("/usr", "local", "bin");  # "/usr/local/bin"

catfile

Concatenate directory names and a filename to form a complete path.

use File::Spec;
my $file = File::Spec->catfile("/usr", "local", "bin", "perl");

catpath

Combine volume, directory, and filename into a complete path (inverse of splitpath).

curdir

Return a string representation of the current directory (“.”).

devnull

Return the path to the null device (“/dev/null”).

file_name_is_absolute

Return true if the given path is absolute (starts with “/”).

join

Alias for catfile. Concatenate path components into a complete path.

Perl equivalent: File::Spec, File::Spec::Unix, File::Spec::Functions

no_upwards

Filter out “.” and “..” entries from a list of directory names.

path

Return the list of directories in the PATH environment variable.

rel2abs

Convert a relative path to an absolute path, optionally relative to a given base.

use File::Spec;
my $abs = File::Spec->rel2abs("lib/Foo.pm");

rootdir

Return a string representation of the root directory (“/”).

splitdir

Split a directory path into its component parts.

use File::Spec;
my @dirs = File::Spec->splitdir("/usr/local/bin");  # ("", "usr", "local", "bin")

splitpath

Split a path into volume, directory, and filename components.

use File::Spec;
my ($vol, $dir, $file) = File::Spec->splitpath("/usr/local/bin/perl");

tmpdir

Return the path to the system temporary directory (from $TMPDIR, $TEMP, $TMP, or “/tmp”).

updir

Return a string representation of the parent directory (“..”).

File::Temp

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides tempfile() and tempdir() with automatic cleanup on process exit.

All created temp files and directories are tracked in a global registry. On process exit (via cleanup_temp_files()), entries marked for cleanup are removed. This prevents stale temp files accumulating in /tmp.

Synopsis

use File::Temp qw(tempfile tempdir);
my ($fh, $filename) = tempfile(SUFFIX => '.dat');
print $fh "some data\n";
my $dir = tempdir(CLEANUP => 1);
# OO interface
my $tmp = File::Temp->new(DIR => '/tmp', SUFFIX => '.log');
print $tmp "log line\n";
my $name = $tmp->filename;

Functions

cleanup

Explicitly trigger cleanup of all registered temporary files and directories. Normally called automatically at process exit.

Synopsis

use File::Temp 'cleanup';
cleanup();

dir_noop

No-op fallback for File::Temp::Dir overload dispatch.

See also: native_dir_stringify

dir_stringify

Stringify overload for File::Temp::Dir objects, returns the directory path.

See also: tempdir, newdir

filename

Instance method. Returns the filename of a File::Temp object by extracting the __filename key from the blessed hashref.

Synopsis

my $fh = File::Temp->new();
my $name = $fh->filename;

mkdtemp

Create a temporary directory from a template. Trailing X characters are replaced with pseudo-random digits. Registered for automatic cleanup.

Synopsis

use File::Temp 'mkdtemp';
my $dir = mkdtemp("myapp_XXXXXXXX");

mkstemp

Create a temporary file from a template and return ($fh, $filename). Trailing X characters are replaced with pseudo-random digits. The file is created exclusively and registered for cleanup.

Synopsis

use File::Temp 'mkstemp';
my ($fh, $name) = mkstemp("myapp_XXXXXXXX");

mkstemps

Like mkstemp but with a suffix. The second argument specifies the suffix length: template characters after that point are preserved as the file extension.

Synopsis

use File::Temp 'mkstemps';
my ($fh, $name) = mkstemps("myapp_XXXXXXXX.dat", 4);

mktemp

Generate a temporary filename from a template. Trailing X characters in the template are replaced with pseudo-random digits.

Synopsis

use File::Temp 'mktemp';
my $name = mktemp("myapp_XXXXXXXX");

new

OO constructor. Creates a temporary file and returns a blessed File::Temp hashref that doubles as a filehandle. Accepts options: DIR, SUFFIX, TEMPLATE/PREFIX, UNLINK.

Synopsis

use File::Temp;
my $fh = File::Temp->new(SUFFIX => '.dat', DIR => '/tmp');
print $fh "data\n";

newdir

OO constructor for temporary directories. Creates a temp directory and returns a blessed File::Temp::Dir hashref with stringify overload. Accepts options: DIR, CLEANUP, TEMPLATE/PREFIX.

Synopsis

use File::Temp;
my $dir = File::Temp->newdir(CLEANUP => 1);
print "$dir\n";  # stringifies to path

tempdir

Create a temporary directory and return its path. Registered for automatic cleanup unless CLEANUP => 0. Accepts options: DIR, CLEANUP, PREFIX/TEMPLATE.

Synopsis

use File::Temp 'tempdir';
my $dir = tempdir(CLEANUP => 1);

tempfile

Create a temporary file and return ($fh, $filename) in list context. The file is created exclusively (race-free via O_EXCL). Accepts options: DIR, SUFFIX, PREFIX/TEMPLATE, UNLINK.

Synopsis

use File::Temp 'tempfile';
my ($fh, $name) = tempfile(DIR => '/tmp', SUFFIX => '.txt');

tmpfile

tmpnam

Generate a temporary filename (without creating it). Returns a path in the system temp directory with a pseudo-random name.

Synopsis

use File::Temp 'tmpnam';
my $name = tmpnam();

unlink0

Unlink a temporary file by name while it may still be open, and remove it from the cleanup registry (since it is already deleted).

Synopsis

use File::Temp 'unlink0';
unlink0($fh, $filename);

File::stat

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides by-name interface to Perl’s stat() builtin. Returns blessed objects with accessor methods for stat fields.

Functions

Accessor methods on the blessed File::stat object. Each returns the corresponding field from stat(2), indexed into the underlying array.

use File::stat;
my $st = stat($filename);
my $uid   = $st->uid;
my $mtime = $st->mtime;

lstat

Like stat, but does not follow symlinks (uses symlink_metadata).

use File::stat;
my $st = lstat('/path/to/symlink');
print $st->mode, "\n";

stat

Returns a blessed File::stat object (arrayref) for a path or filehandle. Follows symlinks.

use File::stat;
my $st = stat('/etc/passwd');
print $st->size, "\n";

FileHandle

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

FileHandle is a compatibility module that wraps IO::File and IO::Handle. In perl5, it’s a thin shim that re-exports IO::File functionality. We provide a native Rust implementation because the perl5 FileHandle.pm does use IO::File which does use IO which tries XSLoader::load 'IO', and that fails under pperl since we have no XS support.

This module sets up the FileHandle package with proper @ISA inheritance from IO::File and IO::Seekable, registers method stubs, and provides dispatch for the key methods that FileHandle exposes.

Perl Equivalent

package FileHandle;
require 5.000;
use strict;
use IO::File;
our @ISA = qw(IO::File);
our $VERSION = "2.05";

Methods

Most methods are inherited from IO::File / IO::Handle. The ones registered here are stubs to ensure method resolution works and can() succeeds.

  • new() - Constructor, returns a blessed FileHandle reference
  • new_from_fd($fd, $mode) - Constructor from file descriptor
  • open($file [, $mode]) - Open a file
  • fdopen($fd, $mode) - Open from file descriptor
  • close() - Close handle
  • print(@args) - Output data
  • printf($fmt, @args) - Formatted output
  • say(@args) - Output with newline
  • getline() - Read one line
  • getlines() - Read all lines
  • autoflush([$val]) - Get/set autoflush
  • flush() - Flush output
  • eof() - Test end of file
  • fileno() - Get file descriptor
  • read($buf, $len [, $offset]) - Read bytes
  • sysread($buf, $len [, $offset]) - Low-level read
  • syswrite($buf [, $len [, $offset]]) - Low-level write
  • seek($pos, $whence) - Seek
  • tell() - Tell position
  • binmode([$layer]) - Set binary mode
  • truncate($len) - Truncate file
  • stat() - File stat
  • opened() - Check if open
  • blocking([$flag]) - Get/set blocking mode

Exports

  • autoflush (default export)
  • SEEK_SET, SEEK_CUR, SEEK_END constants (via @EXPORT_OK)

Hash::Util

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides hash introspection and manipulation utilities. Implements the XS primitives and commonly-used pure-Perl wrappers for hash locking, key restriction, and bucket introspection.

Synopsis

use Hash::Util qw(lock_keys lock_hash hash_value);
my %h = (foo => 1, bar => 2);
lock_keys(%h);             # restrict to existing keys
lock_hash(%h);             # fully immutable
my $hval = hash_value("key");

Functions

_clear_placeholders

Removes placeholder (restricted-deleted) keys from a hash. No-op in this implementation.

use Hash::Util '_clear_placeholders';
_clear_placeholders(%hash);

all_keys

Populates two arrayrefs with visible and hidden (placeholder) keys, then returns a reference to the hash.

use Hash::Util 'all_keys';
all_keys(%hash, @visible, @hidden);

bucket_array

Returns an arrayref with one slot per bucket (undef for empty buckets). Simplified approximation of perl5’s internal layout.

use Hash::Util 'bucket_array';
my $aref = bucket_array(%hash);

bucket_info

Returns a 3-element list: (keys, total_buckets, used_buckets).

use Hash::Util 'bucket_info';
my ($keys, $total, $used) = bucket_info(%hash);

bucket_ratio

Returns a string “used/total” representing the bucket usage ratio of the hash. Total is the next power of 2 (minimum 8).

use Hash::Util 'bucket_ratio';
my $ratio = bucket_ratio(%hash);  # e.g. "3/8"

bucket_stats

Returns a 4-element list: (keys, total_buckets, used_buckets, quality_score). Quality is always 1.0 (ideal).

use Hash::Util 'bucket_stats';
my ($k, $t, $u, $q) = bucket_stats(%hash);

bucket_stats_formatted

Returns a human-readable string summarising bucket statistics.

use Hash::Util 'bucket_stats_formatted';
print bucket_stats_formatted(%hash);
# "keys: 3 buckets: 3/8 quality: 1.00"

hash_locked

Returns 1 if the hash has its keys locked (restricted), 0 otherwise.

use Hash::Util 'hash_locked';
if (hash_locked(%hash)) { ... }

hash_seed

Returns the hash seed used by the internal hash algorithm. Currently returns a fixed “0” string.

use Hash::Util 'hash_seed';
my $seed = hash_seed();

hash_traversal_mask

Returns the hash traversal randomisation mask. Always returns 0 (no randomisation).

use Hash::Util 'hash_traversal_mask';
my $mask = hash_traversal_mask();

hash_unlocked

Returns 1 if the hash does NOT have its keys locked, 0 otherwise. Inverse of hash_locked.

use Hash::Util 'hash_unlocked';
if (hash_unlocked(%hash)) { ... }

hash_value

Computes and returns the integer hash value for a given string using Rust’s DefaultHasher.

use Hash::Util 'hash_value';
my $h = hash_value("key");

hashref_locked

Returns 1 if the hash has its keys locked (restricted), 0 otherwise.

use Hash::Util 'hash_locked';
if (hash_locked(%hash)) { ... }

hashref_unlocked

Returns 1 if the hash does NOT have its keys locked, 0 otherwise. Inverse of hash_locked.

use Hash::Util 'hash_unlocked';
if (hash_unlocked(%hash)) { ... }

hidden_keys

Returns the list of hidden (placeholder) keys in a hash or hashref.

use Hash::Util 'hidden_keys';
my @hk = hidden_keys(%hash);

hidden_ref_keys

Returns the list of hidden (placeholder) keys in a hash or hashref.

use Hash::Util 'hidden_keys';
my @hk = hidden_keys(%hash);

hv_store

Stores a key/value pair directly in the hash, bypassing any key-restriction checks.

use Hash::Util 'hv_store';
hv_store(%hash, "key", "value");

Returns all legal keys (visible + hidden/restricted) for the hash.

use Hash::Util 'legal_keys';
my @lk = legal_keys(%hash);

Returns all legal keys (visible + hidden/restricted) for the hash.

use Hash::Util 'legal_keys';
my @lk = legal_keys(%hash);

lock_hash

Locks all keys AND all values of the hash. No new keys can be added, no existing values can be changed. Returns the hashref.

use Hash::Util 'lock_hash';
lock_hash(%hash);

lock_hash_recurse

Recursively locks the hash and all nested hash values. Returns the hashref.

use Hash::Util 'lock_hash_recurse';
lock_hash_recurse(%hash);

lock_hashref

Locks all keys AND all values of the hash. No new keys can be added, no existing values can be changed. Returns the hashref.

use Hash::Util 'lock_hash';
lock_hash(%hash);

lock_hashref_recurse

Recursively locks the hash and all nested hash values. Returns the hashref.

use Hash::Util 'lock_hash_recurse';
lock_hash_recurse(%hash);

lock_keys

Restricts a hash to its current keyset, or to an explicit list of allowed keys. Adding a key not in the set will die. Returns the hashref.

use Hash::Util 'lock_keys';
lock_keys(%hash);
lock_keys(%hash, @allowed);

lock_ref_keys

Like lock_keys but takes a hashref instead of a hash. Returns the hashref.

use Hash::Util 'lock_ref_keys';
lock_ref_keys($href);
lock_ref_keys($href, @allowed);

lock_ref_keys_plus

Locks the hashref’s keys to the union of its current keys and the supplied additional keys. Returns the hashref.

use Hash::Util 'lock_ref_keys_plus';
lock_ref_keys_plus($href, @extra_keys);

lock_ref_value

Makes a single hash value read-only. The key must already exist.

use Hash::Util 'lock_value';
lock_value(%hash, 'key');

lock_value

Makes a single hash value read-only. The key must already exist.

use Hash::Util 'lock_value';
lock_value(%hash, 'key');

num_buckets

Returns the total number of buckets allocated for the hash. Minimum is 8, then next power of 2 above the key count.

use Hash::Util 'num_buckets';
my $n = num_buckets(%hash);

unlock_hash

Unlocks all keys and values of a previously locked hash. Returns the hashref.

use Hash::Util 'unlock_hash';
unlock_hash(%hash);

unlock_hash_recurse

Recursively unlocks the hash and all nested hash values. Returns the hashref.

use Hash::Util 'unlock_hash_recurse';
unlock_hash_recurse(%hash);

unlock_hashref

Unlocks all keys and values of a previously locked hash. Returns the hashref.

use Hash::Util 'unlock_hash';
unlock_hash(%hash);

unlock_hashref_recurse

Recursively unlocks the hash and all nested hash values. Returns the hashref.

use Hash::Util 'unlock_hash_recurse';
unlock_hash_recurse(%hash);

unlock_keys

Removes the key restriction on a hash, allowing arbitrary keys again. Returns the hashref.

use Hash::Util 'unlock_keys';
unlock_keys(%hash);

unlock_ref_keys

Like unlock_keys but takes a hashref. Returns the hashref.

use Hash::Util 'unlock_ref_keys';
unlock_ref_keys($href);

unlock_ref_value

Makes a previously locked hash value writable again.

use Hash::Util 'unlock_value';
unlock_value(%hash, 'key');

unlock_value

Makes a previously locked hash value writable again.

use Hash::Util 'unlock_value';
unlock_value(%hash, 'key');

used_buckets

Returns the number of occupied buckets (approximated as the number of keys).

use Hash::Util 'used_buckets';
my $n = used_buckets(%hash);

I18N::Langinfo

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides locale information querying via nl_langinfo().

Functions

ABDAY_1

ABDAY_2

ABDAY_3

ABDAY_4

ABDAY_5

ABDAY_6

ABDAY_7

ABMON_1

ABMON_10

ABMON_11

ABMON_12

ABMON_2

ABMON_3

ABMON_4

ABMON_5

ABMON_6

ABMON_7

ABMON_8

ABMON_9

ALT_DIGITS

AM_STR

CODESET

CRNCYSTR

DAY_1

DAY_2

DAY_3

DAY_4

DAY_5

DAY_6

DAY_7

D_FMT

D_T_FMT

ERA

ERA_D_FMT

ERA_D_T_FMT

ERA_T_FMT

MON_1

MON_10

MON_11

MON_12

MON_2

MON_3

MON_4

MON_5

MON_6

MON_7

MON_8

MON_9

NOEXPR

PM_STR

RADIXCHAR

THOUSEP

T_FMT

T_FMT_AMPM

YESEXPR

langinfo

IO::Dir

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides object-oriented directory operations compatible with Perl’s IO::Dir. IO::Dir objects are blessed hashrefs. The __dh_id key stores the directory handle ID (u64) that maps to an entry in DIRHANDLE_TABLE.

Functions

DESTROY

Destructor. Automatically closes the directory handle when the object goes out of scope.

Exportable constant (value 1). Used by tie-based directory operations.

close

Closes the directory handle and removes it from DIRHANDLE_TABLE.

$d->close();

new

Constructor. With no arguments, returns an unopened IO::Dir object. With a directory name, opens the directory and returns a blessed handle.

use IO::Dir;
my $d = IO::Dir->new("/tmp");
my $d = IO::Dir->new();

open

Opens (or re-opens) a directory on an existing IO::Dir object. Closes any previously opened directory first. Returns 1 on success, undef on failure.

my $d = IO::Dir->new;
$d->open("/tmp") or die $!;

read

Reads the next directory entry. Returns the entry name as a string, or undef at end-of-directory.

while (defined(my $entry = $d->read)) {
```perl
print "$entry\n";

}


### rewind

Resets the directory read position to the beginning.

```perl
$d->rewind();

seek

Sets the directory read position to a given offset (as returned by tell).

$d->seek($saved_pos);

tell

Returns the current directory read position, or -1 on error.

my $pos = $d->tell();

IO::File

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides object-oriented file operations compatible with Perl’s IO::File. IO::File inherits from IO::Handle and IO::Seekable.

All file operations delegate to FILEHANDLE_TABLE via the __fh_id key stored on the blessed hashref object.

Synopsis

use IO::File;
my $fh = IO::File->new("file.txt", "r") or die "Cannot open: $!";
while (my $line = $fh->getline) {
    print $line;
}
$fh->close;
my $out = IO::File->new("> output.txt") or die $!;
$out->print("Hello, World!\n");
$out->close;

Functions

O_APPEND

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_CREAT

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_EXCL

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_NOCTTY

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_NONBLOCK

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_RDONLY

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_RDWR

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_SYNC

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_TRUNC

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

O_WRONLY

POSIX file-open flag constants, re-exported from Fcntl for convenience.

use IO::File;
my $fh = IO::File->new("f", O_RDWR | O_CREAT);

new

Constructor. With no arguments, returns an unopened IO::File object. With a filename, opens the file and returns a blessed handle. Supports both string mode ("<", ">", ">>", "+<") and numeric O_* flags.

use IO::File;
my $fh = IO::File->new();
my $fh = IO::File->new("file.txt");
my $fh = IO::File->new("file.txt", "r");
my $fh = IO::File->new("file.txt", O_RDWR | O_CREAT, 0644);
my $fh = IO::File->new("> output.txt");

Single-argument form parses mode prefixes from the filename (e.g., "< file", "> file", ">> file").

new_tmpfile

Creates a temporary file opened for read/write. The file is immediately unlinked, so it vanishes when the handle is closed.

my $fh = IO::File->new_tmpfile();
$fh->print("temp data");

open

Opens (or re-opens) a file on an existing IO::File object. Supports string modes and numeric O_* flags, like new.

my $fh = IO::File->new();
$fh->open("file.txt", "<") or die $!;

IO::Handle

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides object-oriented filehandle methods compatible with Perl’s IO::Handle. All I/O methods delegate to FILEHANDLE_TABLE, the real I/O layer.

IO objects are blessed hashrefs. The __fh_id key stores the filehandle ID (u64) that maps to an entry in FILEHANDLE_TABLE.

Functions

SEEK_CUR

Seek position constants (0, 1, 2). Exported both as constant subs and package scalars.

SEEK_END

Seek position constants (0, 1, 2). Exported both as constant subs and package scalars.

SEEK_SET

Seek position constants (0, 1, 2). Exported both as constant subs and package scalars.

autoflush

Gets or sets the autoflush flag. Returns the previous value.

$fh->autoflush(1);
my $prev = $fh->autoflush();

binmode

Sets the encoding layer on the filehandle. Supports :utf8, :raw, and :bytes. Other layers are no-ops.

$fh->binmode(":utf8");
$fh->binmode(":raw");

blocking

Gets or sets blocking mode. Stub that always returns 1.

$fh->blocking(0);

clearerr

Error tracking stubs. error() always returns 0 (no error). clearerr() always returns 0.

close

Closes the filehandle and removes it from FILEHANDLE_TABLE. Returns 1 on success, undef on failure.

$fh->close();

eof

Returns true if the filehandle is at end-of-file.

while (!$fh->eof) { ... }

error

Error tracking stubs. error() always returns 0 (no error). clearerr() always returns 0.

fcntl

Stubs that return 0. Not yet implemented.

fdopen

Associates an existing IO::Handle object with a file descriptor. Similar to new_from_fd but operates on an already-created object.

my $fh = IO::Handle->new;
$fh->fdopen(fileno(STDOUT), "w");

fileno

Returns the underlying OS file descriptor number, or undef if not open.

my $fd = $fh->fileno();

flush

Flushes the output buffer. Always returns 1.

$fh->flush();

format_formfeed

Format-related accessor stubs. Return empty string.

format_line_break_characters

Format-related accessor stubs. Return empty string.

format_lines_left

Format-related accessor stubs. Return 0.

format_lines_per_page

Format-related accessor stubs. Return 0.

format_name

Format-related accessor stubs. Return empty string.

format_page_number

Format-related accessor stubs. Return 0.

format_top_name

Format-related accessor stubs. Return empty string.

format_write

Format write stub. Returns 1.

getc

Reads a single character. Checks the ungetc buffer first, then reads from the filehandle.

my $ch = $fh->getc();

getline

Reads one record from the filehandle, respecting $/. Croaks if called with arguments.

my $line = $fh->getline();

getlines

Reads all remaining records from the filehandle, respecting $/. Must be called in list context; croaks in scalar context.

my @lines = $fh->getlines();

input_line_number

Gets or sets the per-handle line number (the OO equivalent of $.).

my $lineno = $fh->input_line_number();
$fh->input_line_number(0);

input_record_separator

Get or set the global $/, $,, and $\ special variables respectively.

$fh->input_record_separator("\n");
$fh->output_field_separator(", ");
$fh->output_record_separator("\n");

ioctl

Stubs that return 0. Not yet implemented.

new

Constructor. Returns a new, unopened IO::Handle blessed hashref.

use IO::Handle;
my $fh = IO::Handle->new();

new_from_fd

Constructs an IO::Handle from an existing file descriptor or glob ref. The fd is duped before wrapping.

use IO::Handle;
my $fh = IO::Handle->new_from_fd(fileno(STDOUT), "w");
my $fh = IO::Handle->new_from_fd(\*STDIN, "r");

opened

Returns true if the filehandle is currently open.

if ($fh->opened) { ... }

output_field_separator

Get or set the global $/, $,, and $\ special variables respectively.

$fh->input_record_separator("\n");
$fh->output_field_separator(", ");
$fh->output_record_separator("\n");

output_record_separator

Get or set the global $/, $,, and $\ special variables respectively.

$fh->input_record_separator("\n");
$fh->output_field_separator(", ");
$fh->output_record_separator("\n");

Outputs the given arguments to the filehandle. Respects :utf8 layer.

$fh->print("hello ", "world");

printf

Formatted output via sprintf semantics.

$fh->printf("%s = %d\n", $name, $value);

read

Buffered read of $len bytes into $buf, with optional $offset.

my $n = $fh->read($buf, 1024);
my $n = $fh->read($buf, 1024, $offset);

say

Like print, but appends a newline after all arguments.

$fh->say("hello world");

setbuf

Buffer control stubs. Return 0.

setvbuf

Buffer control stubs. Return 0.

stat

Returns the 13-element stat list for the filehandle via fstat(2).

my @st = $fh->stat();

sync

Flushes the handle to disk. Delegates to flush().

sysread

Unbuffered read of $len bytes into $buf, with optional $offset. Same implementation as read in PetaPerl.

my $n = $fh->sysread($buf, 1024);

syswrite

Unbuffered write of $buf (or a substring thereof) to the filehandle.

my $n = $fh->syswrite($buf);
my $n = $fh->syswrite($buf, $len, $offset);

truncate

Truncates the file to $len bytes.

$fh->truncate(0);

ungetc

Pushes a character (by ordinal value) back onto the handle’s internal unget buffer.

$fh->ungetc(ord('A'));

untaint

Marks the handle as untainted. No-op that returns 1.

write

Writes $len bytes from $buf starting at $offset to the filehandle. Perl5 signature: write($buf, $len, $offset).

$fh->write($buf, length($buf), 0);

IO::Pipe

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides object-oriented pipe operations compatible with Perl’s IO::Pipe. IO::Pipe objects are blessed hashrefs storing reader/writer fh_ids.

For fork-based communication, reader()/writer() without args just configures the object to the appropriate end of the pipe.

Functions

handles

Returns two unopened IO::Pipe::End objects (reader, writer) that can be configured independently.

my ($reader, $writer) = $pipe->handles();

new

Creates a pipe pair via pipe(2). Returns a blessed IO::Pipe object holding both reader and writer filehandle IDs internally.

use IO::Pipe;
my $pipe = IO::Pipe->new();

reader

Configures the pipe as the reader end. Without arguments, closes the write end and re-blesses as IO::Pipe::End. With a command, forks a child that execvp’s the command with its stdout connected to the pipe; the parent reads from the pipe.

$pipe->reader("ls", "-l");    # fork+exec, parent reads
$pipe->reader();              # just configure reader end

writer

Configures the pipe as the writer end. Without arguments, closes the read end and re-blesses as IO::Pipe::End. With a command, forks a child that execvp’s the command with its stdin connected to the pipe; the parent writes to the pipe.

$pipe->writer("sort");        # fork+exec, parent writes
$pipe->writer();              # just configure writer end

IO::Seekable

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides seek/tell operations on filehandles compatible with Perl’s IO::Seekable. IO::Seekable is a mixin class inherited by IO::File and others. All methods delegate to FILEHANDLE_TABLE via the shared extract_fh_id() helper.

Functions

getpos

Returns the current byte position, equivalent to tell(). Intended for use with setpos() to save and restore positions.

my $pos = $fh->getpos();

### seek

Sets the filehandle position. `$whence` is one of SEEK_SET (0),
SEEK_CUR (1), or SEEK_END (2). Returns 1 on success, 0 on failure.

```perl
use IO::Seekable;
$fh->seek(0, SEEK_SET);

setpos

Sets the filehandle position to an absolute byte offset (equivalent to seek($pos, SEEK_SET)). Returns 1 on success, 0 on failure.

$fh->setpos($saved_pos);

tell

Returns the current byte position in the filehandle, or -1 on error.

my $pos = $fh->tell();

IO::Select

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides multiplexed I/O via the POSIX select() system call, compatible with Perl’s IO::Select module.

IO::Select objects are blessed hashrefs internally storing:

  • __fds: an Av mapping fd_number -> stored handle (the original Sv)
  • __count: integer count of registered handles

Functions

add

Adds one or more handles to the select set. Returns the number of handles added.

$sel->add($fh);
$sel->add(\*STDIN, $sock);

bits

Returns the vec-style bitmask string suitable for the 4-argument select() builtin. Returns undef if the set is empty.

my $bits = $sel->bits();

can_read

Returns a list of handles that are ready for reading within the given timeout (seconds, fractional allowed).

my @ready = $sel->can_read(0.5);

can_write

Returns a list of handles that are ready for writing within the given timeout.

my @ready = $sel->can_write(1.0);

count

Returns the number of handles currently registered.

my $n = $sel->count();

exists

Checks if a given handle is in the select set. Returns the stored handle if present, undef otherwise.

if (defined $sel->exists($fh)) { ... }

handles

Returns a list of all registered handles.

my @fhs = $sel->handles();

has_exception

Returns a list of handles that have pending exceptions within the given timeout.

my @exc = $sel->has_exception(0);

new

Constructor. Creates an IO::Select object, optionally pre-populated with an initial set of handles.

use IO::Select;
my $sel = IO::Select->new();
my $sel = IO::Select->new($fh1, $fh2);

remove

Removes one or more handles from the select set. Returns the number of handles removed.

$sel->remove($fh);

select

Static/class method. Performs a POSIX select(2) across up to three IO::Select objects (readers, writers, errors) with a timeout. Returns three arrayrefs of ready handles, or an empty list on timeout.

my ($r, $w, $e) = IO::Select->select($readers, $writers, $errors, $timeout);

IO::Socket

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides object-oriented socket operations compatible with Perl’s IO::Socket. This replaces the XS-based IO::Socket that ships with perl5.

IO::Socket inherits from IO::Handle.

Most methods are currently stubs that return plausible defaults, sufficient for modules that probe socket capabilities without performing real I/O.

Synopsis

use IO::Socket::INET;
# TCP client
my $sock = IO::Socket::INET->new(
    PeerAddr => 'localhost',
    PeerPort => 8080,
    Proto    => 'tcp',
) or die "Cannot connect: $!";
print $sock "GET / HTTP/1.0\r\n\r\n";
# TCP server
my $server = IO::Socket::INET->new(
    LocalPort => 8080,
    Listen    => 5,
    Proto     => 'tcp',
    ReuseAddr => 1,
) or die "Cannot listen: $!";

Functions

accept

Accepts an incoming connection. Stub: returns undef.

my $client = $sock->accept();

atmark

Checks if the socket is at the out-of-band mark. Stub: returns 0.

if ($sock->atmark) { ... }

bind

Binds the socket to an address. Stub: returns 1 (success).

$sock->bind($packed_addr);

connect

Connects to a remote address. Stub: returns 0 (failure).

$sock->connect($packed_addr);

connected

Returns the peer address if connected, undef otherwise. Stub: returns undef.

if ($sock->connected) { ... }

getsockopt

Get or set socket options. sockopt/getsockopt return undef, setsockopt returns 1.

$sock->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
my $val = $sock->getsockopt(SOL_SOCKET, SO_REUSEADDR);

listen

Starts listening for connections with the given backlog. Stub: returns 1.

$sock->listen(5);

new

Constructor. Creates a new IO::Socket blessed hashref, storing any key/value constructor arguments on the hash. In perl5 this delegates to subclasses (IO::Socket::INET, IO::Socket::UNIX).

use IO::Socket;
my $sock = IO::Socket->new(Domain => AF_INET, Type => SOCK_STREAM);

peername

Returns the packed sockaddr of the remote end. Stub: returns undef.

my $addr = $sock->peername();

protocol

Returns the protocol number. Stub: returns 0.

my $proto = $sock->protocol();

recv

Receives data from the socket. Stub: returns undef.

$sock->recv($buf, 1024, $flags);

send

Sends data on the socket. Stub: returns the length of the data argument.

$sock->send($data, $flags);

setsockopt

Get or set socket options. sockopt/getsockopt return undef, setsockopt returns 1.

$sock->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
my $val = $sock->getsockopt(SOL_SOCKET, SO_REUSEADDR);

shutdown

Shuts down part or all of the socket connection. Stub: returns 1.

$sock->shutdown(2);

sockdomain

Returns the socket domain. Stub: returns AF_INET (2).

my $domain = $sock->sockdomain();

socket

Creates the underlying socket. Stub: returns 1.

$sock->socket(AF_INET, SOCK_STREAM, 0);

socketpair

Creates a pair of connected sockets. Stub: returns 1.

$sock->socketpair(AF_UNIX, SOCK_STREAM, 0);

sockname

Returns the packed sockaddr of the local end. Stub: returns undef.

my $addr = $sock->sockname();

sockopt

Get or set socket options. sockopt/getsockopt return undef, setsockopt returns 1.

$sock->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
my $val = $sock->getsockopt(SOL_SOCKET, SO_REUSEADDR);

socktype

Returns the socket type. Stub: returns SOCK_STREAM (1).

my $type = $sock->socktype();

timeout

Gets or sets the timeout value. When setting, returns the new value. When getting, returns undef.

$sock->timeout(30);
my $t = $sock->timeout();

Internals

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides internal Perl functions that manipulate SV flags.

Functions

SvREADONLY

Query or set the readonly flag on an SV. With one argument, returns the current readonly status. With two arguments, sets or clears the flag.

my $ro = Internals::SvREADONLY($sv);      # query
Internals::SvREADONLY($sv, 1);             # make readonly
Internals::SvREADONLY($sv, 0);             # make writable

SvREFCNT

Returns the reference count of an SV. Always returns 1 in PetaPerl (Rust uses Arc refcounting, not Perl’s SV refcount).

my $cnt = Internals::SvREFCNT($sv);

hv_clear_placeholders

No-op. In perl5 this removes placeholder entries from restricted hashes. PetaPerl does not implement restricted hashes, so this is a silent no-op.

Internals::hv_clear_placeholders(\%hash);

List::Util

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides list utility functions (first, sum, min, max, reduce, etc.)

Perl Equivalent

This module provides Rust-native implementations of functions from List::Util. Since PetaPerl does not support XS, these are implemented directly in Rust.

Block-Based Functions

Functions like first, any, all, none, notall, and reduce in Perl take a block argument. These use the &@ prototype which tells the parser to treat func { block } @list syntax correctly.

Synopsis

use List::Util qw(sum min max first any all reduce uniq);
my $total = sum @numbers;
my $smallest = min @numbers;
my $largest  = max @numbers;
my $found = first { $_ > 10 } @numbers;
my $has_neg = any { $_ < 0 } @numbers;
my $all_pos = all { $_ > 0 } @numbers;
my $product = reduce { $a * $b } @numbers;
my @unique  = uniq @strings;

Functions

all

Returns true if all elements in LIST satisfy the given block. Sets $_ to each element in turn. Short-circuits on the first false result.

See also: any, none, notall

any

Returns true if any element in LIST satisfies the given block. Sets $_ to each element in turn. Short-circuits on the first true result.

See also: all, none, notall, first

first(&@)

Returns the first element in LIST for which the block returns true. Sets $_ to each element in turn. Returns undef if no match is found.

See also: any, none, reduce

Returns the first SIZE elements from LIST. If SIZE is negative, returns all but the last -SIZE elements.

See also: tail

max

Returns the numerically largest element of LIST. Returns undef for an empty list.

See also: min, minstr, maxstr

maxstr

Returns the string-wise maximum of LIST. Uses gt comparison. Returns undef for an empty list.

See also: minstr, max, min

mesh

Returns a flat list of interleaved elements from the given array references. Also available as mesh_longest. Shorter arrays are padded with undef.

See also: mesh_shortest, zip, zip_shortest

mesh_longest

mesh_shortest

Like mesh, but stops at the shortest input array instead of padding shorter arrays with undef.

See also: mesh, zip_shortest

min

Returns the numerically smallest element of LIST. Returns undef for an empty list.

See also: max, minstr, maxstr

minstr

Returns the string-wise minimum of LIST. Uses lt comparison. Returns undef for an empty list.

See also: maxstr, min, max

none

Returns true if no element in LIST satisfies the given block. The logical negation of any. Short-circuits on the first true result.

See also: any, all, notall

notall

Returns true if not all elements in LIST satisfy the given block. The logical negation of all. Short-circuits on the first false result.

See also: all, any, none

pairfirst

Like pairgrep, but returns only the first matching pair (as a 2-element list) and stops searching. Returns an empty list if no pair matches.

See also: pairgrep, pairmap, pairs

pairgrep

Calls BLOCK for each consecutive pair of elements in LIST, with $a set to the key and $b set to the value. Returns the key/value pairs for which the block returns true. In scalar context, returns the count of matching pairs.

See also: pairmap, pairfirst, pairs

pairkeys

Returns a list of the first values (keys) of each pair in LIST. LIST is treated as an even-sized list of key/value pairs.

See also: pairvalues, pairs, unpairs

pairmap

Calls BLOCK for each consecutive pair of elements in LIST, with $a set to the key and $b set to the value. Returns a flat list of all values returned by each block invocation.

See also: pairgrep, pairfirst, pairs

pairs

Returns a list of array references, each containing two items from the given list. It is a convenient shortcut to operating on even-sized lists of key/value pairs.

If the list has an odd number of elements, the final pair will have undef as the value. Each returned array reference is a plain ARRAY ref (not blessed).

See also: unpairs, pairkeys, pairvalues, pairmap, pairgrep, pairfirst

pairvalues

Returns a list of the second values (values) of each pair in LIST. LIST is treated as an even-sized list of key/value pairs.

See also: pairkeys, pairs, unpairs

product

Returns the numeric product of all elements in LIST. Returns undef for an empty list, 1 for a single element.

reduce

Reduces LIST by calling BLOCK in a scalar context, with $a and $b set to successive pairs of values. Returns the final value of $a, or undef for an empty list. For a one-element list, returns that element without calling the block.

See also: reductions, sum, product

reductions

Like reduce, but returns a list of all intermediate accumulator values as well as the final result. The first element is always the first value of the list.

See also: reduce

sample

Returns COUNT random elements from LIST without replacement. Each element can be selected at most once.

If COUNT exceeds the list size, returns the entire list shuffled. Croaks if COUNT is negative.

See also: shuffle

shuffle

Returns the elements of LIST in a random order.

Uses the Fisher-Yates shuffle algorithm for uniform distribution.

See also: sample

sum

Returns the numeric sum of all elements in LIST. Returns undef for an empty list.

See also: sum0, product, reduce

sum0

Like sum, but returns 0 instead of undef for an empty list.

See also: sum

tail

Returns the last SIZE elements from LIST. If SIZE is negative, returns all but the first -SIZE elements.

See also: head

uniq

Filters a list of values to remove subsequent duplicates, as determined by a string comparison. Preserves the order of first occurrence. Also available as uniqstr.

Compares values as strings to determine uniqueness. In scalar context, returns the number of unique elements. undef is treated as a distinct value (not equal to empty string).

See also: uniqnum, uniqint

uniqint

Filters a list of values to remove subsequent duplicates, as determined by an integer comparison. Values are truncated to integers before comparing. Preserves the order of first occurrence.

In scalar context, returns the number of unique elements.

See also: uniq, uniqnum

uniqnum

Filters a list of values to remove subsequent duplicates, as determined by a numeric comparison. Preserves the order of first occurrence.

Compares values numerically. 0 and -0 are considered equal. NaN values are considered equal to each other. In scalar context, returns the number of unique elements.

See also: uniq, uniqint

uniqstr

unpairs

The inverse of pairs. Takes a list of array references and returns a flat list of their elements. Each array ref contributes its first two elements (key and value).

See also: pairs, pairkeys, pairvalues

zip

Returns a list of array references, each containing one element from each input array. Also available as zip_longest. Shorter arrays are padded with undef.

See also: zip_shortest, mesh, mesh_shortest

zip_longest

zip_shortest

Like zip, but stops at the shortest input array instead of padding shorter arrays with undef.

See also: zip, mesh_shortest

MIME::Base64

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides base64 encoding and decoding functions compatible with Perl’s MIME::Base64 module. Since PetaPerl does not support XS, this module is implemented directly in Rust.

Synopsis

use MIME::Base64;
my $encoded = encode_base64("Hello, World!");
my $decoded = decode_base64($encoded);
# Suppress line breaks
my $oneline = encode_base64($data, "");
# URL-safe variant
use MIME::Base64 'encode_base64url';
my $token = encode_base64url($data);

Functions

decode_base64

Decode a base64-encoded string back to the original binary data. Whitespace in the input is silently ignored.

use MIME::Base64;
my $decoded = decode_base64("SGVsbG8sIFdvcmxkIQ==");

decode_base64url

Decode a URL-safe base64-encoded string back to binary data.

use MIME::Base64 'decode_base64url';
my $data = decode_base64url($token);

decoded_base64_length

Return the length of the data that decode_base64 would produce for the given encoded string, without actually decoding it.

encode_base64

Encode binary data to base64. An optional second argument specifies the end-of-line sequence inserted every 76 characters (default: “\n”). Pass “” to suppress line breaks.

use MIME::Base64;
my $encoded = encode_base64("Hello, World!");
my $oneline = encode_base64($data, "");

encode_base64url

Encode binary data using the URL-safe base64 alphabet (- and _ instead of + and /), without padding or line breaks.

use MIME::Base64 'encode_base64url';
my $token = encode_base64url($data);

encoded_base64_length

Return the length of the string that encode_base64 would produce for the given data, without actually encoding it.

MIME::QuotedPrint

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides quoted-printable encoding and decoding functions compatible with Perl’s MIME::QuotedPrint module (part of MIME::Base64 distribution). Since PetaPerl does not support XS, this module is implemented directly in Rust.

Functions

decode_qp

Decode a quoted-printable encoded string back to the original data. Soft line breaks (=\n) are removed, =XX sequences are decoded, and trailing whitespace before newlines is stripped.

use MIME::QuotedPrint;
my $decoded = decode_qp($encoded);

encode_qp

Encode a string using quoted-printable encoding (RFC 2045). Non-printable and high-bit characters are represented as =XX. An optional second argument specifies the EOL sequence for soft line breaks (default: “\n”). An optional third argument enables binary mode, encoding CR and LF as =0D and =0A.

use MIME::QuotedPrint;
my $encoded = encode_qp("Hello =World");
my $binary  = encode_qp($data, "\n", 1);

Math::GMP

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Math::GMP — High-speed arbitrary precision integers

This is the Perl-facing frontend for Peta::FFI::GMP. It:

  1. Triggers Peta::FFI::GMP::register() to load libgmp and wire up all ops
  2. Registers the overloaded operators for the Math::GMP class
  3. Implements Math::GMP::new() with its argument parsing logic
  4. Registers aliases: gcd → bgcd, lcm → blcm, DESTROY → destroy

All arithmetic, comparison, and utility functions are already registered under Math::GMP::* by Peta::FFI::GMP::register(), so we only add the Perl-level glue here.

Functions

new

Constructs a new Math::GMP object from a string, integer, or existing Math::GMP.

use Math::GMP;
my $n = Math::GMP->new("123456789012345678901234567890");
my $hex = Math::GMP->new("ff", 16);

See also: new_from_scalar, new_from_scalar_with_base, gmp_copy

POSIX

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Native implementation of Perl’s POSIX module

Provides POSIX constants, math functions, system calls, and utilities.

Implemented Categories

  • Constants: errno, signals, wait status, fcntl/open/seek, file modes, float/integer limits, math constants, FP classification, stdio/stdlib, termios, unistd
  • Math functions: 1-arg (acos..trunc), 2-arg (copysign..remainder), special (frexp, modf, ldexp, fma, remquo, etc.)
  • FP classification: fpclassify, ilogb, isfinite, isinf, isnan, etc.
  • Time functions: asctime, mktime, clock, ctime, difftime, strftime, times, tzset, tzname
  • System info: uname, sysconf, pathconf, fpathconf
  • String functions: strtod, strtol, strtoul, strcoll, strerror, strstr
  • File I/O: open, mkfifo, access
  • Process control: _exit, abort, nice, pause, setgid, setuid, etc.
  • FD operations: close, dup, dup2, lseek, pipe, read, write, etc.
  • Terminal: ctermid, ttyname
  • Locale: setlocale, localeconv

Synopsis

use POSIX qw(floor ceil strftime uname strtod);
my $f = floor(3.7);          # 3
my $c = ceil(3.2);           # 4
my $str = strftime("%Y-%m-%d %H:%M:%S", localtime);
my ($sysname, $nodename, $release, $version, $machine) = uname();
my ($num, $unparsed) = strtod("3.14foo");  # (3.14, 3)
use POSIX ':signal_h';
sigaction(SIGTERM, POSIX::SigAction->new(sub { die "caught" }));

Functions

SigAction::flags

Accessor/mutator methods for SigAction fields. Called with no argument to get the value, or with one argument to set it.

POSIX::Termios

SigAction::handler

Accessor/mutator methods for SigAction fields. Called with no argument to get the value, or with one argument to set it.

POSIX::Termios

SigAction::mask

Accessor/mutator methods for SigAction fields. Called with no argument to get the value, or with one argument to set it.

POSIX::Termios

SigAction::new

Create a new signal action object with a handler, signal mask, flags, and optional safe flag. Returns a blessed POSIX::SigAction object.

my $act = POSIX::SigAction->new(\&handler, $mask, SA_RESTART);

SigAction::safe

Accessor/mutator methods for SigAction fields. Called with no argument to get the value, or with one argument to set it.

POSIX::Termios

SigSet::addset

Add or remove a single signal from the set. Returns 0 on success, -1 for out-of-range signal numbers.

SigSet::delset

Add or remove a single signal from the set. Returns 0 on success, -1 for out-of-range signal numbers.

SigSet::emptyset

Clear all signals from (or add all signals to) the set. Returns 0 on success.

SigSet::fillset

Clear all signals from (or add all signals to) the set. Returns 0 on success.

SigSet::ismember

Test whether a signal is in the set. Returns 1 if present, 0 if absent.

POSIX::SigAction

SigSet::new

Create a new signal set, optionally populated with the given signal numbers. Returns a blessed POSIX::SigSet object.

use POSIX qw(SIGINT SIGTERM);
my $set = POSIX::SigSet->new(SIGINT, SIGTERM);

Termios::getattr

Call tcgetattr() on the given file descriptor and store the result. Returns 0 on success, -1 on failure.

$termios->getattr(fileno(STDIN));

Termios::getcc

Get or set a control character by index (e.g., VEOF, VINTR).

my $cc = $termios->getcc(VEOF);
$termios->setcc(VEOF, 4);

Math Builtins (re-exported)

Termios::getcflag

Return the stored input, output, control, or local mode flags.

Termios::getiflag

Return the stored input, output, control, or local mode flags.

Termios::getispeed

Return the stored input or output baud rate.

Termios::getlflag

Return the stored input, output, control, or local mode flags.

Termios::getoflag

Return the stored input, output, control, or local mode flags.

Termios::getospeed

Return the stored input or output baud rate.

Termios::new

Create a new Termios object with all fields initialized to zero. Returns a blessed POSIX::Termios object.

my $termios = POSIX::Termios->new();

Termios::setattr

Call tcsetattr() with the stored termios values and the given action (TCSANOW, TCSADRAIN, or TCSAFLUSH).

Termios::setcc

Get or set a control character by index (e.g., VEOF, VINTR).

my $cc = $termios->getcc(VEOF);
$termios->setcc(VEOF, 4);

Math Builtins (re-exported)

Termios::setcflag

Set the input, output, control, or local mode flags.

Termios::setiflag

Set the input, output, control, or local mode flags.

Termios::setispeed

Set the input or output baud rate.

Termios::setlflag

Set the input, output, control, or local mode flags.

Termios::setoflag

Set the input, output, control, or local mode flags.

Termios::setospeed

Set the input or output baud rate.

WEXITSTATUS

Inspect the status value returned by waitpid(). Standard POSIX wait macros.

use POSIX ':sys_wait_h';
if (WIFEXITED($status)) { print "exit code: ", WEXITSTATUS($status); }

FD Operations

WIFEXITED

Inspect the status value returned by waitpid(). Standard POSIX wait macros.

use POSIX ':sys_wait_h';
if (WIFEXITED($status)) { print "exit code: ", WEXITSTATUS($status); }

FD Operations

WIFSIGNALED

Inspect the status value returned by waitpid(). Standard POSIX wait macros.

use POSIX ':sys_wait_h';
if (WIFEXITED($status)) { print "exit code: ", WEXITSTATUS($status); }

FD Operations

WIFSTOPPED

Inspect the status value returned by waitpid(). Standard POSIX wait macros.

use POSIX ':sys_wait_h';
if (WIFEXITED($status)) { print "exit code: ", WEXITSTATUS($status); }

FD Operations

WSTOPSIG

Inspect the status value returned by waitpid(). Standard POSIX wait macros.

use POSIX ':sys_wait_h';
if (WIFEXITED($status)) { print "exit code: ", WEXITSTATUS($status); }

FD Operations

WTERMSIG

Inspect the status value returned by waitpid(). Standard POSIX wait macros.

use POSIX ':sys_wait_h';
if (WIFEXITED($status)) { print "exit code: ", WEXITSTATUS($status); }

FD Operations

_exit

Terminate the process immediately without cleanup (unlike exit).

abort

Abort the process, generating a core dump.

abs

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

access

Check file accessibility (R_OK, W_OK, X_OK, F_OK). Returns “0 but true” on success.

Process Control

acos

Inverse trigonometric and hyperbolic functions.

acosh

Inverse trigonometric and hyperbolic functions.

alarm

Schedule a SIGALRM signal after the specified number of seconds. Returns the number of seconds remaining from a previous alarm.

asctime

Convert broken-down time to a string in ctime(3) format.

asin

Inverse trigonometric and hyperbolic functions.

asinh

Inverse trigonometric and hyperbolic functions.

atan

Inverse trigonometric and hyperbolic functions.

atan2

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

atanh

Inverse trigonometric and hyperbolic functions.

cbrt

Cube root and hyperbolic/trigonometric functions.

ceil

Return the smallest integer not less than the argument.

use POSIX 'ceil';
my $c = ceil(3.2);  # 4

chdir

Change the current working directory. Returns 1 on success, 0 on failure.

chmod

Change file permissions. Returns 1 on success, 0 on failure.

clock

Return the processor time consumed by the program in clock ticks.

close

Close a file descriptor. Returns 0 on success, -1 on failure.

constant

if the name is not a known constant.

copysign

Return a value with the magnitude of x and the sign of y.

cos

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

cosh

Cube root and hyperbolic/trigonometric functions.

creat

Create a new file or truncate an existing one. Equivalent to open($path, O_WRONLY|O_CREAT|O_TRUNC, $mode). Returns a file descriptor or undef on failure.

ctermid

Return the pathname of the controlling terminal.

ctime

Convert epoch seconds to a human-readable local time string.

difftime

Return the difference in seconds between two time values.

dup

Duplicate a file descriptor. dup2 duplicates to a specific target FD.

dup2

Duplicate a file descriptor. dup2 duplicates to a specific target FD.

erf

Error functions and gamma functions.

erfc

Error functions and gamma functions.

errno

Return the current value of the C errno variable.

use POSIX 'errno';
my $err = errno();

Perl Builtin Re-exports

exit

Terminate the process with the given exit code.

exp

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

exp2

Exponential and logarithmic functions.

expm1

Exponential and logarithmic functions.

fabs

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

fcntl

Perform a file control operation on a file descriptor. Returns the result value or undef on failure.

use POSIX qw(fcntl F_GETFL);
my $flags = fcntl($fd, F_GETFL, 0);

fdim

Positive difference, maximum, and minimum of two floats.

fegetround

Get the current floating-point rounding direction.

fesetround

Set the floating-point rounding direction (FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO).

Time Functions

fileno

Return the file descriptor number (pass-through).

floor

Return the largest integer not greater than the argument.

use POSIX 'floor';
my $f = floor(3.7);  # 3

fma

Fused multiply-add: x*y + z with a single rounding step.

fmax

Positive difference, maximum, and minimum of two floats.

fmin

Positive difference, maximum, and minimum of two floats.

fmod

Return the floating-point remainder of x/y.

use POSIX 'fmod';
my $r = fmod(10.5, 3.0);  # 1.5

fork

Create a child process. Returns the child PID to the parent, 0 to the child, or undef on failure.

fpathconf

Get the value of a configurable pathname/file-descriptor limit.

String Functions

fpclassify

Classify a floating-point value (FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL).

frexp

Split a float into normalized fraction and exponent. Returns (fraction, exponent).

use POSIX 'frexp';
my ($frac, $exp) = frexp(8.0);  # (0.5, 4)

getcwd

Return the current working directory as a string.

getegid

Return the real or effective user/group ID.

getenv

Return the value of an environment variable, or undef if not set.

Character Classification

geteuid

Return the real or effective user/group ID.

getgid

Return the real or effective user/group ID.

getgrgid

Look up a group database entry by name or GID. Returns a 4-element list: (name, passwd, gid, members). Returns an empty list if not found.

Additional File Operations

getgrnam

Look up a group database entry by name or GID. Returns a 4-element list: (name, passwd, gid, members). Returns an empty list if not found.

Additional File Operations

getlogin

Return the login name associated with the current session, or undef.

getpgrp

Return the process group ID of the calling process.

getpid

Return the process ID or parent process ID.

getppid

Return the process ID or parent process ID.

getpwnam

Look up a password database entry by name or UID. Returns a 10-element list: (name, passwd, uid, gid, quota, comment, gecos, dir, shell, expire). Returns an empty list if not found.

use POSIX 'getpwnam';
my @pw = getpwnam('root');

getpwuid

Look up a password database entry by name or UID. Returns a 10-element list: (name, passwd, uid, gid, quota, comment, gecos, dir, shell, expire). Returns an empty list if not found.

use POSIX 'getpwnam';
my @pw = getpwnam('root');

getuid

Return the real or effective user/group ID.

hypot

Return sqrt(xx + yy) without overflow.

ilogb

Return the exponent of a float as a signed integer.

isalnum

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isalpha

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isatty

Test whether a file descriptor refers to a terminal. Returns 1 or 0.

use POSIX 'isatty';
print "interactive\n" if isatty(0);

iscntrl

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isdigit

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isfinite

Floating-point classification predicates. Return 1 (true) or 0 (false).

isgraph

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isgreater

Floating-point comparison macros. Compare two NV values without raising FP exceptions on NaN. isunordered returns 1 if either argument is NaN. All return 1 (true) or 0 (false).

use POSIX qw(isgreater isunordered NAN);
isgreater(3.0, 2.0);     # 1
isunordered(NAN, 1.0);   # 1

Signal Handling

isgreaterequal

Floating-point comparison macros. Compare two NV values without raising FP exceptions on NaN. isunordered returns 1 if either argument is NaN. All return 1 (true) or 0 (false).

use POSIX qw(isgreater isunordered NAN);
isgreater(3.0, 2.0);     # 1
isunordered(NAN, 1.0);   # 1

Signal Handling

isinf

Floating-point classification predicates. Return 1 (true) or 0 (false).

isless

Floating-point comparison macros. Compare two NV values without raising FP exceptions on NaN. isunordered returns 1 if either argument is NaN. All return 1 (true) or 0 (false).

use POSIX qw(isgreater isunordered NAN);
isgreater(3.0, 2.0);     # 1
isunordered(NAN, 1.0);   # 1

Signal Handling

islessequal

Floating-point comparison macros. Compare two NV values without raising FP exceptions on NaN. isunordered returns 1 if either argument is NaN. All return 1 (true) or 0 (false).

use POSIX qw(isgreater isunordered NAN);
isgreater(3.0, 2.0);     # 1
isunordered(NAN, 1.0);   # 1

Signal Handling

islessgreater

Floating-point comparison macros. Compare two NV values without raising FP exceptions on NaN. isunordered returns 1 if either argument is NaN. All return 1 (true) or 0 (false).

use POSIX qw(isgreater isunordered NAN);
isgreater(3.0, 2.0);     # 1
isunordered(NAN, 1.0);   # 1

Signal Handling

islower

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isnan

Floating-point classification predicates. Return 1 (true) or 0 (false).

isnormal

Floating-point classification predicates. Return 1 (true) or 0 (false).

isprint

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

ispunct

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isspace

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isunordered

Floating-point comparison macros. Compare two NV values without raising FP exceptions on NaN. isunordered returns 1 if either argument is NaN. All return 1 (true) or 0 (false).

use POSIX qw(isgreater isunordered NAN);
isgreater(3.0, 2.0);     # 1
isunordered(NAN, 1.0);   # 1

Signal Handling

isupper

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

isxdigit

Locale-aware character classification functions. Accept an integer (ordinal value of a character) and return 1 (true) or 0 (false).

use POSIX qw(isdigit isalpha);
isdigit(ord('5'));  # 1
isalpha(ord('A'));  # 1

j0

Bessel functions of the first and second kind.

j1

Bessel functions of the first and second kind.

jn

Bessel functions of the first and second kind.

kill

Send a signal to a process. Returns the result of the underlying kill(2) call.

lchown

Change ownership of a symlink (does not follow the link).

Wait Status Macros

ldexp

Multiply a float by 2 raised to an integer power: x * 2^exp.

use POSIX 'ldexp';
my $v = ldexp(0.5, 4);  # 8.0

lgamma

Error functions and gamma functions.

Create or remove a hard link. Returns 1 on success, 0 on failure.

localeconv

Return a hash reference of locale-specific numeric formatting conventions.

FP Comparison

log

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

log10

Exponential and logarithmic functions.

log1p

Exponential and logarithmic functions.

log2

Exponential and logarithmic functions.

logb

Exponential and logarithmic functions.

lrint

Round to nearest long integer (lrint uses current rounding mode, lround rounds halfway away from zero).

FP Rounding Mode

lround

Round to nearest long integer (lrint uses current rounding mode, lround rounds halfway away from zero).

FP Rounding Mode

lseek

Reposition the file offset of an open file descriptor.

mkdir

Create or remove a directory. Returns 1 on success, 0 on failure.

mkfifo

Create a FIFO (named pipe) with the specified permissions.

mktime

Convert broken-down time to epoch seconds.

use POSIX 'mktime';
my $epoch = mktime($sec, $min, $hour, $mday, $mon, $year);

modf

Split a float into integer and fractional parts. Returns (fraction, integer).

use POSIX 'modf';
my ($frac, $int) = modf(3.75);  # (0.75, 3.0)

nan

Return a quiet NaN with the given payload string.

FP Classification

nearbyint

Round to nearest integer using current rounding mode.

Math: 2-argument (NV, NV -> NV)

nextafter

IEEE remainder and next representable float toward y.

Math: Special

nice

Change the process scheduling priority by the given increment.

open

Open a file using POSIX semantics (returns a raw file descriptor).

use POSIX qw(open O_RDONLY);
my $fd = POSIX::open("/etc/passwd", O_RDONLY);

pathconf

Get the value of a configurable pathname/file-descriptor limit.

String Functions

pause

Suspend the process until a signal is received.

pipe

Create a pair of connected file descriptors. Returns (read_fd, write_fd).

pow

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

printf

Format strings using Perl’s sprintf/printf. Re-exported through POSIX for compatibility with code that imports them from POSIX.

read

Read bytes from a file descriptor into a buffer. Returns the number of bytes read.

remainder

IEEE remainder and next representable float toward y.

Math: Special

remove

Remove a file or directory. Returns 1 on success, 0 on failure.

remquo

Return the remainder and partial quotient of x/y.

rename

Rename a file. Returns 1 on success, 0 on failure.

use POSIX 'rename';
rename("old.txt", "new.txt");

rint

Round to nearest integer using current rounding mode.

Math: 2-argument (NV, NV -> NV)

rmdir

Create or remove a directory. Returns 1 on success, 0 on failure.

round

Round to the nearest integer, halfway cases away from zero.

use POSIX 'round';
my $r = round(2.5);  # 3

scalbn

Scale a float by a power of the radix: x * FLT_RADIX^n.

setgid

Set group ID, user ID, process group ID, or create a new session.

setlocale

Set or query the program’s locale.

use POSIX 'setlocale';
setlocale(LC_ALL, "C");

setpgid

Set group ID, user ID, process group ID, or create a new session.

setsid

Set group ID, user ID, process group ID, or create a new session.

setuid

Set group ID, user ID, process group ID, or create a new session.

sigaction

Install or query a signal handler for a given signal number. Stub implementation: returns 0 (success) without installing a C-level handler. For real signal handling, use Perl’s %SIG.

use POSIX qw(sigaction SIGINT);
my $act = POSIX::SigAction->new('IGNORE');
sigaction(SIGINT, $act);

signbit

Floating-point classification predicates. Return 1 (true) or 0 (false).

sigpending

Store the set of currently pending signals into a SigSet object. Returns 0 on success, -1 on failure.

sigprocmask

Examine or change the process signal mask. $how is SIG_BLOCK, SIG_UNBLOCK, or SIG_SETMASK. Optionally stores the previous mask into $oldsigset.

use POSIX qw(sigprocmask SIG_BLOCK SIGINT);
my $new = POSIX::SigSet->new(SIGINT);
my $old = POSIX::SigSet->new();
sigprocmask(SIG_BLOCK, $new, $old);

POSIX::SigSet

sigsuspend

Temporarily replace the signal mask and suspend the process until a signal is delivered. Always returns -1 with errno EINTR.

use POSIX qw(sigsuspend);
my $set = POSIX::SigSet->new();
sigsuspend($set);

sin

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

sinh

Cube root and hyperbolic/trigonometric functions.

sleep

Suspend execution for the specified number of seconds (C-level, not Perl’s sleep).

sprintf

Format strings using Perl’s sprintf/printf. Re-exported through POSIX for compatibility with code that imports them from POSIX.

sqrt

Standard math functions re-exported through POSIX for compatibility. These delegate to the corresponding Perl builtins.

Process Identity

srand

Seed the C library random number generator.

Perl equivalent: POSIX

strcoll

Compare two strings according to the current locale.

strerror

Return the error message string for an errno value.

use POSIX 'strerror';
print strerror(2);  # "No such file or directory"

strftime

Format a broken-down time according to a format string.

use POSIX 'strftime';
my $str = strftime("%Y-%m-%d", localtime);

strstr

Find the first occurrence of a substring. Returns the offset or -1.

File I/O

strtod

Convert a string to a double. Returns (value, unparsed_length).

use POSIX 'strtod';
my ($val, $remaining) = strtod("3.14foo");

strtol

Convert a string to a long integer with the given base. Returns (value, unparsed_length).

use POSIX 'strtol';
my ($val, $remaining) = strtol("0xFF", 16);

strtoul

Convert a string to an unsigned long integer with the given base.

sysconf

Get the value of a configurable system limit or option.

system

Execute a shell command. Returns the exit status of the command.

tan

Cube root and hyperbolic/trigonometric functions.

tanh

Cube root and hyperbolic/trigonometric functions.

tcdrain

Terminal I/O control functions for draining output, flow control, flushing, and process groups.

Locale

tcflow

Terminal I/O control functions for draining output, flow control, flushing, and process groups.

Locale

tcflush

Terminal I/O control functions for draining output, flow control, flushing, and process groups.

Locale

tcgetpgrp

Terminal I/O control functions for draining output, flow control, flushing, and process groups.

Locale

tcsendbreak

Terminal I/O control functions for draining output, flow control, flushing, and process groups.

Locale

tcsetpgrp

Terminal I/O control functions for draining output, flow control, flushing, and process groups.

Locale

tgamma

Error functions and gamma functions.

time

Return the current epoch time in seconds.

times

Return process and child CPU times as (real, user, system, cuser, csystem).

tolower

Convert a character’s ordinal value to upper or lower case according to the current locale. Returns the converted ordinal value.

User/Group Database

toupper

Convert a character’s ordinal value to upper or lower case according to the current locale. Returns the converted ordinal value.

User/Group Database

trunc

Truncate toward zero (discard fractional part).

use POSIX 'trunc';
my $t = trunc(-3.7);  # -3

ttyname

Return the name of the terminal device associated with a file descriptor.

tzname

Return the standard and daylight-saving timezone names as a two-element list.

System Info

tzset

Set timezone information from the TZ environment variable.

umask

Set the file creation mask. Returns the previous mask value.

uname

Return system identification as (sysname, nodename, release, version, machine).

use POSIX 'uname';
my ($sys, $node, $rel, $ver, $mach) = uname();

Create or remove a hard link. Returns 1 on success, 0 on failure.

wait

Wait for a child process to change state. wait waits for any child; waitpid waits for a specific PID with the given flags (e.g., WNOHANG).

waitpid

Wait for a child process to change state. wait waits for any child; waitpid waits for a specific PID with the given flags (e.g., WNOHANG).

write

Write bytes from a buffer to a file descriptor. Returns the number of bytes written.

Terminal Control

y0

Bessel functions of the first and second kind.

y1

Bessel functions of the first and second kind.

yn

Bessel functions of the first and second kind.

PadWalker

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

PadWalker provides introspective access to lexical variables in Perl subroutine pads (scratchpads). It is widely used by debugging tools, test frameworks, and REPL implementations.

Implemented Functions

  • peek_my(N) — hashref of my vars at call depth N
  • peek_our(N) — hashref of our (package) vars visible at call depth N
  • peek_sub(\\&sub) — hashref of all pad vars in a CV
  • closed_over(\\&sub) — hashref of only the captured (closed-over) vars
  • set_closed_over(\\&sub, \\%h) — replace closed-over vars from a hashref
  • var_name(N, \\$ref) — find the name of a variable by reference identity

Semantics

peek_my(0) returns variables in the calling scope (the Perl sub that called peek_my). interp.hot.pad is the current frame (level 0), and call_stack[len - N].saved_pad gives level N’s pad.

Values in the returned hashref are live references so that mutations like ${$h->{'$x'}} = 42 update the actual variable in the target scope.

For array slots (@arr), the value is an ARRAY ref pointing to the actual Av. For hash slots (%hash), the value is a HASH ref pointing to the actual Hv. For scalar slots ($x), the value is a SCALAR ref via the live cell.

Reference

See perl5-modules/PadWalker-2.5/PadWalker.xs for the original XS algorithm.

Peta::FFI

pperl-specific module built into the interpreter. Runtime: PP.

Peta::FFI — Dynamic FFI for calling C library functions from Perl

Three layers:

  • Layer 0: Raw FFI via dlopen() + call() — user provides type signatures
  • Layer 1: Pre-baked bindings — Peta::FFI::Libc, Peta::FFI::UUID
  • Layer 2: Discovery — scan() enumerates available libraries and functions

Uses libffi for the raw call dispatch — any C function signature works.

Functions

call

dlclose

dlopen

ffi_call

call C function via libffi

ffi_dlclose

close library handle

ffi_dlopen

returns library handle as integer

ffi_scan

returns hashref of { soname => path } for all system libraries

scan

Peta::FFI::GMP

pperl-specific module built into the interpreter. Runtime: PP.

Peta::FFI::GMP — Pre-baked libgmp bindings (Layer 1)

Uses dlopen("libgmp.so.10") at registration time. If libgmp is not installed, use Peta::FFI::GMP (or use Math::GMP) dies with a helpful message.

GMP integers (mpz_t) are heap-allocated via libc malloc/free and stored as raw pointers (Box::into_raw cast to i64) inside blessed Perl refs of class “Math::GMP”. This exactly mirrors what the XS module does.

Object lifecycle: new() → malloc(sizeof(mpz_t)) + mpz_init_set_str → bless as Math::GMP DESTROY() → mpz_clear + free

All arithmetic ops return new blessed objects (matching XS behaviour).

Functions

_gmp_lib_version

add_ui_gmp

band

bdiv

bfac

bgcd

bior

blcm

blshift

bmodinv

bmulf

bnok

broot

brootrem

brshift

bsqrt

bsqrtrem

bxor

destroy

mpz_clear + free; called via DESTROY

div_2exp_gmp

fibonacci

get_str_gmp

gmp_copy

gmp_tstbit

intify

is_perfect_power

is_perfect_square

jacobi

legendre

mmod_gmp

mod_2exp_gmp

mul_2exp_gmp

new_from_scalar

new_from_scalar_with_base

op_add

op_bool

op_div

op_eq

op_mod

op_mul

op_numify

op_pow

op_spaceship

op_stringify

op_sub

powm_gmp

probab_prime

sizeinbase_gmp

stringify

uintify

Peta::FFI::Libc

pperl-specific module built into the interpreter. Runtime: PP.

Peta::FFI::Libc — Pre-baked libc bindings (Layer 1)

Calls libc::* directly via the libc crate — no dlopen needed. Each function is a NativeFn that reads args from the stack and pushes a result.

Functions

F_OK

R_OK

W_OK

X_OK

abs

access

chmod

chown

clock

getegid

getenv

geteuid

getgid

gethostname

getpid

getppid

getuid

labs

mkdir

rmdir

setenv

sleep

strerror

strlen

time

uname

unsetenv

usleep

Peta::FFI::UUID

pperl-specific module built into the interpreter. Runtime: PP.

Peta::FFI::UUID — Pre-baked libuuid bindings (Layer 1)

Uses dlopen("libuuid.so.1") at registration time. If libuuid is not installed, use Peta::FFI::UUID dies with a helpful message.

Functions:

  • uuid_generate() → returns UUID string
  • uuid_generate_random() → returns UUID string (random-based)
  • uuid_generate_time() → returns UUID string (time-based)
  • uuid_parse(string) → returns raw 16-byte binary UUID or undef on failure
  • uuid_is_null(string) → returns boolean
  • uuid_unparse(raw) → returns UUID string from 16-byte binary

Functions

uuid_generate

uuid_generate_random

uuid_generate_time

uuid_is_null

uuid_parse

uuid_unparse

Scalar::Util

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides scalar utility functions (blessed, reftype, etc.)

Synopsis

use Scalar::Util qw(blessed reftype weaken looks_like_number);
if (blessed $obj) {
    print ref($obj), "\n";
}
my $type = reftype($ref);    # "HASH", "ARRAY", etc.
weaken($ref);                # prevent circular reference leak
if (looks_like_number($str)) {
    print "$str is numeric\n";
}

Functions

blessed

Returns the package name if the argument is a blessed reference, undef otherwise.

See also: ref, reftype

dualvar

Creates a scalar that has both numeric and string values independently. Currently limited in PetaPerl — returns only the numeric value.

isdual

Returns true if EXPR is a dualvar (has independent numeric and string values). Not implemented in PetaPerl — always returns false.

isvstring

Returns true if EXPR was created as a v-string (v1.2.3). Not implemented in PetaPerl — always returns false.

isweak

Returns true if REF is a weak reference.

See also: weaken, unweaken

looks_like_number

Returns true if EXPR looks like a number to Perl (integer, float, or scientific notation). Useful for input validation.

openhandle

Returns FH if it is an open filehandle, undef otherwise. Recognizes STDIN, STDOUT, STDERR and PetaPerl filehandle IDs.

readonly

Returns true if SCALAR is read-only. Currently not implemented in PetaPerl — always returns false.

refaddr

Returns the internal memory address of the referent as a plain integer. Returns undef for non-references.

See also: reftype, blessed

reftype

Returns the underlying type of the reference (SCALAR, ARRAY, HASH, CODE, GLOB, REF) without regard to blessing. Returns undef for non-references.

See also: blessed, ref

set_prototype

Sets the prototype of a subroutine at runtime. CODEREF must be a reference to a subroutine. PROTO is the new prototype string, or undef to remove the prototype. Returns the coderef.

tainted

Returns true if EXPR is tainted. PetaPerl does not implement taint mode — always returns false.

unweaken

Strengthens a weak reference back to a normal (strong) reference.

See also: weaken, isweak

weaken

Weakens a reference so it does not prevent garbage collection of the referent. The reference will become undef when the referent is destroyed.

See also: unweaken, isweak

Socket

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Native implementation of Perl’s Socket module

Provides socket-related constants and functions compatible with Perl’s Socket module. Since PetaPerl does not support XS, this module is implemented directly in Rust.

Synopsis

use Socket qw(inet_aton inet_ntoa pack_sockaddr_in
              unpack_sockaddr_in getaddrinfo);
my $packed = inet_aton("127.0.0.1");
my $ip     = inet_ntoa($packed);
my $sockaddr = pack_sockaddr_in(80, inet_aton("127.0.0.1"));
my ($port, $addr) = unpack_sockaddr_in($sockaddr);
# Name resolution
my ($err, @res) = getaddrinfo("localhost", "http");

Constants

Address families (AF_INET, AF_INET6, AF_UNIX, etc.), socket types (SOCK_STREAM, SOCK_DGRAM, etc.), protocol constants, and more.

Functions

getaddrinfo

Resolve a hostname and service name to a list of socket address structures. Returns a list of hashrefs with keys: family, socktype, protocol, addr, canonname.

use Socket qw(getaddrinfo);
my ($err, @res) = getaddrinfo("localhost", "http");

getnameinfo

Reverse-resolve a packed sockaddr to a hostname and service name.

use Socket qw(getnameinfo);
my ($err, $host, $service) = getnameinfo($sockaddr);

inet_aton

Convert an IPv4 address string (dotted-quad or hostname) to a packed 4-byte binary address. Returns undef on failure.

use Socket 'inet_aton';
my $packed = inet_aton("127.0.0.1");

inet_ntoa

Convert a packed 4-byte binary IPv4 address to a dotted-quad string.

use Socket 'inet_ntoa';
my $ip = inet_ntoa($packed);  # "127.0.0.1"

inet_ntop

Address-family-aware conversion of a packed binary address to a string.

use Socket qw(inet_ntop AF_INET);
my $ip = inet_ntop(AF_INET, $packed);

inet_pton

Address-family-aware conversion of an IP address string to packed binary form. Supports AF_INET (4 bytes) and AF_INET6 (16 bytes).

use Socket qw(inet_pton AF_INET6);
my $packed = inet_pton(AF_INET6, "::1");

pack_sockaddr_in

Pack a port number and a packed IPv4 address into a sockaddr_in structure.

use Socket;
my $sockaddr = pack_sockaddr_in(80, inet_aton("127.0.0.1"));

pack_sockaddr_in6

Pack a port, packed IPv6 address, scope ID, and flow info into a sockaddr_in6 structure.

pack_sockaddr_un

Pack a Unix domain socket path into a sockaddr_un structure.

use Socket 'pack_sockaddr_un';
my $sockaddr = pack_sockaddr_un("/tmp/my.sock");

sockaddr_family

Extract the address family (AF_INET, AF_INET6, AF_UNIX) from a packed sockaddr.

sockaddr_in

Context-dependent: in list context, equivalent to unpack_sockaddr_in; with two arguments, equivalent to pack_sockaddr_in.

sockaddr_in6

IPv6 equivalent of sockaddr_in.

unpack_sockaddr_in

Unpack a sockaddr_in structure into a port number and packed IPv4 address.

use Socket;
my ($port, $addr) = unpack_sockaddr_in($sockaddr);

unpack_sockaddr_in6

Unpack a sockaddr_in6 structure into port, address, scope ID, and flow info.

unpack_sockaddr_un

Extract the pathname from a sockaddr_un structure.

Storable

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides serialization/deserialization functions for Perl data structures. This module implements a subset of Storable’s functionality compatible with common use cases.

Synopsis

use Storable qw(freeze thaw dclone store retrieve);
my $frozen = freeze(\%hash);
my $thawed = thaw($frozen);
store(\%hash, '/tmp/data.storable');
my $data = retrieve('/tmp/data.storable');
my $copy = dclone(\@deep_structure);

Functions

dclone

Deep-clone a Perl data structure (freeze + thaw in one step). Handles circular references.

use Storable 'dclone';
my $copy = dclone(\%original);

fd_retrieve

Deserialize a data structure from an open filehandle.

file_magic

Return a hash describing the Storable file format magic of a given file.

freeze

Serialize a Perl data structure into a binary string (in-memory). Uses native byte order.

use Storable 'freeze';
my $serialized = freeze(\%hash);

lock_nstore

Locking variants of store, nstore, and retrieve (currently aliases without actual locking).

lock_retrieve

Locking variants of store, nstore, and retrieve (currently aliases without actual locking).

lock_store

Locking variants of store, nstore, and retrieve (currently aliases without actual locking).

nfreeze

Serialize a Perl data structure into a binary string using network (big-endian) byte order for portability across architectures.

use Storable 'nfreeze';
my $portable = nfreeze(\@array);

nstore

Serialize a data structure and write it to a file using network byte order.

nstore_fd

Serialize a data structure to an open filehandle (native or network byte order).

read_magic

Return a hash describing the Storable format magic from a binary string header.

recursion_limit

Get or set the maximum recursion depth for serialization/deserialization.

recursion_limit_hash

Get or set the maximum recursion depth for serialization/deserialization.

retrieve

Read a file written by store/nstore and deserialize it back into a data structure.

use Storable 'retrieve';
my $data = retrieve('/tmp/data.storable');

stack_depth

Get or set the maximum recursion depth for serialization/deserialization.

stack_depth_hash

Get or set the maximum recursion depth for serialization/deserialization.

store

Serialize a data structure and write it to a file. Uses native byte order.

use Storable 'store';
store(\%hash, '/tmp/data.storable');

store_fd

Serialize a data structure to an open filehandle (native or network byte order).

thaw

Deserialize a binary string (produced by freeze or nfreeze) back into a Perl data structure.

use Storable 'thaw';
my $data = thaw($serialized);

Sub::Util

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides subroutine utility functions for introspection and manipulation.

Synopsis

use Sub::Util qw(subname set_subname);
my $name = subname(\&foo);    # "main::foo"
my $anon = sub { ... };
set_subname("MyPkg::handler", $anon);

Functions

prototype

Return the prototype string of a subroutine, or undef if no prototype is set.

use Sub::Util 'prototype';
my $proto = prototype(\&mysub);  # e.g. "$$" or undef

set_prototype

Set or remove the prototype of a subroutine reference. Pass undef to remove the prototype. Mutates the CV in-place and returns the same reference.

use Sub::Util 'set_prototype';
set_prototype('$$', \&mysub);
set_prototype(undef, \&mysub);  # remove prototype

Perl equivalent: Sub::Util (part of Scalar-List-Utils distribution)

set_subname

Set the name of a subroutine reference. Mutates the CV in-place and returns the same reference. Unqualified names are placed in the “main::” namespace.

use Sub::Util 'set_subname';
my $sub = set_subname("MyPkg::renamed", \&original);

subname($$)

Return the fully qualified name of a subroutine reference (e.g. “main::foo”). Dies if the argument is not a code reference.

use Sub::Util 'subname';
my $name = subname(\&foo);  # "main::foo"

Sys::Hostname

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides hostname retrieval functionality.

Functions

hostname

Returns the hostname of the current machine via libc gethostname().

use Sys::Hostname;
my $host = hostname();

See also: Cwd, POSIX

Time::HiRes

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

pp runtime adapter for Time::HiRes.

Provides high-resolution time operations: sub-second timing, sleeping, and alarm scheduling.

Core time logic delegated to super::core.

mro

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

MRO (Method Resolution Order) implementation

Provides DFS and C3 linearization of class hierarchies, matching the behaviour of perl5’s mro core module (mro.xs).

Public API:

  • get_linear_isa(class [, "dfs"|"c3"]) — recursive linearization
  • set_mro(class, "dfs"|"c3") — per-class MRO preference
  • get_mro(class) — return stored preference (default “dfs”)
  • get_isarev(class) — reverse @ISA index (computed on-the-fly)
  • get_pkg_gen(class) — generation counter (per-package, real tracking)
  • is_universal(class) — true only for “UNIVERSAL”
  • method_changed_in / invalidate_all_method_caches — cache invalidation

Functions

get_isarev

Returns an arrayref of classes that directly or indirectly inherit from the given class (reverse @ISA index, computed on the fly).

use mro;
my $subclasses = mro::get_isarev('BaseClass');

get_linear_isa

Returns the linearized @ISA for a class using DFS (default) or C3 algorithm.

use mro;
my $isa = mro::get_linear_isa('MyClass');        # DFS
my $isa = mro::get_linear_isa('MyClass', 'c3');  # C3

get_mro

Returns the stored MRO algorithm name for a class (defaults to "dfs").

use mro;
my $type = mro::get_mro('MyClass');  # "dfs" or "c3"

get_pkg_gen

Returns the per-package generation counter. Bumped on each define_sub or method_changed_in call. Returns 0 for non-existent packages, 1 for packages with no explicit counter yet.

use mro;
my $gen = mro::get_pkg_gen('MyClass');

import

Called implicitly by use mro 'c3'. Sets the MRO algorithm for the calling package.

use mro 'c3';  # sets C3 for current package

invalidate_all_method_caches

Clears the entire CV cache without targeting a specific class.

use mro;
mro::invalidate_all_method_caches();

is_universal

Returns true only if the given class is "UNIVERSAL".

use mro;
my $ok = mro::is_universal('UNIVERSAL');  # 1

method_changed_in

Bumps the generation counter for the given class and invalidates all CV caches. Call when method resolution may have changed.

use mro;
mro::method_changed_in('MyClass');

set_mro

Sets the MRO algorithm for a class. Valid values: "dfs", "c3".

use mro;
mro::set_mro('MyClass', 'c3');

sdl2

pperl-specific module built into the interpreter. Runtime: PP.

Native SDL2 module — dlopen-based SDL2 bindings with direct SDL2-style API.

Uses dlopen("libSDL2.so") at registration time. Exposes SDL2 functions directly with pointer handles as i64 scalars.

API

use SDL2;
SDL2::init(SDL2::INIT_VIDEO);
my $win  = SDL2::create_window("Title", $w, $h, $flags);
my $rend = SDL2::create_renderer($win, $flags);
my $tex  = SDL2::create_texture($rend, $w, $h);
## ... render loop ...
SDL2::update_texture($tex, $pixel_data, $pitch);
SDL2::render_clear($rend);
SDL2::render_copy($rend, $tex);
SDL2::render_present($rend);
my $type = SDL2::poll_event();  # returns event type or 0
SDL2::destroy_texture($tex);
SDL2::destroy_renderer($rend);
SDL2::destroy_window($win);
SDL2::quit();

Functions

INIT_AUDIO

INIT_EVERYTHING

INIT_TIMER

INIT_VIDEO

KEYDOWN

KEYUP

QUIT

RENDERER_ACCELERATED

RENDERER_PRESENTVSYNC

RENDERER_SOFTWARE

WINDOWPOS_UNDEFINED

WINDOW_FULLSCREEN

WINDOW_RESIZABLE

create_renderer

create_texture

Creates ARGB8888 streaming texture.

create_window

delay

destroy_renderer

destroy_texture

destroy_window

fill_rect

draw filled rect

get_error

get_ticks

init

poll_event

quit

render_clear

render_copy

render_mandelbrot_frame

Full Mandelbrot frame: compute + upload + present. All in native Rust with Rayon parallelism.

render_present

set_draw_color

update_frame

Takes an array ref of integer ARGB color values. No binary strings involved. This is the clean path — integers in, bytes out, no encoding issues.

update_texture

$pixel_data is a packed binary string of ARGB8888 pixels (perl5 path).

version

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Native implementation of the version module

Provides version object parsing, comparison, and stringification. The perl5 version module is XS-based — this native implementation provides the core methods needed by modules like experimental.

Supported API

  • version->new("1.23") / version->new(v1.2.3)
  • version->parse("1.23")
  • version->declare("v1.2.3")
  • $v->numify / $v->stringify / $v->normal
  • $v->is_alpha / $v->is_qv
  • Comparison via <=> and cmp (overloaded)

Functions

_VERSION

boolean

Overload handler for boolean context; returns true if any version part is non-zero.

use version;
my $v = version->new("0.0.0");
if ($v) { ... }  # false

See also: vcmp, numify

declare

Parses a version string as dotted-decimal (v-string) form.

use version;
my $v = version->declare("v1.2.3");
my $v = version->declare("1.2.3");  # auto-prefixed with v

See also: new, qv

import

Sets up UNIVERSAL::VERSION override to use version.pm comparison logic.

use version;

See also: _VERSION, new

is_alpha

Returns true if the version contains an underscore (alpha/dev release).

use version;
my $v = version->new("1.23_01");
print $v->is_alpha;  # 1

See also: is_qv

is_qv

Returns true if the version was created in dotted-decimal (v-string) form.

use version;
my $v = version->new("v1.2.3");
print $v->is_qv;  # 1

See also: is_alpha

new

Constructs a new version object from a version string or number.

use version;
my $v = version->new("1.23");
my $v = version->new("v1.2.3");

See also: parse, declare, qv

noop

Overload fallback handler; returns undef for unsupported operations.

use version;
# Called internally when an unsupported operator is applied

See also: vcmp, boolean, stringify

normal

Returns the normalized dotted-decimal form (e.g., “v1.2.3”).

use version;
my $v = version->new("1.002003");
print $v->normal;  # "v1.2.3"

See also: numify, stringify

numify

Returns the numeric representation of the version (e.g., “1.002003”).

use version;
my $v = version->new("v1.2.3");
print $v->numify;  # "1.002003"

See also: stringify, normal

overload_cmp

Overload handler for version comparison operators, delegates to vcmp().

use version;
my $v1 = version->new("1.2");
if ($v1 > version->new("1.0")) { ... }

See also: vcmp, native_overload_stringify

overload_stringify

Overload handler for string interpolation, delegates to stringify().

use version;
my $v = version->new("1.23");
print "$v";

See also: stringify, native_overload_cmp

parse

Parses a version string into a version object, identical to new().

use version;
my $v = version->parse("1.23");

See also: new, declare

qv

Constructs a version object in dotted-decimal form, alias for declare().

use version;
my $v = version::qv("1.2.3");

See also: declare, new

stringify

Returns the string representation of the version, used by overloaded “”.

use version;
my $v = version->new("v1.2.3");
print "$v";  # "v1.2.3"

See also: numify, normal

vcmp

Compares two version objects, returning -1, 0, or 1 (spaceship operator).

use version;
my $v1 = version->new("1.2");
my $v2 = version->new("1.3");
print $v1 <=> $v2;  # -1

See also: new, stringify

version_check

Replacement for UNIVERSAL::VERSION that uses version.pm comparison logic.

use version;
Foo->VERSION("1.23");  # dies if Foo's $VERSION < 1.23

See also: vcmp, new, import

Built-in Functions (P5 Runtime)

pperl P5 runtime built-in functions — faithful perl5 C mirror in Rust.

Array Functions

  • delete — delete($h{key}).
  • each — each(%hash).
  • exists — exists($h{key}).
  • keys — keys(%hash).
  • pop — pop(@array).
  • push — push(@array, @list).
  • shift — shift(@array).
  • sort — sort(@list).
  • splice — splice(@array, offset, length, list).
  • unshift — unshift(@array, @list).
  • values — values(%hash).

File Functions

Formatting

  • sprintf — sprintf($fmt, @args) builtin.

Hash Functions

I/O Functions

  • binmode — set binary/layer mode on a filehandle.
  • close — close a file handle.
  • eof — check end-of-file.
  • fileno — fileno(FH)
  • getc — getc(FH)
  • open — open a file handle.
  • pipe — pipe(READ, WRITE)
  • print — print LIST (optionally to a filehandle).
  • printf — printf FORMAT, LIST.
  • readline — read line(s) from a filehandle.
  • say — say LIST (print with trailing newline).
  • seek — seek(FH, POS, WHENCE)
  • select — 1-arg: set default output FH
  • sysopen — sysopen(FH, PATH, FLAGS [, MODE]).
  • sysread — sysread(FH, SCALAR, LENGTH [, OFFSET]).
  • syswrite — syswrite(FH, SCALAR [, LENGTH [, OFFSET]]).
  • tell — tell(FH)
  • truncate — truncate(FH_OR_NAME, LENGTH).

Numeric Functions

  • abs — Returns the absolute value of its argument.
  • atan2 — Returns the arctangent of Y/X in the range -pi to pi.
  • cos — Returns the cosine of its argument (in radians).
  • exp — Returns e (Euler’s number) raised to the power of its argument.
  • int — Truncates a numeric value toward zero.
  • log — Returns the natural logarithm (base e) of its argument.
  • rand — Returns a pseudo-random floating-point number.
  • sin — Returns the sine of its argument (in radians).
  • sqrt — Returns the square root of its argument.
  • srand — Seeds the pseudo-random number generator used by rand.

Process Functions

  • alarm — alarm($seconds).
  • die — die(@args).
  • exec — exec(COMMAND)
  • exit — exit($status).
  • fork — fork()
  • kill — kill($sig, @pids)
  • sleep — sleep($seconds)
  • system — system(COMMAND) or system(PROG, ARGS…).
  • wait — wait()
  • waitpid — waitpid($pid, $flags)
  • warn — warn(@args).

Regular Expressions

  • m// — m// and m//g matching.
  • pos — pos($str)
  • qr — compile regex to blessed Regexp reference.
  • quotemeta — quotemeta($str).
  • s/// — s/// substitution.
  • split — split($pattern, $string, $limit).
  • tr — tr/// transliteration.

Scope and Variables

  • bless — bless($ref, $classname).
  • caller — caller() function.
  • defined — defined($x).
  • do — do ‘file.pl’.
  • pos — pos($str)
  • prototype — CORE::prototype(&sub) or prototype(“name”).
  • ref — ref($x), returns type name or empty string.
  • require — require ‘file.pl’ or require Module.
  • reset — reset() builtin.
  • tie — tie a variable to a class.
  • tied — return tied object for a variable.
  • undef — produce undef (or undefine a variable).
  • untie — unbind a tied variable.
  • vec — vec(STRING, OFFSET, BITS).
  • wantarray — wantarray().

String Functions

  • chomp — remove trailing newline, returns number of chars removed.
  • chop — remove last character, returns removed character.
  • chr — chr($n), convert integer to single-byte character.
  • crypt — crypt($plaintext, $salt)
  • hex — hex($str).
  • index — find substring position.
  • join — join($sep, @list).
  • lc — lowercase (lc($x)).
  • lcfirst — lowercase first character (lcfirst($x)).
  • length — string length.
  • oct — oct($str).
  • ord — ord($s), get numeric value of first character.
  • pack — pack(TEMPLATE, LIST).
  • quotemeta — quotemeta($str).
  • reverse — reverse(@list) in list context, reverse($str) in scalar context.
  • rindex — reverse find substring position.
  • sprintf — sprintf($fmt, @args) builtin.
  • substr — substring extraction.
  • uc — uppercase (uc($x)).
  • ucfirst — uppercase first character (ucfirst($x)).
  • unpack — unpack(TEMPLATE, EXPR).

Time Functions

abs

Returns the absolute value of its argument.

Returns the absolute value of EXPR, or $_ if omitted. Preserves the numeric type: integers remain integers (Iv), floats remain floats (Nv).

Synopsis

my $n = abs(-42);      # 42
my $n = abs($x);
my $n = abs;           # abs($_)

See Also

int

alarm

Schedules a SIGALRM signal to be delivered after a given number of seconds.

Arranges for a SIGALRM to be delivered to the process after the specified number of seconds. If an alarm was already pending, the old alarm is cancelled and the number of seconds that were remaining is returned. Calling alarm(0) cancels the current alarm without setting a new one.

Commonly used with eval/die or $SIG{ALRM} to implement timeouts:

eval {
    local $SIG{ALRM} = sub { die "timeout\n" };
    alarm(10);
    # ... long operation ...
    alarm(0);
};

Negative values are clamped to 0.

Synopsis

my $remaining = alarm(30);   # fire SIGALRM in 30 seconds
alarm(0);                    # cancel any pending alarm

See Also

PP runtime: alarm | sleep, alarm, POSIX

atan2

Returns the arctangent of Y/X in the range -pi to pi.

Returns the arctangent of Y/X in radians, using the signs of both arguments to determine the quadrant. Both arguments are coerced to Nv (f64). Stack order: Y is below X (Y pushed first).

Synopsis

my $angle = atan2(1, 1);       # pi/4
my $pi    = atan2(0, -1);      # pi
my $angle = atan2($y, $x);

See Also

sin, cos

backticks

Executes a shell command and captures its standard output.

Passes the command string to /bin/sh -c "..." for execution. In scalar context, returns the entire captured stdout as a single string. In list context, returns a list of lines (each including its trailing newline), split on $/ (input record separator).

After execution, $? (child error) is set to the child process wait status. Stdin is inherited from the parent so that interactive commands (e.g. tput) work correctly; stderr is also inherited.

Synopsis

my $output = `command`;
my $output = qx{command};
my @lines  = `command`;

See Also

PP runtime: backticks | system, exec, open

binmode

Sets the I/O discipline (encoding layer) for a filehandle.

Configures the encoding layer on a filehandle. When called with just a filehandle, sets binary mode (disabling any CRLF translation, though Linux does not perform CRLF translation by default). When called with a layer string, applies the specified encoding.

Currently recognized layers:

  • :utf8, :encoding(UTF-8) – mark the handle as UTF-8
  • :raw, :bytes – mark the handle as raw bytes

Returns true on success, false on failure.

Synopsis

binmode(STDOUT);              # set binary mode (no CRLF translation)
binmode(STDOUT, ":utf8");     # set UTF-8 encoding layer
binmode(STDOUT, ":raw");      # remove all layers
binmode($fh, ":encoding(UTF-8)");

Implementation Notes

perl5 pp_sys.c:1013 (PP_wrapped pp_binmode):

  1. Pop optional discp (discipline/layer string) from stack.
  2. Resolve GV → IO → IoIFP.
  3. Call mode_from_discipline(d, len) for :raw/:crlf.
  4. Call PerlIO_binmode(fp, IoTYPE, mode, d) which calls PerlIO_apply_layers.

pperl: mirrors the flag model — parse layer string, set/clear PERLIO_F_UTF8. binmode(FH) → clear PERLIO_F_UTF8 (binmode with no layer = raw) binmode(FH, ‘:utf8’) → set PERLIO_F_UTF8 binmode(FH, ‘:raw’) → clear PERLIO_F_UTF8

See Also

PP runtime: binmode | open, PerlIO, Encode

bless

Associates a reference with a package, turning it into an object.

Marks the referent so that method calls will dispatch through the given class (or the current package if no class is specified). The reference is modified in place and also returned, enabling the common return bless {}, $class pattern.

Handles multiple referent types: for Av and Hv containers the blessing is stored via interior mutability on the container itself. For Rv wrapping an Av or Hv, the inner container is blessed. For scalar references, a new blessed RvInner is created.

Synopsis

my $obj = bless {}, 'MyClass';
my $obj = bless {};              # blesses into current package
my $obj = bless \$scalar, 'Foo';
my $obj = bless [], 'Bar';

Implementation Notes

perl5 (pp.c pp_bless): TOPs = classname (or current package if 1 arg) ref = TOPm1s SvSTASH_set(SvRV(ref), stash) SETs(ref)

p5: Set xmg_stash on referent’s body + SVs_OBJECT flag. perl5: SvSTASH_set(SvRV(ref), stash). bless is mark-based: pushmark, ref, classname, bless.

See Also

PP runtime: bless | ref, Scalar::Util

caller

Returns information about the calling subroutine’s context.

In scalar context, returns the package name of the calling subroutine. In list context, returns a multi-element list with (at minimum) package, filename, and line number. When called with a numeric argument N, returns information about the Nth stack frame above the current subroutine:

IndexField
0package name
1filename
2line number
3subroutine
4hasargs
5wantarray

Returns undef (scalar) or an empty list if the requested stack depth exceeds the actual call depth. caller(0) returns info from call_stack.last() (the current sub’s frame); caller(1) looks one frame further up.

When the op has WANT bits set to 0 (unknown), the context is inherited from the enclosing call frame, matching perl5 behavior.

Synopsis

my $package = caller;                  # scalar: package name
my ($pkg, $file, $line) = caller;      # list: basic triple
my ($pkg, $file, $line, $sub, ...) = caller(0);
my @info = caller(1);                  # caller's caller

Implementation Notes

perl5 (pp_ctl.c): walks cxstack to find the caller’s Sub frame. Returns (package, filename, line) in list context. Returns package in scalar context.

See Also

PP runtime: caller | die, warn, caller

chdir

Changes the process’s current working directory.

Changes the working directory to the specified path. If called with no argument or an empty string, changes to the directory specified by the HOME environment variable (or / if HOME is not set).

Returns 1 on success, undef on failure.

Synopsis

chdir("/tmp") or die "Cannot chdir: $!";
chdir($dir);
chdir();         # changes to $HOME
chdir("");       # also changes to $HOME

See Also

PP runtime: chdir | mkdir, rmdir, Cwd, getcwd

chmod

Changes file permissions on one or more files.

Changes the permissions of each listed file to MODE. The first argument is the numeric mode (typically given as an octal literal); the remaining arguments are file paths. Returns the number of files successfully changed. Preserves file-type bits (S_IFMT) from the existing mode and replaces only the permission bits (lower 12 bits).

Synopsis

chmod 0755, $file1, $file2;
chmod 0644, @files;
my $count = chmod 0600, glob("*.conf");

See Also

PP runtime: chmod | chown, stat, umask

chomp

Removes the trailing record separator (default: newline) from each argument. Returns the total number of characters removed.

Respects $/ (input record separator). When $/ is undef (slurp mode), chomp is a no-op. When $/ is "" (paragraph mode), removes all trailing newlines. Handles CRLF line endings — \r\n counts as 2 characters removed.

Synopsis

$count = chomp($var);
$count = chomp(@list);
chomp $var;

See Also

PP runtime: chomp | chop, $/

chop

Removes the last character from each argument. Returns the last character removed (from the final argument).

Unlike chomp, chop always removes exactly one character regardless of $/. Operates on each element when given an array. Returns the last character removed. Correctly handles multi-byte UTF-8 characters.

Synopsis

$char = chop($var);
$char = chop(@list);
chop $var;

See Also

PP runtime: chop | chomp

chown

Changes the owner and group of one or more files.

Changes the owner and group of each listed file. The first argument is the numeric UID, the second is the numeric GID, and the remaining arguments are file paths. A value of -1 for either UID or GID means “don’t change that field” (converted to u32::MAX for the libc call). Uses libc::chown (follows symlinks). Returns the number of files successfully changed.

Synopsis

chown $uid, $gid, @files;
chown 0, 0, '/etc/shadow';
chown -1, $gid, @files;    # -1 leaves uid unchanged

See Also

PP runtime: chown | chmod, stat

chr

Converts a Unicode code point number to the corresponding character.

Returns the character represented by the given code point. Negative values produce an empty string. Invalid Unicode code points (e.g. surrogates) produce the replacement character U+FFFD.

Synopsis

$char = chr(65);        # "A"
$char = chr(0x263A);    # "☺"

See Also

PP runtime: chr | ord

close

Closes a filehandle, flushing any buffered output.

Closes the filehandle and releases its resources from the global FILEHANDLE_TABLE. For in-memory filehandles (opened against a scalar reference), the accumulated buffer is written back to the target variable. For pipe filehandles, the child process’s stdin is closed and waitpid is called; $? is set to the child’s exit status.

Both lexical ($fh) and bareword (FH) filehandle forms are supported. The standard handles STDIN/STDOUT/STDERR are recognized by name.

Returns 1 on success, undef on failure.

Synopsis

close($fh) or die "Cannot close: $!";
close(FH);

Implementation Notes

perl5: pp_sys.c — close(FH) or close() (default STDOUT).

See Also

PP runtime: close | open, eof, $?

closedir

Closes a directory handle, releasing its resources.

Removes the directory handle from the global DIRHANDLE_TABLE. After closing, the handle is no longer valid for readdir or other directory operations.

Returns 1 on success, undef if the handle was not found or already closed.

Synopsis

closedir($dh);
closedir(DH);

See Also

PP runtime: closedir | opendir, readdir

cos

Returns the cosine of its argument (in radians).

Returns the cosine of EXPR (interpreted as an angle in radians), or $_ if omitted. The argument is coerced to Nv (f64). Always returns an Nv.

Synopsis

my $c = cos(0);        # 1
my $c = cos($radians);
my $c = cos;           # cos($_)

See Also

sin, atan2

crypt

One-way password hashing using the system crypt(3) library.

Calls the system crypt(3) function. The SALT determines the hashing algorithm (DES, MD5, SHA-256, SHA-512, etc. depending on the platform). The result can be compared against a stored hash to verify a password without storing the plaintext.

Synopsis

my $hashed = crypt($plaintext, $salt);
if (crypt($input, $hashed) eq $hashed) { ... }  # verify

See Also

PP runtime: crypt | Digest::SHA, Digest::MD5

defined

Tests whether a value is defined (not undef).

Pops one value from the stack and pushes 1 if it is defined, 0 otherwise. For code values (Cv), “defined” means the CV has actual executable code and is not merely an empty stub. For all other types, delegates to Sv::is_defined().

Synopsis

if (defined $x)        { ... }
if (defined &func)     { ... }   # is sub defined (not a stub)?
if (defined $hash{$k}) { ... }

Implementation Notes

perl5 (pp.c): sv = *PL_stack_sp; *PL_stack_sp = boolSV(SvOK(sv));

See Also

PP runtime: defined | undef, exists

delete

Removes the specified element(s) from a hash or array.

For hashes, removes the key-value pair and returns the deleted value (or undef if the key did not exist). Read-only hashes (such as %!) and Hash::Util-restricted hashes reject deletion with an error. For arrays, sets the element to undef without changing the array size; use splice to actually shrink.

Stash-backed hashes (accessed via %{"Package::"}) route the deletion through the PackageRegistry so the symbol is removed from the live stash, not just from the snapshot Hv.

Synopsis

delete $hash{$key};
delete @hash{@keys};               # hash slice
delete $array[$index];
my $val = delete $hash{$key};       # returns deleted value

Implementation Notes

perl5: pp_delete (pp.c:5614). Stack has hv_sv, key_sv. Deletes the key from the hash and returns the old value (or undef). hv.c:1310-1348: if SvMAGICAL(hv) and element SvMAGICAL, call mg_clear(sv) before removing from hash (e.g. PERL_MAGIC_envelem -> unsetenv).

See Also

PP runtime: delete | exists, each, keys, splice

die

Raises an exception, terminating execution unless caught by an eval block or a try/catch construct.

If called with a string argument, raises that string as the exception message. If the string does not end with a newline, Perl appends at FILE line LINE. If called with a reference (e.g. a hashref or blessed object), the reference is propagated as the exception value in $@.

When called with no arguments (or an empty string / undef), re-raises the current value of $@. If $@ is also empty, dies with "Died".

If there is an active try block (from use feature 'try'), the exception is caught and execution transfers to the catch block instead of propagating.

Synopsis

die "something went wrong";
die "Error at $file line $line\n";
die $exception_object;
die;                              # re-raises $@

Implementation Notes

perl5 (pp_sys.c:583): mark-based list op. If SP - MARK != 1, joins all args with “” via do_join into TARG. Then mess_sv for location if no trailing \n, then die_unwind.

If inside an eval, sets $@, unwinds cxstack to the eval frame, and returns the eval’s retop (so execution continues after the eval). If no eval on the cxstack, prints to stderr and terminates.

See Also

PP runtime: die | warn, eval

do

Reads, compiles, and executes a Perl file, returning the value of the last expression evaluated.

Unlike require, do FILE:

  • Always re-reads and re-executes the file (no %INC caching).
  • Does not die on failure; instead it returns undef and sets $@ to the error message.
  • Does not inherit caller lexicals (unlike eval STRING).
  • Evaluates the file in the current package context.

On success, returns the value of the last expression evaluated in the file and clears $@. On compilation or runtime error, returns undef and sets $@.

Synopsis

do "config.pl";
my $result = do "/path/to/file.pm";

See Also

PP runtime: do | require, eval, use

each

Returns the next key-value pair from a hash’s internal iterator.

Advances the hash’s internal iterator and returns the next entry. In list context, pushes a (key, value) pair. In scalar context, returns only the key. When the iterator is exhausted, returns an empty list (list context) or undef (scalar context), and the iterator resets automatically for the next traversal.

Note: keys and values reset the same internal iterator, so calling them during an each loop will restart iteration.

Synopsis

while (my ($k, $v) = each %hash) { ... }
while (my $k = each %hash) { ... }    # scalar context: key only

Implementation Notes

perl5 (pp.c): Returns (key, value) pair using the hash’s internal iterator. Returns empty list when iterator is exhausted. Does NOT reset the iterator — keys %hash or iterator exhaustion resets it.

See Also

PP runtime: each | keys, values, exists, delete

eof

Tests whether the next read on a filehandle will return end-of-file.

Returns true (1) when the filehandle has reached end-of-file, or an empty string (false) otherwise. Called without arguments, eof() checks the filehandle most recently read via <> or readline.

If the filehandle is invalid or has never been read, returns true.

Synopsis

if (eof($fh))  { ... }   # test specific filehandle
if (eof())     { ... }   # test last-read filehandle
if (eof)       { ... }   # same as eof()

See Also

PP runtime: eof | readline, read, tell

exec

Replaces the current process image with a new program via execvp(3).

On success, exec does not return because the current process image is replaced entirely. If exec does return, it means the call failed, and the return value is false.

In the single-argument form, shell metacharacters trigger execution via /bin/sh -c. If no metacharacters are detected, the command is executed directly. In the multi-argument form, the first argument is the program and subsequent arguments are passed directly (no shell involvement).

The child inherits Perl’s %ENV (the OS environment is replaced before exec).

Synopsis

exec("ls -la");              # shell form
exec("ls", "-la");           # list form (no shell)
exec { "ls" } "ls", "-l";   # indirect object form

See Also

PP runtime: exec | system, fork, exec

exists

Tests whether a hash key or array index is present in the container.

Pops a key and a container (hash or array) from the stack. For hashes, checks whether the key is present (even if the associated value is undef). For arrays, checks whether the index is within bounds and the element has not been deleted. Pushes 1 (true) or 0 (false).

When the container is a stash (package symbol table accessed via %{"Package::"}), checks for the existence of any symbol type (scalar, array, hash, or code) with the given name.

Synopsis

if (exists $hash{$key})   { ... }
if (exists $array[$idx])  { ... }
if (exists $stash{$name}) { ... }   # stash symbol lookup

Implementation Notes

perl5: TOS is the result of helem (the SV slot itself). But our helem already consumed the key. We need to handle this differently. Actually, perl5 uses a special flag path. For exists, the stack has the helem op’s result. In p5 we handle it: TOS has the helem SV. exists returns true if the helem produced a non-undef SV. pp_exists — exists($h{key}).

perl5: Pp_exists (pp.c). Stack has hv_sv, key_sv. Returns &PL_sv_yes if key exists, &PL_sv_no otherwise.

See Also

PP runtime: exists | defined, delete, keys

exit

Terminates the program with the given exit status.

Evaluates the argument as an integer exit status and terminates the process. If no argument is given, exits with status 0. END blocks and object destructors are run before exit (in perl5; pperl currently calls std::process::exit directly).

In daemon mode (config.daemon_mode), exit does not terminate the process but instead returns an Err(RuntimeError::Exit) so the caller can handle it.

Synopsis

exit;          # exits with status 0
exit 0;        # success
exit 1;        # failure
exit $code;

See Also

PP runtime: exit | die, END blocks

exp

Returns e (Euler’s number) raised to the power of its argument.

Returns e raised to the power of EXPR, or $_ if omitted. The argument is coerced to Nv (f64). Always returns an Nv.

Synopsis

my $e  = exp(1);       # 2.71828...
my $r  = exp($x);
my $r  = exp;          # exp($_)

See Also

log, sqrt

fileno

Returns the underlying OS file descriptor for a Perl filehandle.

Returns the numeric file descriptor associated with the given filehandle, or undef if the handle is not connected to a real OS file descriptor (e.g. in-memory handles, closed handles).

Accepts bareword filehandles (STDIN), lexical filehandles ($fh), and IO::Handle / IO::File objects (blessed hashrefs with __fh_id).

The standard streams always return their well-known descriptors: STDIN = 0, STDOUT = 1, STDERR = 2.

Synopsis

$fd = fileno(STDIN);     # 0
$fd = fileno(STDOUT);    # 1
$fd = fileno($fh);       # fd number or undef

See Also

PP runtime: fileno | open, close, IO::Handle

fork

Creates a child process by duplicating the current process.

Calls POSIX fork(2) to create a new child process. Both parent and child continue execution from the point of the fork call.

Returns:

  • undef on failure (fork could not be performed)
  • 0 in the child process
  • The child’s PID (positive integer) in the parent process

Synopsis

my $pid = fork();
die "fork failed: $!" unless defined $pid;
if ($pid == 0) {
    # child process
    exit(0);
} else {
    # parent process, $pid is child's PID
    waitpid($pid, 0);
}

See Also

PP runtime: fork | exec, wait, waitpid, exit

getc

Reads a single character from a filehandle.

Returns the next character from the specified filehandle as a one-character string. When called without arguments, reads from STDIN (filehandle id 0). Returns undef at end-of-file or on error.

Accepts both lexical ($fh) and bareword (FH) filehandles.

Synopsis

$ch = getc($fh);   # one character from $fh
$ch = getc();      # one character from STDIN

See Also

PP runtime: getc | readline, read, eof

glob

Expands shell-style filename patterns into matching pathnames.

Expands the given pattern using shell globbing rules. Supports *, ?, [charset], {alt1,alt2} (brace expansion), and ~ (home directory). In list context returns all matches; in scalar context returns the next match (iterating on successive calls with the same pattern).

The <PATTERN> syntax is equivalent to glob(PATTERN).

When File::Glob is loaded, delegates to bsd_glob semantics where whitespace in patterns is treated literally. Otherwise, the default behaviour splits the pattern on whitespace to process multiple globs.

Results are returned sorted and deduplicated.

Synopsis

@files = glob('*.pl');
@files = glob('lib/**/*.pm');
@files = <*.txt>;         # angle-bracket form

See Also

PP runtime: glob | opendir, readdir, File::Glob

gmtime

Converts a Unix timestamp to UTC (Greenwich Mean Time), returning the same structure as localtime but without timezone or DST adjustments.

Identical to localtime in interface, but all values are in UTC rather than the local timezone. The $isdst field is always 0. If called without an argument, uses the current time.

The implementation uses a pure-Rust UTC calculation (no libc dependency), correctly handling negative timestamps (dates before the epoch) and leap years.

Synopsis

my @t = gmtime(0);     # (0,0,0,1,0,70,4,0,0) - epoch in UTC
my $s = gmtime(0);     # "Thu Jan  1 00:00:00 1970"
my @t = gmtime();      # current time in UTC

See Also

PP runtime: gmtime | localtime, time, POSIX::strftime

hex

Interprets a hexadecimal string as an integer.

Accepts an optional 0x or 0X prefix. Underscores are allowed as digit separators. Leading and trailing whitespace is stripped. Returns 0 for empty or non-hex input.

Synopsis

$val = hex("ff");       # 255
$val = hex("0xDEAD");   # 57005
$val = hex("ff_ff");    # 65535

See Also

PP runtime: hex | oct

index

Finds the first occurrence of a substring within a string.

Searches STRING for the first occurrence of SUBSTR starting at character position START (default 0). Returns the zero-based character position of the match, or -1 if the substring is not found.

Positions are in characters (not bytes), so this works correctly with multi-byte UTF-8 strings. An ASCII fast path avoids the overhead of character-index conversion when both operands are pure ASCII.

When the OPpTARGET_MY flag (0x10) is set, the result is stored directly into the pad slot rather than pushed to the stack.

Synopsis

$pos = index($string, $substr);
$pos = index($string, $substr, $start);

Implementation Notes

perl5: index($str, $substr [, $position]).

See Also

PP runtime: index | rindex, substr, pos

int

Truncates a numeric value toward zero.

Returns the integer portion of EXPR by truncating toward zero. If EXPR is already an integer (sv_iok), returns it unchanged. For floats, converts via trunc() as i64. Always returns an Iv.

Synopsis

my $n = int(4.9);      # 4
my $n = int(-4.9);     # -4
my $n = int;           # int($_)

See Also

abs, POSIX::floor, POSIX::ceil

join

Concatenates a list of values into a single string with a separator.

Takes a separator string followed by a list of values (from a pushmark-delimited argument list on the stack). The separator is inserted between each adjacent pair of values. Array arguments are flattened. Returns the resulting string; an empty string when no list values are given.

Uses Cow<str> for the separator and parts to avoid allocation when the underlying SVs are already string-typed.

Synopsis

$str = join(',', @array);
$str = join(' ', 'hello', 'world');
$str = join('', @chars);           # no separator

Implementation Notes

perl5 (pp.c): mark-based. First arg is separator, rest are values.

See Also

PP runtime: join | split, sprintf

keys

Returns all keys of a hash, or the number of keys in scalar context.

In list context, pushes all keys of the hash onto the stack as individual string values. In scalar context, returns the number of key-value pairs. Accepts both %hash and $hashref (auto- dereferences). As a side effect, resets the hash’s internal iterator used by each.

Synopsis

my @k = keys %hash;
my $count = keys %hash;         # scalar context: number of keys
for my $key (keys %hash) { ... }

Implementation Notes

perl5: In scalar context, returns the number of keys. In list context, pushes all keys onto the stack.

See Also

PP runtime: keys | values, each, exists, delete

kill

Sends a signal to one or more processes or process groups.

The first argument is the signal, specified either as a name ('TERM', 'HUP', with or without SIG prefix) or as a number. The remaining arguments are PIDs to signal.

If the signal is negative, the absolute value is sent to the process group identified by each PID (using kill(-sig, -abs(pid))).

Signal 0 does not actually send a signal but checks whether the process exists and is reachable (permission check).

Returns the number of processes successfully signaled.

Synopsis

kill 'TERM', $pid;                # send SIGTERM by name
kill 15, $pid;                    # send SIGTERM by number
kill 'TERM', $pid1, $pid2;        # signal multiple processes
kill 0, $pid;                     # check if process exists
kill -15, $pgrp;                  # signal a process group

See Also

PP runtime: kill | %SIG, POSIX signals, fork, waitpid

lc

Converts a string to lowercase.

Returns a lowercased copy of STRING. For ASCII input, an in-place byte mutation fast path avoids allocation when a TARG pad slot is available. For Unicode input, Rust’s to_lowercase() is used, which handles case mappings that change byte length.

Synopsis

$lower = lc($string);
$lower = lc('HELLO');   # "hello"

Implementation Notes

perl5 reference: pp.c:4758 (lc path in pp_uc/pp_lc unified function). Non-UTF-8 locale path: use libc::tolower() per byte. perl5: pp.c:4818-4872.

See Also

PP runtime: lc | uc, ucfirst, lcfirst

lcfirst

Lowercases the first character of a string.

Returns a copy of STRING with the first character converted to lowercase and all other characters left unchanged. For ASCII first characters, a single-byte in-place mutation is used. For Unicode first characters, to_lowercase() may change the byte length.

Returns an empty string when given an empty string.

Synopsis

$result = lcfirst($string);
$result = lcfirst('HELLO');   # "hELLO"

Implementation Notes

perl5 reference: pp.c:4100 S_do_ucfirst (shared ucfirst/lcfirst function). Non-UTF-8 locale path: use libc::tolower on first byte. perl5: pp.c:4228-4233 – toLOWER_LC for first byte.

See Also

PP runtime: lcfirst | ucfirst, uc, lc

length

Returns the number of characters in a string.

Returns the character count (not byte count) for Unicode correctness. length(undef) returns undef, matching perl5 behavior.

Synopsis

$len = length($string);
$len = length("café");   # 4 (characters, not bytes)

See Also

PP runtime: length | substr

link

Creates a hard link to an existing file.

Creates a new directory entry NEWFILE that refers to the same inode as OLDFILE. Both paths must be on the same filesystem. The link count of the file is incremented. Uses std::fs::hard_link. Returns 1 on success, 0 on failure (and sets $!).

Synopsis

link $oldfile, $newfile or die "Cannot link: $!";
link '/data/original', '/data/hardlink';

See Also

PP runtime: link | symlink, unlink, rename

localtime

Converts a Unix timestamp to the local time zone, returning either a 9-element list or a human-readable string depending on context.

If called without an argument (or with undef), uses the current time. In list context, returns a 9-element list:

IndexFieldRange
0$sec0-60 (60 = leap)
1$min0-59
2$hour0-23
3$mday1-31
4$mon0-11 (Jan = 0)
5$yearyears since 1900
6$wday0-6 (Sun = 0)
7$yday0-365
8$isdstDST flag (-1, 0, 1)

In scalar context, returns a ctime-style string like "Thu Jan 1 00:00:00 1970".

Uses libc::localtime_r for accurate timezone/DST handling.

Synopsis

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    = localtime($time);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    = localtime();           # uses current time
my $str = localtime($time);  # "Thu Jan  1 00:00:00 1970"

Implementation Notes

perl5 (pp_sys.c:3945): List context: pushes 9 elements (sec,min,hour,mday,mon,year,wday,yday,isdst). Scalar context: pushes formatted string “Tue Feb 24 14:30:00 2026”.

See Also

PP runtime: localtime | gmtime, time, POSIX::strftime, POSIX::mktime

log

Returns the natural logarithm (base e) of its argument.

Returns the natural logarithm of EXPR, or $_ if omitted. The argument is coerced to Nv (f64). Returns -Inf for 0 and NaN for negative values. For other bases use log($x) / log($base). Always returns an Nv.

Synopsis

my $l = log(exp(1));   # 1
my $l = log($x);
my $l = log;           # log($_)

See Also

exp, sqrt

lstat

Returns file metadata without following symbolic links.

Identical to stat except that if the target is a symbolic link, lstat returns information about the link itself rather than the file it points to. Uses std::fs::symlink_metadata. Returns the same 13-element list as stat, or an empty list on failure.

Synopsis

my @s = lstat($symlink);
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
    $atime, $mtime, $ctime, $blksize, $blocks) = lstat($path);
lstat _;               # reuse last stat buffer

See Also

PP runtime: lstat | stat, readlink, symlink

m//

Tests a string against a regular expression pattern.

Matches the target string against the compiled regex pattern. The target is determined by context:

  • If targ > 0: reads from the pad slot (lexical $str =~ /pat/)
  • If STACKED flag is set: pops from the stack (expression =~ /pat/)
  • Otherwise: matches against $_ (bare /pat/)

In scalar context, returns true (1) or false (“”) indicating whether the pattern matched. Sets $1, $2, etc. for capture groups and updates pos() for /g matches.

In list context without /g, returns the list of captured substrings ($1, $2, …), or (1) if there are no captures.

In list context with /g, returns all matches: if the pattern has captures, returns all captured groups from all matches; otherwise returns all matched substrings.

Patterns are cached per-op for static regexes; dynamic patterns (from regcomp) use a HashMap-based cache.

Synopsis

if ($str =~ /pattern/)       { ... }
if ($str =~ /pattern/flags)  { ... }
my @captures = ($str =~ /(\w+)/g);
/bare pattern/;                        # matches against $_

Implementation Notes

perl5 pp_hot.c pp_match: handles scalar/list context, /g with MGLOB magic pos tracking, match variables ($1, $2, $&, $`, $’, $+).

See Also

PP runtime: m// | s///, split, qr//, perlre, perlop

mkdir

Creates a new directory with the specified permissions.

Creates a directory with the given name. The optional second argument specifies the permissions mode (default 0777), which is modified by the process umask before being applied. If no arguments are given, uses $_ as the directory name.

Returns 1 on success, 0 on failure. On failure, sets $! to the OS error code (e.g. EEXIST, EACCES).

Synopsis

mkdir("newdir", 0755) or die "Cannot mkdir: $!";
mkdir("newdir");         # default mode 0777 (modified by umask)
mkdir $_;                # uses $_ if no argument

See Also

PP runtime: mkdir | rmdir, chmod, umask

oct

Interprets a string as an octal number, with automatic prefix dispatch.

Dispatches on prefix: 0x/x for hexadecimal, 0b/b for binary, 0o for explicit octal (Perl 5.34+), otherwise plain octal. Underscores are allowed as digit separators. Leading whitespace is stripped.

Synopsis

$val = oct("77");       # 63
$val = oct("0xff");     # 255 (hex)
$val = oct("0b1010");   # 10  (binary)
$val = oct("0o77");     # 63  (explicit octal)

See Also

PP runtime: oct | hex

open

Opens a file, pipe, or duplicates a file descriptor, associating it with a filehandle.

Supports both the 2-argument form (mode embedded in the filename string) and the 3-argument form (separate mode and filename). The 3-argument form is preferred as it avoids shell metacharacter issues.

ModeMeaning
<Read
>Write (truncate)
>>Append
+<Read/write (existing file)
+>Write/read (truncate)
|-Pipe to command
-|Pipe from command
>& / <&Dup filehandle
>&= / <&=Dup by file descriptor number

In-memory I/O is supported via scalar references: open $fh, '>', \$buf.

Returns 1 on success, undef on failure. Sets $! on error.

Synopsis

open(my $fh, '<', $filename);       # read
open(my $fh, '>', $filename);       # write (truncate)
open(my $fh, '>>', $filename);      # append
open(my $fh, '+<', $filename);      # read/write
open(my $fh, '|-', @cmd);           # pipe to command
open(my $fh, '-|', @cmd);           # pipe from command
open(my $fh, '>&', $other_fh);      # dup for writing
open(my $fh, '<', \$scalar);        # in-memory I/O
open(FH, "<$filename");             # 2-arg form

Implementation Notes

perl5 (pp_sys.c:809): GV is mark+1, then mode/filename args.

See Also

PP runtime: open | close, binmode, print, read, sysopen, perlopentut

opendir

Opens a directory for reading via a directory handle.

Associates a directory handle with the named directory. The handle can then be used with readdir, rewinddir, seekdir, telldir, and closedir. All directory entries (including . and ..) are loaded eagerly into a Vec at open time.

The directory handle variable receives a numeric ID stored in the global DIRHANDLE_TABLE. Both lexical (my $dh) and bareword (DH) forms are supported.

Returns 1 on success, undef on failure.

Synopsis

opendir(my $dh, $dir) or die "Cannot open $dir: $!";
opendir(DH, "/tmp");

See Also

PP runtime: opendir | readdir, closedir, rewinddir, File::Find

ord

Returns the Unicode code point of the first character of a string.

Returns the code point of the first character. For an empty string, returns 0. Multi-character strings return only the first character’s code point.

Synopsis

$code = ord("A");       # 65
$code = ord("☺");      # 9786

See Also

PP runtime: ord | chr

pack

Converts a list of values into a binary string according to a template.

Takes a TEMPLATE string and a LIST of values and packs them into a binary string. The template is a sequence of format characters, each optionally followed by a repeat count or * (consume all remaining).

Common format characters:

  • a, A, Z – ASCII string (null-padded, space-padded, null-terminated)
  • c, C – signed/unsigned char (8-bit)
  • s, S – signed/unsigned short (16-bit native)
  • l, L – signed/unsigned long (32-bit native)
  • q, Q – signed/unsigned quad (64-bit native)
  • n, N – unsigned short/long in network (big-endian) byte order
  • v, V – unsigned short/long in VAX (little-endian) byte order
  • f, d – single/double-precision float (native)
  • H, h – hex string (high/low nybble first)
  • w – BER compressed integer
  • U – Unicode code point
  • x – null byte padding
  • X – back up one byte
  • @ – null-pad to absolute position
  • (...)N – group with repeat count

Whitespace and #-comments in templates are ignored.

Synopsis

my $bin = pack("A10", "hello");         # space-padded string
my $bin = pack("NnA*", $long, $short, $str);
my $bin = pack("(A2)*", @pairs);        # group repeat

Implementation Notes

perl5 reference: pp_pack.c:3175 PP_wrapped(pp_pack, 0, 1).

C structure: dSP; dMARK; dORIGMARK; dTARGET; SV *cat = TARG; // pad[op_targ] pat_sv = *++MARK; // template is first arg after mark MARK++; // advance past template SvPVCLEAR(cat); // clear cat, preserve UTF-8 off SvUTF8_off(cat); packlist(cat, pat, patend, MARK, SP + 1); SP = ORIGMARK; PUSHs(cat);

See Also

PP runtime: pack | unpack, vec

pipe

Creates a unidirectional pipe pair (read end + write end).

Calls the POSIX pipe(2) system call to create a connected pair of file descriptors. The first argument receives the read end and the second receives the write end, both as filehandles.

Typically used together with fork to set up inter-process communication. Returns 1 on success or undef on failure (with $! set to the system error).

Synopsis

pipe(my $read_fh, my $write_fh) or die "pipe: $!";

See Also

PP runtime: pipe | open, close

pop

Removes and returns the last element of an array.

Removes the last element from the array and returns it. If the array is empty, returns undef. The array shrinks by one element.

Synopsis

my $val = pop @array;
pop @array;              # discard removed element

Implementation Notes

perl5 (pp.c:6339): pop last element from array, return it.

See Also

PP runtime: pop | push, shift, unshift, splice

pos

Gets or sets the position of the last m//g match on a string.

As an rvalue, returns the character offset where the most recent global (/g) match left off, or undef if no match has been performed on that string. As an lvalue, sets the starting position for the next m//g match.

Without an argument, operates on $_. Positions are stored in a side table (pos_table) keyed by string content.

Synopsis

$str =~ /pattern/g;
my $p = pos($str);       # position after last /g match
pos($str) = 0;           # reset match position
my $p = pos;             # defaults to $_

Implementation Notes

perl5 pp.c pp_pos: reads MGLOB magic from target SV.

See Also

PP runtime: pos | m//g, reset

print

Outputs a list of values to a filehandle.

Pops values from the stack and writes them to the target filehandle. When the STACKED flag is set, the first value after the mark is the filehandle; otherwise, STDOUT is used.

The output field separator $, ($OFS) is inserted between values, and the output record separator $\ ($ORS) is appended after all values. For say (which shares this implementation), a newline is always appended after the output.

Supports Gv (typeglob), bareword string, and lexical (integer ID) filehandle forms. In-memory filehandles (open my $fh, '>', \$buf) write to the backing buffer.

Returns 1 on success (Perl convention).

Synopsis

print "hello world\n";
print STDERR "error message\n";
print $fh @data;
print @array;                # uses $, and $\

Implementation Notes

perl5 (pp_hot.c:1519): processes mark..sp, writes to filehandle. When OPf_STACKED, first value after mark is the filehandle.

See Also

PP runtime: print | say, printf, write, perlvar

printf

Formats and prints a list of values to a filehandle using a format string.

Equivalent to print FILEHANDLE sprintf(FORMAT, LIST). Formats the list of values according to FORMAT (see sprintf for format specifiers) and writes the result to the specified filehandle (or STDOUT if none is given).

Like print, honors $\ (output record separator), which is appended after the formatted output. The filehandle is determined by the stacked flag on the op, exactly as in pp_print.

Internally delegates to pp_sprintf for formatting, then writes the resulting string to the filehandle.

Synopsis

printf FORMAT, LIST;
printf FILEHANDLE FORMAT, LIST;

See Also

PP runtime: printf | sprintf, print, say

prototype

Returns the prototype string of a subroutine, or undef if none.

Accepts a code reference, a reference to a CV, or a string naming the subroutine. For CORE:: builtins, returns the well-known prototype from a built-in table (e.g. \@@ for push, ;$ for defined). For user-defined subs, looks up the CV in the stash and returns its prototype. Unqualified names are resolved in the current package. Returns undef when the subroutine has no prototype or cannot be found.

Synopsis

my $proto = prototype(\&mysub);         # "$$"
my $proto = prototype('CORE::push');    # "\@@"
my $proto = prototype('main::func');    # look up by name

Implementation Notes

perl5 pp.c:pp_prototype.

See Also

PP runtime: prototype | sub, CORE::

push

Appends one or more values to the end of an array, growing it as needed.

Treats the first argument as an array and appends all subsequent arguments to it. Array arguments are flattened (their elements are pushed individually, not as nested arrays). Returns the new number of elements in the array.

Synopsis

push @array, $value;
push @array, @more_values;
push @array, $v1, $v2, $v3;
my $new_len = push @array, $value;

Implementation Notes

perl5 (pp.c:6351): mark-based. First arg after mark is the array, rest are values to push. Returns new array length.

See Also

PP runtime: push | pop, unshift, shift, splice

qr

Creates a precompiled regular expression object.

Compiles the pattern (from OpData::Pm or a runtime-interpolated string in runtime_regex_pattern) into a regex and wraps it as a blessed reference in the Regexp class. The stringified form is (?^flags:pattern), matching perl5 behaviour.

The returned object can be used directly in pattern matches, interpolated into other patterns, or stored in variables, arrays, and hashes for later use.

Synopsis

my $re = qr/pattern/flags;
$str =~ $re;             # use in match
$str =~ /${re}suffix/;   # interpolate into pattern

Implementation Notes

perl5 pp_ctl.c pp_qr: creates an RV pointing to a regex SV, blessed into “Regexp”.

See Also

PP runtime: qr | m//, s///, perlre, perlop

quotemeta

Escapes all non-“word” characters with a preceding backslash.

Returns a version of the string where every non-word character ([^A-Za-z0-9_]) is preceded by \. Useful for interpolating literal strings into regular expressions. If all characters are already word characters, the input is returned unchanged with no allocation.

Synopsis

my $safe = quotemeta($string);
$string =~ /\Q$pattern\E/;     # \Q invokes quotemeta

See Also

PP runtime: quotemeta | m//, s///

rand

Returns a pseudo-random floating-point number.

Returns a pseudo-random floating-point number greater than or equal to 0 and less than EXPR (or 1 if omitted). If EXPR evaluates to zero, 1.0 is used instead. Uses the interpreter’s StdRng state, which can be seeded via srand. Not suitable for cryptographic purposes.

Synopsis

my $r = rand;          # 0 <= $r < 1
my $r = rand(10);      # 0 <= $r < 10
my $r = rand($n);

See Also

PP runtime: rand | srand, int

readdir

Reads entries from an open directory handle.

In scalar context, returns the next directory entry name as a string, or undef when the directory is exhausted. In list context, returns all remaining entries as a list of strings.

Entry names are bare filenames (no path prefix). The entries . and .. are included, matching perl5 behavior.

Synopsis

my $entry = readdir($dh);      # scalar: one entry
my @entries = readdir($dh);     # list: all remaining entries

See Also

PP runtime: readdir | opendir, closedir, rewinddir, glob

readline

PP function for readline

Reads one or more records from a filehandle.

In scalar context, returns the next record from the filehandle (including the record separator, typically a newline). Returns undef at end-of-file. In list context, returns all remaining records.

The record separator is controlled by $/:

  • "\n" (default) – line-at-a-time mode
  • "" – paragraph mode (reads until a blank line)
  • undef – slurp mode (reads the entire file)
  • \N (reference to integer) – fixed-length record mode (reads N bytes)
  • arbitrary string – reads until that separator is found

When the STACKED flag is set, the result is assigned to the lvalue target on the stack (e.g. my $line = <$fh>).

Supports both lexical ($fh) and bareword (FH) filehandles, as well as IO::Handle / IO::File objects.

Synopsis

$line  = <$fh>;           # scalar: next line
@lines = <$fh>;           # list: all remaining lines
$line  = readline($fh);   # explicit function form

Implementation Notes

perl5 (pp_hot.c pp_readline): pops GV from stack, reads from its IO. Supports DATA, user file handles, and STDIN.

See Also

PP runtime: readline | eof, read, getc, chomp

readlink

Returns the target of a symbolic link.

Returns the filename pointed to by the symbolic link EXPR, or $_ if omitted. Returns undef on error (e.g., not a symlink, file not found) and sets $!. Uses std::fs::read_link and converts the result to a string via to_string_lossy.

Synopsis

my $target = readlink $symlink;
my $target = readlink '/usr/bin/perl';
my $target = readlink;     # readlink($_)

See Also

PP runtime: readlink | symlink, lstat, stat

ref

Returns the reference type or blessed class name of a value.

Pops one value and pushes a string describing its reference type. For blessed references, returns the class name (e.g. "MyClass"). For unblessed references, returns the underlying type: "SCALAR", "ARRAY", "HASH", "CODE", "REF", "GLOB", or "Regexp". For non-references, returns the empty string "".

Delegates to Sv::ref_type() for the actual type determination.

Synopsis

my $type = ref $ref;           # "HASH", "ARRAY", "SCALAR", "CODE", etc.
my $class = ref $object;       # "MyClass" for blessed references
if (ref $x) { ... }            # true if $x is a reference

Implementation Notes

perl5 (pp.c pp_ref): If blessed, returns class name. Otherwise returns type name (SCALAR, ARRAY, HASH, CODE, REF, GLOB, etc).

See Also

PP runtime: ref | bless, Scalar::Util

rename

Renames or moves a file.

Renames the file OLDNAME to NEWNAME. If NEWNAME already exists, it is replaced atomically (on the same filesystem). Cannot move files across filesystem boundaries. Uses std::fs::rename. Returns 1 on success, 0 on failure (and sets $!).

Synopsis

rename $old, $new or die "Cannot rename: $!";
rename 'file.tmp', 'file.dat';

See Also

PP runtime: rename | unlink, link, File::Copy

require

Loads and executes a Perl file or module, caching it in %INC so that subsequent calls for the same module are no-ops.

Behaviour depends on the argument type:

  1. Version number (numeric or v-string): asserts that the Perl version is at least that value. pperl claims 5.42 compatibility, so any 5.x requirement succeeds; 6+ dies.

  2. Bareword / string: converts Foo::Bar to Foo/Bar.pm, then searches @INC for the file. If already present in %INC, the call is a no-op. Otherwise the file is compiled and executed in a sub-interpreter, subs are registered, and %INC is updated.

Dies with "Can't locate ..." if the file is not found. Returns 1 on success.

Synopsis

require 5.036;              # version check
require Carp;               # load Carp.pm via @INC
require "config.pl";        # load a file by path

Implementation Notes

perl5 (pp_ctl.c:4059):

  1. Get filename from stack
  2. Check %INC — skip if already loaded
  3. Search @INC for the file
  4. Compile and execute the file
  5. Record in %INC, push 1 on success

pperl p5: compile file via builder bridge, merge subs, execute.

See Also

PP runtime: require | use, do, eval, @INC, %INC

reset

Resets ?? (one-shot match) operators so they can match again.

The ?? operator matches only once between calls to reset. Without an argument, resets all one-shot matches in the current package. With a character-range argument, resets only those in variables whose names fall within the range.

This is a deprecated and rarely used feature. Returns 1.

Implementation status: Stub (no-op). PetaPerl does not currently track ?? match state.

Synopsis

reset;            # reset all ?? in current package
reset 'a-z';      # reset ?? in variables matching range

See Also

PP runtime: reset | m//g, pos

reverse

Reverses a list of values or the characters of a string, depending on context.

In list context, collects all items between the stack mark and the current position (flattening any intermediate arrays), reverses their order, and pushes them back. In scalar context, concatenates all items into a single string and reverses the characters. An ASCII fast path reverses bytes in-place; non-ASCII strings are reversed by Unicode code point.

Synopsis

my @rev = reverse @list;
my @rev = reverse 1, 2, 3;       # (3, 2, 1)
my $rev = reverse "Hello";        # "olleH"

See Also

PP runtime: reverse | sort

rewinddir

Resets the position of a directory handle back to the beginning.

Sets the current position of DIRHANDLE back to the beginning of the directory, so that subsequent readdir calls will re-read the directory from the start. The directory must have been previously opened with opendir.

Returns 1 on success, undef on failure.

Synopsis

rewinddir(DIRHANDLE);

See Also

PP runtime: rewinddir | opendir, readdir, closedir

rindex

Finds the last occurrence of a substring within a string.

Searches STRING for the last occurrence of SUBSTR, considering only matches that start at or before character position END (default: end of string). Returns the zero-based character position of the match, or -1 if the substring is not found.

For an empty SUBSTR, returns min(END, length(STRING)) to match perl5 behaviour.

Positions are in characters (not bytes), so this works correctly with multi-byte UTF-8 strings. An ASCII fast path uses rfind directly when both operands are pure ASCII.

When the OPpTARGET_MY flag (0x10) is set, the result is stored directly into the pad slot rather than pushed to the stack.

Synopsis

$pos = rindex($string, $substr);
$pos = rindex($string, $substr, $end);

See Also

PP runtime: rindex | index, substr, pos

rmdir

Removes an empty directory.

Deletes the named directory, which must be empty. If no argument is given, uses $_. Returns 1 on success, 0 on failure. On failure, sets $! to the OS error code (e.g. ENOTEMPTY, ENOENT, EACCES).

Only empty directories can be removed; use File::Path::rmtree for recursive deletion.

Synopsis

rmdir("olddir") or die "Cannot rmdir: $!";
rmdir $_;        # uses $_ if no argument

See Also

PP runtime: rmdir | mkdir, unlink, File::Path

s///

Searches a string for a pattern and replaces matched text.

Compiles the pattern from OpData::Pm, finds matches in the target string, performs the substitution, and updates the source variable in place. Pushes the number of successful substitutions to the stack (0 if no match, which is false in boolean context).

The target string is resolved from:

  • A PadSv op (lexical variable $str =~ s/.../.../)
  • A GvSv op (package variable)
  • A Sassign op (compound (my $x = $y) =~ s/.../.../)

Supports replacement templates with backreferences ($1, $&, etc.) and replacement ops for the /e flag (evaluate replacement as Perl code).

Flags: /g (global), /i (case-insensitive), /m (multiline), /s (single-line), /e (eval replacement), /r (non-destructive, returns modified copy).

Synopsis

$str =~ s/pattern/replacement/;
$str =~ s/pattern/replacement/g;     # global
$str =~ s/pattern/replacement/gi;    # global, case-insensitive
($copy = $orig) =~ s/old/new/g;      # compound assign+subst
s/pattern/replacement/;               # operates on $_

Implementation Notes

perl5 pp_hot.c pp_subst: full s/// with /g, captures, replacement template.

See Also

PP runtime: s/// | m//, tr///, perlre, perlop

say

Like print, but automatically appends a newline after the output.

Behaves identically to print except that a newline character is appended after all output (including any $\ record separator). Requires use feature 'say' or use v5.10 or later in perl5.

Internally delegates to pp_print, which detects OpCode::Say and appends the newline to the correct filehandle.

Synopsis

say "hello world";           # prints "hello world\n"
say STDERR "debug info";
say $fh @data;

See Also

PP runtime: say | print, printf

seek

Sets the read/write position in a filehandle.

Repositions the file pointer for the given filehandle. The WHENCE argument selects the reference point: 0 (SEEK_SET) for the beginning of the file, 1 (SEEK_CUR) for the current position, and 2 (SEEK_END) for the end of the file. Returns 1 on success, 0 on failure.

Also accepts IO::Handle / IO::File objects (blessed hashrefs with __fh_id).

Synopsis

seek($fh, $offset, 0);   # SEEK_SET — absolute position
seek($fh, $offset, 1);   # SEEK_CUR — relative to current
seek($fh, $offset, 2);   # SEEK_END — relative to end

See Also

PP runtime: seek | tell, sysseek, Fcntl

select

Sets the default output filehandle or performs I/O multiplexing.

1-argument form: Sets the default output filehandle (used by print and write when no explicit handle is given) and returns the previously selected handle. With no arguments, returns the current default output handle name without changing it.

4-argument form: Wraps the POSIX select(2) system call. Waits for I/O readiness on the file descriptors encoded in the bit vectors RBITS (readable), WBITS (writable), and EBITS (exceptional). TIMEOUT is in seconds (may be fractional). Returns the number of ready descriptors, or -1 on error.

Synopsis

$prev = select($fh);                                # 1-arg form
$nfound = select($rbits, $wbits, $ebits, $timeout); # 4-arg form

See Also

PP runtime: select | print, IO::Select, fileno

shift

Removes and returns the first element of an array, shifting all remaining elements down by one index.

Removes the first element from the array, shifts all remaining elements down by one, and returns the removed element. Returns undef if the array is empty.

When the SPECIAL flag is set (bare shift without an explicit array), the implicit target depends on call context: inside a subroutine it reads from @_ (pad slot 0); at file scope it reads from @ARGV. Handles both regular Av and AliasedAv (the typical representation of @_).

Synopsis

my $val = shift @array;
my $arg = shift;          # shifts from @_ inside a sub, @ARGV at top level

Implementation Notes

perl5 (pp.c:6378): OPf_SPECIAL → shift from default array. Inside a sub, default = @_. At top level, default = @ARGV.

See Also

PP runtime: shift | unshift, pop, push, splice

sin

Returns the sine of its argument (in radians).

Returns the sine of EXPR (interpreted as an angle in radians), or $_ if omitted. The argument is coerced to Nv (f64). Always returns an Nv.

Synopsis

my $s = sin(3.14159);  # ~0
my $s = sin($radians);
my $s = sin;           # sin($_)

See Also

cos, atan2

sleep

Suspends execution for the specified number of seconds.

Causes the process to sleep for the given number of whole seconds. Returns the number of seconds actually slept, which may be less than requested if a signal interrupted the sleep.

If Time::HiRes::sleep has been imported into the current package, fractional seconds are supported and the override is used instead. Time::HiRes::sleep dies on negative values.

Without arguments, sleeps indefinitely (until a signal arrives). Negative values are clamped to 0.

Uses libc::sleep() (not std::thread::sleep) so that signals like SIGALRM can interrupt the sleep. If interrupted, any pending $SIG{...} handler is dispatched synchronously before returning.

Synopsis

my $slept = sleep(5);    # pause 5 seconds, returns 5
sleep;                   # sleep forever (until signal)
# with: use Time::HiRes qw(sleep);
sleep(0.5);              # fractional seconds

See Also

PP runtime: sleep | alarm, Time::HiRes, select

sort

Sorts a list of values and returns them in sorted order.

Sorts the input list according to the comparison mode determined by the op’s private flags:

  • Default (string): Lexicographic comparison via cmp.
  • Numeric (OPpSORT_NUMERIC, 0x01): Floating-point comparison.
  • Custom block (OPpSORT_BLOCK, 0x20): A CV on the stack is used as the comparator. $a and $b are set in the current package via cached cells and the CV is executed using the multicall pattern (pad + call frame set up once, only $a/$b updates per comparison) with a merge-sort algorithm.
  • Descend (OPpSORT_DESCEND, 0x08): Reverses the final result.

Array arguments are flattened before sorting.

Synopsis

my @sorted = sort @list;                    # lexicographic
my @sorted = sort { $a <=> $b } @list;      # numeric
my @sorted = sort { $b cmp $a } @list;      # reverse lexicographic
my @sorted = sort \&compare, @list;          # named comparator

Implementation Notes

perl5 (pp_sort.c): OPpSORT_NUMERIC (0x01): use C-level numeric compare (detected at compile time) OPpSORT_DESCEND (0x08): reverse order OPpSORT_BLOCK (0x20): comparison sub on stack (custom comparator) Default (no flags): lexical string sort via sv_cmp

perl5 detects { $a <=> $b } at compile time (S_simplify_sort in op.c), sets OPpSORT_NUMERIC, and deletes the comparison block entirely. The sort then uses a C-level SvIV compare — zero sub calls.

See Also

PP runtime: sort | reverse, map, grep

splice

Removes and/or replaces elements in an array, returning the removed elements.

Removes $length elements starting at $offset from @array, optionally replacing them with @replacements, and returns the removed elements. Negative $offset counts from the end of the array. If $length is omitted, removes everything from $offset onward. A negative $length leaves that many elements at the end. Replacement arrays are flattened. The array is rebuilt in place.

Synopsis

my @removed = splice @array, $offset;
my @removed = splice @array, $offset, $length;
my @removed = splice @array, $offset, $length, @replacements;
splice @array, 0, 0, @prepend;    # insert at beginning

Implementation Notes

perl5 (pp.c:6107): Removes/replaces elements from an array. Stack: [mark] array offset? length? replacement_list… Returns removed elements (list ctx) or last removed (scalar ctx).

See Also

PP runtime: splice | push, pop, shift, unshift, delete

split

Splits a string into a list of substrings using a pattern.

Splits EXPR (or $_ if omitted) into substrings at each match of PATTERN. If LIMIT is positive, returns at most LIMIT fields. If LIMIT is negative, behaves as if arbitrarily large. If LIMIT is zero or omitted (default), trailing empty fields are stripped.

Special case: split ' ' (with pmflags & 0x800) uses awk-style semantics – splits on runs of whitespace and ignores leading whitespace. An empty pattern (split //, $str) splits every character.

If a split_targ is set in the op, results are stored directly into the target array (pad slot) instead of the stack. In scalar context, returns the number of fields.

Synopsis

@fields = split /PATTERN/, EXPR, LIMIT;
@fields = split /PATTERN/, EXPR;
@fields = split /PATTERN/;           # splits $_
@fields = split ' ', $string;        # awk-style split
my $count = split /,/, $csv;         # scalar context: count

Implementation Notes

perl5 (pp.c pp_split): complex. Handles regex patterns, literal strings. Minimal: split on literal single-char or string separator.

See Also

PP runtime: split | join, m//g, perlre

sprintf

Formats a string according to a printf-style format specification.

Takes a format string followed by a list of arguments (from a pushmark-delimited argument list on the stack) and returns the formatted result string. Supports the standard C-style conversion specifiers:

  • %s string, %d/%i signed decimal, %u unsigned decimal
  • %o octal, %x/%X hex, %b/%B binary
  • %e/%E scientific, %f/%F fixed, %g/%G general float
  • %c character (by codepoint), %p pointer, %% literal percent

Flags: - (left-justify), + (force sign), (space for sign), 0 (zero-pad), # (alternate form). Width and precision may be literal or * (consumed from the argument list). Indexed arguments via %N$ are supported. The %vd vector flag formats a string as dot-separated byte values (e.g. version strings).

Uses a zero-allocation fast path: arguments are peeked from the stack as &[Sv] without cloning, and integer formatting uses stack buffers.

Synopsis

$s = sprintf('%d items at $%.2f each', $count, $price);
$s = sprintf('%05x', $hex_val);
$s = sprintf('%-20s', $name);

Implementation Notes

perl5 (pp.c:3166): Reads format string and args from the stack (mark-based). Processes % directives, producing a formatted string.

We handle the directives needed by benchlib.pl: %d, %s, %f, %g, %e, %x, %o, %%, %.*f, %04d, %02d, %.0f, %.1f, %.5g

See Also

PP runtime: sprintf | printf, join

sqrt

Returns the square root of its argument.

Returns the positive square root of EXPR, or $_ if omitted. The argument is coerced to Nv (f64) before the operation. Returns NaN for negative arguments.

Synopsis

my $r = sqrt(144);     # 12
my $r = sqrt($x);
my $r = sqrt;          # sqrt($_)

See Also

abs, exp, log

srand

Seeds the pseudo-random number generator used by rand.

Sets the seed for the random number generator used by rand. If EXPR is provided and defined, its integer value is used. Otherwise, a system-derived seed (nanosecond timestamp XOR PID) is used. Creates a new StdRng instance via seed_from_u64. Returns the seed that was actually used as an integer.

Synopsis

srand(42);             # deterministic sequence
srand;                 # seed from system entropy

See Also

PP runtime: srand | rand

stat

Returns file metadata as a 13-element list.

Returns a 13-element list giving the status info for a file. The elements are: dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, blocks. Follows symbolic links (use lstat to stat the link itself). Returns an empty list on failure.

When Time::HiRes::stat has been imported into the caller’s namespace, the atime/mtime/ctime fields are returned as floating-point seconds with nanosecond precision.

Synopsis

my @s = stat($file);
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
    $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
stat $fh;                  # stat on a filehandle
stat _;                    # reuse last stat buffer

Implementation Notes

stat(EXPR). perl5 pp_sys.c:3053.

Scalar context: push success bool. List context: push 13 stat fields.

See Also

PP runtime: stat | lstat, chmod, chown

stringify

Converts a value to its string representation, respecting overload.

If the value is already a string, passes it through unchanged. If it is a blessed reference with a "" (stringify) overload handler, invokes that handler. Otherwise converts via default stringification (Sv::to_str_cow).

Synopsis

my $s = "$value";    # stringify via interpolation

See Also

PP runtime: stringify | ref, overload

substr

and 3-argument forms)

Extracts a substring from a string.

Returns the portion of STRING starting at character position OFFSET. If OFFSET is negative, it counts backwards from the end of the string. When LENGTH is given, at most that many characters are extracted; a negative LENGTH leaves that many characters at the end.

The argument count is encoded in the lower 4 bits of the op’s private flags (set by codegen). When those bits are zero (e.g. bytecode/JSON fallback path), the stack length is used instead.

Synopsis

$part = substr($string, $offset);
$part = substr($string, $offset, $length);

Implementation Notes

perl5 (pp.c pp_substr): 2, 3, or 4 args. We handle the common 2-arg (pos to end) and 3-arg (pos, len) cases.

See Also

PP runtime: substr | index, rindex, substr

symlink

Creates a symbolic (soft) link.

Creates a symbolic link LINKNAME that points to TARGET. Unlike hard links, symbolic links can span filesystems and can point to directories. Uses std::os::unix::fs::symlink. Returns 1 on success, 0 on failure (and sets $!).

Synopsis

symlink $target, $linkname or die "Cannot symlink: $!";
symlink '/usr/bin/perl', '/usr/local/bin/perl';

See Also

PP runtime: symlink | link, readlink, unlink

sysopen

Opens a file using POSIX open(2) flags for precise mode control.

Unlike the high-level open, sysopen takes explicit POSIX mode flags (from Fcntl) and an optional permissions mask. The flags are passed directly to the underlying open(2) system call.

Arguments on the stack (via mark): filehandle target, filename, mode flags, and optional permissions (defaults to 0666, modified by umask).

Returns 1 on success (the filehandle is stored in the first argument) or undef on failure.

Synopsis

use Fcntl;
sysopen(my $fh, $filename, O_RDWR | O_CREAT, 0644);
sysopen(FH, $path, O_WRONLY | O_TRUNC);

See Also

PP runtime: sysopen | open, close, Fcntl

sysread

Performs an unbuffered read directly from the OS file descriptor.

Reads up to LENGTH bytes from the filehandle into BUFFER using the underlying read(2) system call, bypassing Perl’s buffered I/O layer. When OFFSET is given, data is placed starting at that byte position within the buffer.

Returns the number of bytes actually read (which may be less than LENGTH), 0 at end-of-file, or undef on error (with $! set to the OS errno).

Warning: Do not mix sysread with buffered I/O (read, readline, <>) on the same filehandle.

Synopsis

$n = sysread($fh, $buf, $length);
$n = sysread($fh, $buf, $length, $offset);

See Also

PP runtime: sysread | read, syswrite, sysseek

sysseek

Repositions the OS file pointer, bypassing Perl’s buffered I/O layer.

Calls lseek(2) on the underlying file descriptor to reposition the read/write offset. Unlike seek, this operates at the system-call level and does not interact with Perl’s I/O buffering.

Returns the resulting absolute position (as a “0 but true” string when the position is zero, per perl5 convention), or undef on error (with $! set).

Warning: Do not mix sysseek with buffered I/O (seek, tell) on the same filehandle.

Synopsis

$pos = sysseek($fh, $offset, SEEK_SET);  # absolute
$pos = sysseek($fh, 0, SEEK_CUR);        # query position
$pos = sysseek($fh, 0, SEEK_END);        # query file size

See Also

PP runtime: sysseek | seek, tell, sysread, syswrite

system

Executes an external command and waits for it to complete.

In the single-argument form, the command string is passed to /bin/sh -c for shell interpretation (pipes, redirects, etc.). In the multi-argument form (list form), the program is executed directly via execvp without a shell, avoiding shell metacharacter interpretation.

Returns the full wait status (same encoding as $?):

  • 0 on success (exit code 0)
  • $? >> 8 extracts the exit code
  • Lower 8 bits contain signal information
  • -1 on failure to execute

The child process inherits Perl’s %ENV (not the OS environment).

Synopsis

my $status = system("ls -la");
my $status = system("program", "arg1", "arg2");
system("echo hello") == 0 or die "system failed: $?";

See Also

PP runtime: system | exec, fork, waitpid, backticks, $?

syswrite

Performs an unbuffered write directly to the OS file descriptor.

Writes data from SCALAR to the filehandle using the underlying write(2) system call, bypassing Perl’s buffered I/O layer. When LENGTH is specified, at most that many bytes are written. When OFFSET is given, writing starts from that byte position within the scalar.

Returns the number of bytes actually written, or undef on error (with $! set to the OS errno).

Warning: Do not mix syswrite with buffered I/O (print, say) on the same filehandle.

Synopsis

$n = syswrite($fh, $scalar);
$n = syswrite($fh, $scalar, $length);
$n = syswrite($fh, $scalar, $length, $offset);

See Also

PP runtime: syswrite | sysread, sysseek, print

tell

Returns the current byte position in a filehandle.

Returns the current read/write position (in bytes) for the given filehandle, or for the most recently read filehandle when called without arguments. Returns -1 on error or when no filehandle is available.

The returned value is suitable as the POSITION argument to seek.

Synopsis

$pos = tell($fh);    # position in $fh
$pos = tell();       # position in last-read filehandle

See Also

PP runtime: tell | seek, eof, sysseek

tie

Binds a variable to an object class, enabling magical access through method dispatch.

Associates a variable with a class by calling the class’s TIEHASH, TIEARRAY, TIESCALAR, or TIEHANDLE constructor. After tying, all accesses to the variable are redirected through FETCH/STORE/etc. methods on the tie object. Returns the tie object on success.

Implementation status: Stub. Returns undef (simulating a graceful failure) because full tie support requires deep integration with the value layer to intercept variable access.

Synopsis

tie my %hash, 'DB_File', 'filename';
tie my @array, 'Tie::File', 'data.txt';
tie my $scalar, 'Tie::StdScalar';
my $obj = tie %hash, 'MyTieClass', @args;

See Also

PP runtime: tie | tied, untie

tied

Returns the underlying object of a tied variable.

Returns the object that was associated with the variable by a previous tie call. Returns undef if the variable is not tied. Commonly used to access the tie object’s methods directly.

Implementation status: Stub. Always returns undef because tie is not yet implemented.

Synopsis

my $obj = tied %hash;
if (tied $scalar) { ... }   # check if variable is tied

See Also

PP runtime: tied | tie, untie

time

Returns the number of seconds since the Unix epoch.

Returns the current time as a non-negative integer representing whole seconds elapsed since 1970-01-01 00:00:00 UTC (the Unix epoch).

If Time::HiRes::time has been imported into the current package (overriding the builtin), this op dispatches to the high-resolution version which returns a floating-point value with microsecond precision. The CORE::time form (SPECIAL flag set) always uses the integer builtin, bypassing any override.

Synopsis

my $now = time();             # e.g. 1704067200
my $core = CORE::time;        # always integer, bypasses overrides
# with: use Time::HiRes qw(time);
my $hires = time();           # e.g. 1704067200.123456

Implementation Notes

perl5 (pp_sys.c:3877): when = time(NULL); XPUSHi(when); RETURN;

Returns current Unix epoch seconds as IV. Zero dereferences, one syscall.

See Also

PP runtime: time | localtime, gmtime, Time::HiRes

tr

Performs character-by-character transliteration on a string.

Transliterates characters in the target string according to a precompiled translation table. Each character found in the search list is replaced by the corresponding character in the replacement list.

The target string is obtained from the targ pad slot, the stack, or $_. For tr/// (OP_TRANS), the string is modified in place and the count of translated characters is pushed. For tr///r (OP_TRANSR), a new modified copy is pushed instead.

Modifiers (encoded in op_private):

  • /c (0x20): Complement the search list
  • /d (0x80): Delete found but unreplaced characters
  • /s (0x08): Squash duplicate replaced characters

Synopsis

$str =~ tr/searchlist/replacementlist/;
$str =~ y/a-z/A-Z/;          # y is a synonym
my $count = ($s =~ tr/a//);   # count occurrences
my $new   = $s =~ tr/a-z/A-Z/r;  # /r returns copy

See Also

PP runtime: tr | s///, m//, perlop

truncate

Truncates a file to a specified length.

Truncates the file identified by the first argument to at most LENGTH bytes. The first argument may be either an open filehandle or a filename string. When given a filename, the file is opened for writing just long enough to perform the truncation.

Returns 1 on success, undef on failure (e.g. permissions, invalid filehandle).

Synopsis

truncate($fh, $length);          # by open filehandle
truncate('/path/to/file', $len);  # by filename

See Also

PP runtime: truncate | open, seek, tell

uc

Converts a string to uppercase.

Returns an uppercased copy of STRING. For ASCII input, an in-place byte mutation fast path avoids allocation when a TARG pad slot is available. For Unicode input, Rust’s to_uppercase() is used, which handles case mappings that change byte length (e.g. the German sharp-s \u{00DF} becomes "SS").

Synopsis

$upper = uc($string);
$upper = uc('hello');   # "HELLO"

Implementation Notes

perl5 reference: pp.c:4441 PP_wrapped(pp_uc, 1, 0). Non-UTF-8 path: when IN_LC_RUNTIME(LC_CTYPE), use libc::toupper() for each byte (perl5: pp.c:4591 for (s < send) *d = toUPPER_LC(*s)). UTF-8 path: deferred – for now uses Unicode case folding. perl5: pp.c:4583-4619.

See Also

PP runtime: uc | lc, ucfirst, lcfirst

ucfirst

Uppercases the first character of a string.

Returns a copy of STRING with the first character converted to uppercase and all other characters left unchanged. For ASCII first characters, a single-byte in-place mutation is used. For Unicode first characters, to_uppercase() may produce multiple characters (e.g. the ffi ligature U+FB03).

Returns an empty string when given an empty string.

Synopsis

$result = ucfirst($string);
$result = ucfirst('hello');   # "Hello"

Implementation Notes

perl5 reference: pp.c:4100 S_do_ucfirst (shared ucfirst/lcfirst function). Non-UTF-8 locale path: use libc::toupper on first byte. perl5: pp.c:4228-4243 – toLOWER_LC / toUPPER_LC for first byte.

See Also

PP runtime: ucfirst | lcfirst, uc, lc

umask

Gets or sets the process file-creation permission mask.

With an argument, sets the umask to the given value and returns the previous mask. Without an argument (or with undef), queries the current mask by temporarily setting it to 0 and restoring it, then returns the value.

The umask affects the default permissions of newly created files and directories. The actual permissions are requested & ~umask.

Returns the previous umask as an integer (typically displayed in octal).

Synopsis

my $old_mask = umask(0022);  # Set new mask, return old
my $current  = umask();      # Query current mask

See Also

PP runtime: umask | chmod, mkdir, open

undef

Produces the undefined value, or undefines an existing variable.

Operates in three modes determined by op flags:

  • MOD (lvalue placeholder): Used in list assignment patterns like my (undef, $x) = @list. Adds a sentinel to lvalue_targets so pp_aassign skips that position.
  • STACKED (function call undef $x): Pops the target from the stack and undefines it. For arrays and hashes this clears the container; for hash/array element targets it sets the element to undef; for scalars it writes undef through the alias.
  • Default: If targ is set, sets the corresponding pad slot to undef. Always pushes undef onto the stack as the return value.

Synopsis

my $x = undef;
undef $x;                    # set $x to undef
undef @array;                # clear the array
undef %hash;                 # clear the hash
my (undef, @rest) = @list;   # skip first element in list assignment

See Also

PP runtime: undef | defined

unlink

Deletes one or more files from the filesystem.

Deletes a list of files. If called with no arguments (empty list after pushmark, or no mark at all), defaults to $_. Returns the number of files successfully deleted. Does not delete directories (use rmdir for that). Uses std::fs::remove_file.

Synopsis

unlink $file;
my $count = unlink @files;
unlink or die "Cannot unlink $_: $!";   # uses $_
unlink glob("*.bak");

Implementation Notes

perl5: pp_sys.c via apply(). Iterates stack args, unlink(2) each, returns count.

See Also

PP runtime: unlink | rename, link, rmdir

unpack

Extracts values from a binary string according to a template.

The inverse of pack: takes a TEMPLATE and a binary STRING (or $_ if omitted) and returns a list of values extracted according to the template format characters. The template syntax is the same as for pack, including group syntax (...)N, repeat counts, and *.

String data is decoded using Latin-1 encoding (each byte maps 1:1 to a char code point), which correctly round-trips with pack’s output. The U format uses character offsets instead of byte offsets.

Synopsis

my @vals = unpack("A10", $data);
my ($long, $short, $str) = unpack("NnA*", $packet);
my @chars = unpack("C*", $bytes);
my @vals = unpack("A5", $_);   # $_ when string omitted

Implementation Notes

perl5 reference: pp_pack.c:1881 PP_wrapped(pp_unpack, 2, 0).

C structure: dPOPPOPssrl; // pop right (string), pop left (template) GIMME_V == G_SCALAR // scalar context flag unpackstring(pat, patend, s, strend, flags) if (!cnt && G_SCALAR) PUSHs(&PL_sv_undef)

See Also

PP runtime: unpack | pack, vec, ord

unshift

Prepends one or more values to the beginning of an array.

Prepends the listed values to the front of the array, preserving argument order: unshift(@a, 1, 2, 3) yields (1, 2, 3, @original). Array arguments are flattened. Returns the new number of elements. Internally, values are inserted in reverse order to achieve the correct final ordering.

Synopsis

unshift @array, $value;
unshift @array, $v1, $v2, $v3;
unshift @array, @more_values;
my $new_len = unshift @array, $value;

Implementation Notes

perl5 (pp.c:6436): prepend values to the beginning of an array. Returns new array length.

See Also

PP runtime: unshift | shift, push, pop, splice

untie

Removes the tie binding from a variable, restoring normal access.

Breaks the association between a variable and its tie class established by tie. Subsequent accesses to the variable bypass the FETCH/STORE methods and operate on the variable directly. Returns true on success.

Implementation status: Stub. Always returns 1 (success) since tie is not yet implemented.

Synopsis

untie %hash;
untie @array;
untie $scalar;

See Also

PP runtime: untie | tie, tied

utime

Sets the access and modification timestamps of one or more files.

Takes an access time, a modification time (both as Unix epoch seconds, or undef for “now”), followed by a list of file paths. Each file’s timestamps are updated via the POSIX utimes(2) call. When Time::HiRes::utime is available, nanosecond precision is used via utimensat(2).

Passing undef for either timestamp sets it to the current time.

Returns the number of files successfully modified.

Synopsis

utime $atime, $mtime, @files;
utime undef, undef, @files;    # set to current time

See Also

PP runtime: utime | stat, lstat

values

Returns all values of a hash, or the number of values in scalar context.

In list context, pushes all values of the hash onto the stack. In scalar context, returns the number of key-value pairs (same as keys in scalar context). Accepts both %hash and $hashref. Resets the hash’s internal iterator as a side effect.

When the REF flag is set (foreach aliasing context from the native parser), values are pushed onto the stack and the hash plus its keys are recorded for write-back of $_ modifications during iteration.

Synopsis

my @v = values %hash;
my $count = values %hash;       # scalar context: number of values
for my $val (values %hash) { ... }

Implementation Notes

perl5: In scalar context, returns number of values. In list context, pushes all values.

See Also

PP runtime: values | keys, each, exists, delete

vec

Treats a string as a bit vector and accesses individual bitfields.

Treats EXPR as a bit vector made up of elements of width BITS and returns the unsigned integer value of the element at OFFSET. BITS must be a power of 2 from 1 to 32.

As an lvalue, vec stores values into the specified bitfield, growing the string as needed.

my $flags = "";
vec($flags, 0, 8) = 255;     # Set byte 0 to 255
vec($flags, 1, 8) = 10;      # Set byte 1 to 10
my $val = vec($flags, 0, 8); # Get byte 0 (255)
my $bit = vec($flags, 5, 1); # Get bit 5 of byte 0

Synopsis

my $val = vec($string, $offset, $bits);
vec($string, $offset, $bits) = $value;

See Also

PP runtime: vec | pack, unpack

wait

Waits for any child process to terminate.

Suspends the current process until any child process terminates. Returns the PID of the deceased child, or -1 if there are no child processes. Sets $? to the wait status of the child (encoding exit code and signal information).

Synopsis

my $pid = wait();
print "Child $pid exited with status $?\n";

See Also

PP runtime: wait | waitpid, fork, $?

waitpid

Waits for a specific child process to change state.

Suspends the current process until the child identified by $pid terminates (or changes state, depending on flags). Sets $? to the wait status.

Returns:

  • The PID of the terminated child on success
  • 0 if WNOHANG was specified and no child has exited yet
  • -1 on error (e.g. no such child)

When $pid is -1, waits for any child (equivalent to wait()).

Synopsis

my $result = waitpid($pid, 0);        # blocking wait
my $result = waitpid($pid, WNOHANG);  # non-blocking
my $result = waitpid(-1, 0);           # any child (like wait)

See Also

PP runtime: waitpid | wait, fork, POSIX

wantarray

wantarray().

Implementation Notes

perl5: returns true in list context, false in scalar, undef in void. Since we don’t track context fully yet, return false (scalar) by default.

Implementation Notes

perl5: returns true in list context, false in scalar, undef in void. Since we don’t track context fully yet, return false (scalar) by default.

warn

Issues a warning message to STDERR without terminating the program.

Prints the argument to STDERR. If the string does not end with a newline, Perl appends at FILE line LINE.

If $SIG{__WARN__} is set to a code reference, that handler is called with the warning message as its sole argument instead of printing to STDERR. The handler runs in void context and its return value is discarded.

Synopsis

warn "something looks wrong";
warn "problem at $file line $line\n";
warn $object;

Implementation Notes

perl5 (pp_sys.c:537): mark-based list op. If SP - MARK > 1, joins all args with “” via do_join into TARG. If SP == MARK (no args), uses “”. Single arg: uses TOPs directly. Then mess_sv for location if no trailing \n, write to stderr.

See Also

PP runtime: warn | die, Carp

x

Repeats a string a given number of times.

Pops the repeat count (right operand) and the string (left operand) from the stack. Returns a new string consisting of the left operand concatenated with itself COUNT times. A count of zero or less produces an empty string. Inf and NaN counts are treated as zero (matching perl5).

Synopsis

$line = '-' x 80;          # 80 dashes
$pad  = ' ' x $width;
$s    = 'ab' x 3;          # "ababab"

See Also

PP runtime: x | x, perlop

Native Modules (P5 Runtime)

pperl P5 runtime native (Rust) module implementations.

B

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Config

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

clear

croak “Config is read-only”.

config_sh

return all config as shell variable assignments.

Uses core::config_sh_bytes() for formatting.

config_vars

print config values in key=‘value’; format.

Uses core::format_config_var() for formatting.

delete

croak “Config is read-only”.

exists

tied hash exists check.

fetch

tied hash fetch. Look up $key in %Config::Config.

firstkey

reset iterator and return first key.

import

Export %Config into the caller’s namespace.

The hash is populated by p5_register_config_hash() at require-time.

myconfig

return a summary string of the config.

Uses core::MYCONFIG_SUMMARY for the content.

nextkey

return next key from iterator.

store

croak “Config is read-only”.

Cwd

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

abs_path

canonicalize path resolving symlinks

cwd

return current working directory

import

export cwd/getcwd/abs_path/realpath into the caller’s package.

Mirrors Exporter: Cwd has @EXPORT = qw(cwd getcwd fastcwd fastgetcwd). @EXPORT_OK includes abs_path/realpath/fast_abs_path. For simplicity we always export all functions (matches common usage).

Data::Dumper

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

preamble

Digest::MD5

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

add

append data to context, return $self.

add_bits

add bit string to context.

addfile

read all data from filehandle, add to context.

b64digest

return base64 digest (22 chars, no padding), reset context.

clone

duplicate context, preserving class.

context

get mode: return ($block_count, $state[, $unprocessed]).

$ctx->context($block_count, $state[, $unprocessed]) — set mode: restore state.

digest

return binary digest (16 bytes), reset context.

hexdigest

return hex digest (32 chars), reset context.

md5

binary 16-byte digest.

md5_base64

22-char base64 digest (no padding).

md5_hex

32-char hex digest.

new

reset

clear accumulated data, return $self.

Digest::SHA

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

add

add_bits

addfile

not fully implemented, returns self.

algorithm

return algorithm number.

b64digest

base64 digest (no padding), resets context.

clone

clone the context.

digest

binary digest, resets context.

dump

getstate

serialize state to Perl-compatible string.

hashsize

return hash size in bits.

hexdigest

hex digest, resets context.

load

new

putstate

reset

Encode

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

decode

Decode byte string from the given encoding to Unicode string.

Mirrors Encode::decode() from Encode.xs. Input bytes without SVF_UTF8 → output string with SVF_UTF8.

decode_utf8

Shorthand for decode(‘UTF-8’, $bytes). Returns character string (SVF_UTF8 set).

encode

Encode Unicode string to byte string in the given encoding.

Mirrors Encode::encode() from Encode.xs. Input string with SVF_UTF8 → output bytes without SVF_UTF8.

encode_utf8

Shorthand for encode(‘UTF-8’, $string). Returns byte string (no SVF_UTF8).

encodings

Returns list of available encoding names.

fb_croak

1

fb_default

fb_htmlcref

0x0208 = 520

fb_perlqq

0x0108 = 264

fb_quiet

4

fb_warn

6

fb_xmlcref

0x0408 = 1032

find_encoding

Returns a blessed hashref {Name => $canonical} or undef if unknown.

perl5: Encode::find_encoding returns an Encode::XS or Encode::utf8 object. We return a blessed HV with a “Name” key, mirroring perl5’s object.

from_to

Convert $octets in-place from $from encoding to $to encoding. Returns length of the converted string, or undef on failure.

perl5: Encode::from_to in Encode.pm calls encode/decode. In the p5 runtime we modify the SV’s PV buffer in-place.

import

called by use Encode qw(...).

No-op in p5 runtime: the builder resolves imported names at compile time.

is_utf8

Returns true if the SV has the SVF_UTF8 flag set.

perl5: Encode.xs:pp_utf8_on checks SvUTF8(sv). This checks the actual flag, not the string content.

leave_src

8

preamble

resolve_alias

Returns the canonical encoding name, or undef if unknown.

utf8_off

Clear SVF_UTF8 on the passed-in SV in-place.

perl5: Encode.xs XSUB _utf8_off: SvUTF8_off(sv).

utf8_on

Set SVF_UTF8 on the passed-in SV in-place.

perl5: Encode.xs XSUB _utf8_on: SvUTF8_on(sv).

Errno

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

exists

exists $!{ENOENT}: check if name is a known errno constant.

fetch

$!{ENOENT}: return true if current errno matches the named constant,

false otherwise. Mirrors perl5 Errno.pm: sub FETCH { $!+0 == $Errno{$_[1]} }.

firstkey

return the first errno constant name for hash iteration.

import

no-op stub. Constants are handled at compile time.

nextkey

return the next errno constant name after the given key.

tiehash

return the class name as a blessed ref stub.

Fcntl

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

autoload

stub, pushes undef for any unresolved constant.

import

no-op stub. Constants are handled at compile time.

File::Basename

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

basename

dirname

fileparse

Returns list: (name, dir, suffix)

fileparse_set_fstype

always “Unix” on Linux

File::Copy

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

copy

copy a file, return 1 on success, 0 on failure.

Supports filenames and filehandles for both $from and $to.

move

rename or copy+unlink, return 1 on success, 0 on failure.

File::Find

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

File::Glob

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

File::Path

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

make_path

Returns list of created directories (like perl5). In scalar context, returns the count.

remove_tree

File::Spec

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

File::Temp

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

dir_stringify

The invocant is a blessed PV ref — deref to get the PV SV, return its string value. perl5: use overload ‘“”’ => sub { ${$_[0]} }

filename

return path of a temp file object

new

OO constructor, returns IO SV

newdir

OO constructor for temp directories.

Returns a blessed reference to a PV SV whose string value is the dirname. The PV SV is blessed into File::Temp::Dir and has stringify overload so “$obj” returns the dirname path.

tempdir

returns directory path

tempfile

returns ($fh, $filename)

File::stat

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

FileHandle

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

FileHandle is a legacy IO class that inherits from IO::File -> IO::Handle. Constructor methods (new, new_from_fd, open, fdopen) create IO SVs directly. All IO methods (close, print, read, seek, etc.) delegate to IO::Handle’s p5 implementations.

Functions

fdopen

open from file descriptor on existing handle.

new

Returns a new IO SV, optionally opened.

new_from_fd

create handle from file descriptor.

open

open a file on this handle.

seek_cur

seek_end

seek_set

stub

pop args, push undef, return retop.

Hash::Util

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Implements all 39 Hash::Util functions for the p5 runtime. Operates directly on P5Sv/P5HvBody/P5He raw pointers.

Hash locking uses SVF_READONLY flag (perl5 sv.h bit 27):

  • On an HV SV: keys are restricted (no new keys allowed).
  • On a value SV: value is read-only (immutable). Placeholder entries use sv_placeholder() sentinel (hidden keys in a restricted hash that have been “deleted” or never assigned).

Functions

all_keys

populate arrays with visible and hidden keys

clear_placeholders

remove placeholder entries

hash_locked

returns 1 if keys restricted, 0 otherwise

hash_unlocked

returns 1 if keys NOT restricted, 0 otherwise

hashref_locked

same as hash_locked

hashref_unlocked

same as hash_unlocked

hidden_keys

returns list of hidden (placeholder) keys

hidden_ref_keys

same as hidden_keys

hv_store

store key/value pair

returns all legal keys (visible + hidden)

same as legal_keys

lock_hash

lock_hash_recurse

lock_hashref

same as lock_hash but takes a hashref directly

lock_hashref_recurse

lock_keys

lock_ref_keys

lock_ref_keys_plus

lock_ref_value

lock_value

unlock_hash

unlock_hash_recurse

unlock_hashref

same as unlock_hash but takes a hashref directly

unlock_hashref_recurse

unlock_keys

unlock_ref_keys

unlock_ref_value

unlock_value

I18N::Langinfo

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Exports langinfo() and nl_langinfo constants. Self-contained — operates on P5Sv via P5Interp.

Functions

langinfo

wraps libc nl_langinfo().

IO::Dir

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Provides OO directory handle operations backed by libc opendir/readdir.

Functions

close

close directory handle.

import

noop.

new

create new dir handle, optionally opened.

open

open directory.

read

read next directory entry.

rewind

rewind directory to beginning.

seek

tell

IO::File

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

IO::File inherits from IO::Handle. Methods not found here fall through to IO::Handle::p5 in the dispatch chain (wired in native/mod.rs).

Functions

binmode

close

import

noop.

new

With no args: returns unopened IO SV. With args: opens file and returns IO SV.

open

seek

tell

IO::Handle

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

autoflush

get/set autoflush.

binmode

set encoding layer.

blocking

stub, always returns 1.

clearerr

always returns 0.

close

close the filehandle.

eof

check end-of-file.

error

always returns 0 (no error tracking).

fdopen

open from file descriptor.

fileno

return file descriptor number.

flush

flush output buffer.

getc

read a single character.

getline

read one line.

getlines

read all remaining lines.

import

noop.

input_line_number

new

returns a new unopened IO SV.

opened

check if handle is open.

write to filehandle.

printf

formatted write.

read

buffered read.

say

print with trailing newline.

seek

seek_cur

seek_end

seek_set

stat

fstat on the file descriptor.

tell

truncate

truncate file.

write

write bytes.

IO::Pipe

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

close

close pipe end, waitpid if child was forked.

This handles IO::Pipe::End::close. After closing the PerlIO layer, if this pipe end was created by reader(CMD)/writer(CMD), waits for the child process to exit.

handles

return two unopened IO::Pipe::End IO SVs.

import

noop.

new

create a pipe(2) pair.

Returns an IO SV (type PIPE) representing the reader end. The writer fd is stored in the side table, consumed by reader()/writer().

reader

configure as reader end.

Without args: closes writer fd, opens reader fd as PerlIO, returns IO SV. With command: fork+exec child writing to pipe, parent reads from pipe.

writer

configure as writer end.

Without args: closes reader fd, opens writer fd as PerlIO, returns IO SV. With command: fork+exec child reading from pipe, parent writes to pipe.

IO::Seekable

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

IO::Seekable provides seek/tell/setpos/getpos methods. Most delegate to IO::Handle equivalents.

Functions

import

noop.

seek

delegates to IO::Handle.

setpos

seek to absolute position.

tell

IO::Select

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

IO::Select objects are blessed HV refs internally storing:

  • __fds: an AV ref mapping fd_number -> stored handle SV
  • __count: integer count of registered handles

Mirrors the pp.rs implementation using P5Interp/P5Sv types.

Functions

add

add handles to the select set.

bits

return vec-style bitmask string.

can_read

which handles are readable.

can_write

which handles are writable.

count

return number of registered handles.

exists

check if handle is in the set.

handles

return list of all registered handles.

has_exception

which handles have exceptions.

import

noop.

new

create new select set.

remove

remove handles from the select set.

select

Static method. Returns () on timeout, or ($readable, $writable, $excepted) arrayrefs.

IO::Socket

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

import

noop, returns 1.

new

creates a blessed hashref with constructor args.

Mirrors pp.rs dispatch “new”: creates HV, stores key/value pairs from args (skipping invocant), blesses into IO::Socket, returns RV.

send

returns length of $data.

sockdomain

returns AF_INET (2).

socktype

returns SOCK_STREAM (1).

stub_one

for bind, listen, shutdown, setsockopt,

socket, socketpair.

stub_undef

for accept, peername, sockname, sockopt,

getsockopt, connected, recv.

stub_zero

for connect, protocol, atmark.

timeout

get/set timeout.

With arg: returns the new value. Without: returns undef.

Internals

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

hv_clear_placeholders

perl5 universal.c:752-764 (XS_Internals_hv_clear_placehold) -> hv.c hv_clear_placeholders()

Removes all placeholder entries from a restricted hash. A placeholder is an HE whose hent_val == &PL_sv_placeholder.

svreadonly

query readonly flag

Internals::SvREADONLY($sv, $on) — set/clear readonly flag

perl5 universal.c:669-700 (XS_Internals_SvREADONLY)

Prototype: [$%@];$ arg0 is always a reference; we SvRV to get the referent. With 1 arg: returns &PL_sv_yes if READONLY, &PL_sv_no otherwise. With 2 args: if truthy, set SVf_READONLY and return yes;

         if falsy, clear SVf_READONLY and return no.

svrefcnt

return reference count

Internals::SvREFCNT($sv, $newcount) — set reference count (dangerous!)

perl5 universal.c:728-750 (XS_Internals_SvREFCNT)

Prototype: [$%@];$ Returns refcnt - 1 (minus the ref created by the backslash). With 2 args: sets SvREFCNT to $newcount + 1, returns $newcount.

List::Util

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

preamble

MIME::Base64

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

decode_base64

decode_base64url

decoded_base64_length

encode_base64

encode_base64url

encoded_base64_length

MIME::QuotedPrint

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

decode_qp

encode_qp

Math::GMP

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

import

mirrors lib/Math/GMP.pm:73-79.

Called as: Math::GMP->import(‘:constant’)

When ‘:constant’ is in the import list, registers native_new_str as the overload::constant handler for integer literals. This is the compile-time hook that makes 2 ** 100 use GMP arithmetic.

perl5 Math/GMP.pm:78: overload::constant integer => sub { Math::GMP->new(shift) }

In pperl we call the same interp-level machinery that overload_constant (mod.rs) uses: set HINT_NEW_INTEGER | HINT_LOCALIZE_HH in interp.hints and store a CV pointing to native_new_str in GvHV(interp.hintgv)[“integer”].

new_str

Creates a Math::GMP object from the string form of the literal.

perl5 GMP.pm:78: sub { Math::GMP->new(shift) } shift = $_[0] = the string form of the literal token (decimal integer string).

POSIX

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

PadWalker

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Limitations

The p5 runtime stores pads as flat arrays of *mut P5Sv without any name metadata at runtime. Neither P5CvBody nor P5Interp carry pad slot names — those exist only at compile time in the codegen’s OpArena and are not preserved into the interpreter.

As a result, PadWalker introspection functions cannot map pad slots to variable names. They return correctly-typed empty hashrefs (which is what callers expect) rather than bare undef (which causes type errors downstream).

To make PadWalker fully functional in the p5 runtime, pad name arrays would need to be stored on P5CvBody and P5Interp — a change outside the scope of this module.

Functions

closed_over

return hashref of closed-over variables for a CV.

Returns empty hashref — p5 runtime lacks pad name metadata.

peek_my

return hashref of my variables at call depth $level.

Level 0 = the calling sub’s pad. Level N = N sub frames up.

NOTE: The p5 runtime does not store pad slot names at runtime, so we return an empty hashref (correct type, no entries). This avoids type errors in callers that expect hashref returns.

peek_our

return hashref of our variables at call depth $level.

NOTE: Returns empty hashref — p5 runtime lacks pad name metadata.

peek_sub

return hashref of all pad variables for a CV.

Dies if the argument is not a code reference. Returns empty hashref for user subs (no pad name data available). Dies with “no padlist” for native/stub subs (matches perl5 XS behaviour).

set_closed_over

replace closed-over variables from a hashref.

No-op in p5 runtime — pad name metadata not available to map names to slots.

var_name

Returns undef — p5 runtime lacks pad name metadata.

Peta::FFI

pperl-specific module built into the interpreter. Runtime: P5.

Scalar::Util

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

blessed

returns package name if blessed reference, undef otherwise.

dualvar

create a dual-valued SV with both numeric and string values.

Creates an SV with IOK|POK (or NOK|POK) flags set.

isbool

check if SV is a boolean (PL_sv_yes or PL_sv_no).

isdual

check if scalar is a dualvar. Always returns false.

isvstring

check if scalar was created as a v-string. Always returns false.

isweak

stub, always returns false.

looks_like_number

check if SV looks like a number.

openhandle

check if FH is an open filehandle.

Returns the handle if open, undef otherwise. Recognizes STDIN (fd 0), STDOUT (fd 1), STDERR (fd 2) as always open.

preamble

readonly

check SVf_READONLY flag.

pperl does not currently use a readonly flag, so always returns false.

refaddr

returns numeric address of the referent.

reftype

returns ref type string (SCALAR, ARRAY, HASH, CODE, etc.)

without regard to blessing. Returns undef for non-references.

set_prototype

set CV prototype string.

Stub implementation — prototype setting not yet wired into p5 CVs.

tainted

always returns false (no taint mode in pperl).

unweaken

stub.

weaken

stub. Weak references not fully implemented in p5 runtime.

Socket

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

getaddrinfo

Each result is a hashref: {family, socktype, protocol, addr, canonname}. Mirrors Socket.xs: wraps libc::getaddrinfo.

getnameinfo

Mirrors Socket.xs: wraps libc::getnameinfo.

import

no-op in p5 runtime; Exporter mechanism handles symbol copying.

in6addr_any

in6addr_loopback

inaddr_any

inaddr_broadcast

inaddr_loopback

inaddr_none

inet_aton

resolve to packed 4-byte IPv4 address or undef.

Mirrors Socket.xs: inet_aton tries inet_aton(3) which handles dotted-quad and hostnames. For now: dotted-quad + “localhost” fast path; hostname → DNS via libc::getaddrinfo (AF_INET).

inet_ntoa

convert 4-byte packed address to dotted-quad string.

Mirrors Socket.xs: calls inet_ntoa(3).

inet_ntop

AF-aware packed address to string.

Mirrors Socket.xs: calls inet_ntop(af, src, dst, size).

inet_pton

AF-aware address parsing.

Mirrors Socket.xs: calls inet_pton(af, src, dst).

pack_sockaddr_in

Mirrors Socket.xs: constructs struct sockaddr_in with sin_family=AF_INET, sin_port in network byte order, sin_addr from 4-byte packed input.

pack_sockaddr_in6

Mirrors Socket.xs: struct sockaddr_in6 layout.

pack_sockaddr_un

Mirrors Socket.xs: struct sockaddr_un, sun_path up to 107 bytes.

sockaddr_family

Mirrors Socket.xs: reads first two bytes as sa_family_t (native byte order).

sockaddr_in

sockaddr_in($sockaddr) (1 arg) = unpack_sockaddr_in

sockaddr_in6

sockaddr_in6($sockaddr) (1 arg) = unpack_sockaddr_in6

unpack_sockaddr_in

Mirrors Socket.xs: extracts sin_port (network→host) and sin_addr (4 bytes).

unpack_sockaddr_in6

unpack_sockaddr_un

Storable

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

dclone

deep-clone a data structure.

fd_retrieve

deserialize from a filehandle.

Reads available data from the filehandle in chunks until EOF.

file_magic

inspect file header, return info hashref or undef.

freeze

serialize to a binary PV string.

import

handle use Storable qw(...).

Exports requested functions into the caller’s namespace.

lock_nstore

nstore with flock.

lock_retrieve

retrieve with flock.

lock_store

store with flock.

nfreeze

freeze with network-order prefix byte.

Data is identical to freeze but prefixed with NFREEZE_PREFIX so that read_magic/file_magic can distinguish netorder output.

nstore

store with network-order prefix.

nstore_fd

store_fd with network-order prefix.

preamble

read_magic

inspect in-memory data header, return info hashref or undef.

retrieve

deserialize from a file.

stack_depth

return current recursion limit.

stack_depth_hash

return current hash recursion limit.

store

serialize to a file.

store_fd

serialize to a filehandle.

thaw

deserialize a binary PV back to a data structure.

Sub::Util

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

preamble

prototype

Returns the prototype string of the subroutine, or undef if no prototype.

Mirrors Sub::Util.xs XS_Sub__Util_prototype: cv = MUTABLE_CV(SvRV(sv)) if CvPROTO(cv): sv_setpvn(TARG, CvPROTO(cv), CvPROTOLEN(cv)); PUSHTARG else: PUSHs(&PL_sv_undef)

perl5 cv.h:112-118: CvPROTO reads SvPVX_const(sv) when SvPOK(sv). pperl: P5CvBody.prototype is a PV SV, null = no prototype.

set_prototype

Sets (or clears when $proto is undef) the prototype of a subroutine. Returns the same code reference.

Mirrors Sub::Util.xs XS_Sub__Util_set_prototype: if SvOK(proto): cv_ckproto_len_flags + sv_setpvn on CvPROTO else: SvPOK_off(cv); Safefree(SvPVX_mutable(cv)) return the original coderef

pperl: assign/clear P5CvBody.prototype PV SV.

set_subname

Sets the name of the subroutine in-place. Unqualified names are placed in “main::”. Returns the same code reference.

Mirrors Sub::Util.xs XS_Sub__Util_set_subname: qualify $name into pkg + shortname cvgv_set(cv, gv) with a new/found GV under that name return the original coderef

pperl simplification: we store the name string directly as P5CvBody.name, bypassing GV manipulation (which requires full glob assignment support).

subname

Returns the fully qualified name of the subroutine (e.g. “main::foo”), or “ANON” for anonymous subs.

Mirrors Sub::Util.xs XS_Sub__Util_subname: cv = MUTABLE_CV(SvRV(sv)) name = CvNAMED(cv) ? hek_to_sv(CvNAMEHEK(cv)) : sv_from_gv(CvGV(cv))

In pperl: P5CvBody.name is the name PV SV (FQN), null = anonymous.

Sys::Hostname

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

hostname

return the system hostname as a PV string.

Mirrors Sys::Hostname XS: calls POSIX gethostname(3) with a 256-byte stack buffer, finds the null terminator, allocates a PV from the bytes up to it, and returns it mortalised.

import

export hostname() into the caller’s package.

Mirrors Exporter: Sys::Hostname has @EXPORT = (‘hostname’). Gets the CV from Sys::Hostname::hostname and installs it into the caller’s stash so bare hostname() calls resolve.

preamble

Time::HiRes

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

preamble

mro

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

get_isarev

reverse @ISA index.

Returns arrayref of classes that inherit from $class.

get_linear_isa

get_mro

get_pkg_gen

import

called by use mro 'c3'.

Sets the MRO algorithm for the calling package.

invalidate_all_method_caches

is_universal

true only for “UNIVERSAL”.

method_changed_in

preamble

set_mro

sdl2

pperl-specific module built into the interpreter. Runtime: P5.

Rust module path: sdl2::p5, Perl package name: SDL2. Self-contained — operates on P5Sv via P5Interp, no imports from crate::runtime or crate::value.

version

Native Rust implementation built into the interpreter. Runtime: P5. See original documentation for the full Perl reference.

Functions

bool

version object is true if any component is non-zero.

perl5: (bool checks numify, so version 0 is false, non-zero is true.

declare

force qv interpretation.

vutil.c:new_version with is_qv=true override.

import

called by use version.

Sets up the version stash and overload table. vutil.c:VERSION (the export function).

is_alpha

true if alpha/dev version (underscore).

vutil.c: checks “alpha” key in hash.

is_lax

check if valid lax version string.

vutil.c:is_lax_version. Simple heuristic: parseable with at least one digit.

is_qv

true if dotted-decimal form.

vutil.c: checks “qv” key in hash.

is_strict

check if valid strict version string.

vutil.c:is_strict_version.

new

vutil.c:new_version

nomethod

returns undef.

normal

return normalized v-string. vutil.c:vnormal.

numify

return numeric string. Overloaded via “(0+”.

vutil.c:vnumify

preamble

stringify

return string form. Overloaded via “(”“.

vutil.c:vstringify

vcmp

comparison operator.

vutil.c:vcmp. Returns -1/0/1 as IV.

Differences from Perl 5

PetaPerl targets near-complete compatibility with Perl 5.42, but intentionally diverges in several areas. This document catalogs both intentional differences and current limitations.

Intentional Differences

Always-On Modern Features

PetaPerl enables all modern Perl features by default. No use feature or version declaration is needed:

  • say — print with newline
  • state — persistent lexical variables
  • // — defined-or operator
  • fc — Unicode case folding
  • Subroutine signatures

use v5.XX declarations are parsed but have no effect. PetaPerl always behaves like 5.42.

No XS Support

PetaPerl does not load XS (C extension) modules. Instead:

  • Native implementations: Performance-critical modules (List::Util, Scalar::Util, Digest::MD5, etc.) are reimplemented in Rust as native functions
  • Pure Perl fallback: Modules with Pure Perl implementations work automatically
  • JIT compensation: The JIT compiler closes the performance gap that historically justified XS

This is a strategic decision: XS ties Perl to a C calling convention and prevents JIT optimization. PetaPerl’s JIT + auto-parallelization makes Pure Perl competitive with XS.

Linux Only

PetaPerl runs on Linux exclusively (any architecture supported by Cranelift: x86-64, AArch64, RISC-V, s390x). No Windows, macOS, BSD, or other platforms.

$^O always returns "linux".

p5 Runtime

PetaPerl includes an experimental --p5 flag that uses a self-contained Perl 5 compatible runtime. This is an alternative execution mode, not the default.

Version Reporting

$] reports 5.042000 and $^V reports v5.42.0. Code that checks these values will see PetaPerl as a perl 5.42 interpreter.

Unimplemented Features

tie/tied/untie

The tie mechanism is parsed and the functions exist, but they are stubs that return undef. Tied variables do not dispatch to the tie class methods.

Modules that depend on tie (e.g., Tie::File, Tie::Hash::NamedCapture internals) will not function correctly.

Formats

format/write/formline are parsed but the format output system is not implemented. write and formline are stubs. Use printf/sprintf instead.

Lvalue Subroutines

The :lvalue attribute is parsed but not enforced. Subroutines declared with :lvalue behave like normal subroutines.

Internals::SvREADONLY

Not implemented. This affects modules that rely on marking scalars read-only (charnames, unicore).

Some Pragmas

PragmaStatus
strictParsed, partially enforced
warningsParsed, partial warning categories
utf8Parsed, strings are always UTF-8 internally
constantFully working
base/parentFully working
CarpFully working
overloadPartially working
rePartially working

Command-Line Flags

These perl5 flags are not supported:

FlagDescription
-IAdd include path (use PERL5LIB instead)
-M / -mLoad module from command line
-n / -pImplicit input loop
-lAutomatic line-ending processing
-a / -FAutosplit mode
-iIn-place edit
-xExtract script from message
-TTaint mode

Known Limitations

Regex Engine

PetaPerl’s regex engine passes 99.3% of perl5’s re_tests (1959/1972). Known gaps:

  • Self-referential captures (e.g., (a\1)) — 3 tests
  • local in (?{code}) blocks — 2 tests
  • Multi-character Unicode case folding — 2 tests
  • Branch reset group backreferences — 5 tests
  • String interpolation edge cases — 1 test

Module Compatibility

ModuleIssue
Test2::APIuse Module(\$lexical) loses compile-time lexical side effects
Module::CoreListTimeout (>20s) due to enormous hash literals
charnames/unicoreDepends on Internals::SvREADONLY
Pod::Simple::RTFFlip-flop operator edge case

Flip-Flop Operator

The .. and ... operators in scalar context (flip-flop) have edge cases where pperl’s behavior differs from perl5. Most common uses work, but complex chaining may trigger errors.

Filehandle Heuristics

print $x, "\n" interprets $x as a potential filehandle (matching perl5 behavior). For variables that hold filehandle references, use the block form: print {$fh} "data\n".

Compile-Time Module Effects

use Module(args) runs at compile time in perl5. Modules that produce lexical side effects at compile time (e.g., use Instance(\$INST)) may not work because pperl’s runtime doesn’t replay those effects. Package-variable side effects work correctly.

Native Modules

PetaPerl does not support XS (C extensions). Instead, performance-critical CPAN modules are reimplemented natively in Rust. These “native modules” provide the same Perl-level API as their XS counterparts but integrate directly with PetaPerl’s runtime — enabling JIT compilation, auto-parallelization, and zero FFI overhead.

How It Works

When you write use List::Util qw(sum min max), PetaPerl’s module loader detects that List::Util has a native implementation and registers Rust function pointers directly. There is no compilation step, no .so loading, and no XS glue code.

Native functions are dispatched via NativeFn — a direct function pointer with O(1) call overhead, identical to built-in operators.

Available Native Modules

Core Utilities

ModuleFunctionsNotes
Scalar::Utilblessed, reftype, refaddr, weaken, isweak, looks_like_number, …Full API
List::Utilsum, min, max, first, any, all, none, reduce, …MULTICALL optimized
Sub::Utilsubname, set_subname
Hash::Utillock_keys, lock_hash, …

Digest / Crypto

ModuleFunctionsNotes
Digest::MD5md5, md5_hex, md5_base64, OO interfaceFull API
Digest::SHAsha1, sha256, sha512, OO interfaceFull API
MIME::Base64encode_base64, decode_base64
MIME::QuotedPrintencode_qp, decode_qp

File / System

ModuleFunctionsNotes
File::Basenamebasename, dirname, fileparse
File::Copycopy, move
File::Findfind, finddepth
File::Globbsd_glob
File::Pathmake_path, remove_tree
File::Speccatdir, catfile, rel2abs, …
File::Temptempfile, tempdirOO + functional
File::statstat (OO)
Cwdcwd, getcwd, abs_path
Sys::Hostnamehostname

I/O

ModuleFunctionsNotes
IO::FileOO file handle
IO::HandleOO handle base
IO::DirOO directory handle
IO::PipeOO pipe handle
IO::Selectselect wrapper
IO::SocketOO socket handle
IO::Seekableseek/tell mixin
FileHandleLegacy OO handle
Socketsocket primitives

Data / Encoding

ModuleFunctionsNotes
Data::DumperDumper
Storablefreeze, thaw, nstore, retrieve
Encodeencode, decode, find_encoding
JSON::PPencode_json, decode_jsonVia Pure Perl

Numeric / Math

ModuleFunctionsNotes
POSIXfloor, ceil, fmod, strtod, strftime, …Subset
Math::GMPArbitrary precision integersVia Peta::FFI::GMP

Build / Config

ModuleFunctionsNotes
Config%Config hashBuild configuration
FcntlO_RDONLY, O_WRONLY, …Constants auto-loaded
ErrnoENOENT, EACCES, …Constants auto-loaded

Introspection

ModuleFunctionsNotes
BCompiler backend introspectionMinimal
PadWalkerpeek_my, peek_our
mroget_linear_isa, set_mro
versionVersion object handlingFull OO API

Performance

Native modules run at the same speed as built-in operators since they share the same dispatch mechanism. For block-taking functions (first, any, all, reduce), PetaPerl uses MULTICALL optimization to avoid per-element subroutine call overhead.

Benchmarks (vs perl5 with XS):

FunctionPetaPerl nativeperl5 XSRatio
List::Util::sum1.7x fasterbaseline
List::Util::min/max2.9x fasterbaseline
List::Util::first~1xbaselineMULTICALL parity

Adding Native Modules

See Documentation Pipeline for how native module documentation is extracted from Rust source.

Auto-FFI (Peta::FFI)

PetaPerl includes a built-in Foreign Function Interface that allows calling C library functions directly from Perl — without writing XS, without a C compiler, without any build step. This is a PetaPerl-exclusive feature with no perl5 equivalent.

Architecture

The FFI system has three layers:

LayerModulePurpose
0Peta::FFIRaw FFI: dlopen + call with type signatures
1Peta::FFI::Libc, ::UUID, ::GMPPre-baked bindings for specific libraries
2Peta::FFI::scan()Discovery: enumerate available system libraries

Layer 0 uses libffi for dispatch — any C function signature works, no code generation required.

Layer 0: Raw FFI

dlopen / call / dlclose

use Peta::FFI qw(dlopen call dlclose);

my $lib = dlopen("libm.so.6");
my $result = call($lib, "sqrt", "(d)d", 2.0);   # √2 = 1.4142...
print "sqrt(2) = $result\n";
dlclose($lib);

Type Signature Format

The signature string "(arg_types)return_type" uses single-character type codes:

CodeC TypePerl Mapping
vvoid(no value)
iintIV
llongIV
Lunsigned long / size_tUV
ddoubleNV
ffloatNV
pconst char*String (input)
Pvoid* (mutable buffer)String (output)

Examples:

  • "(d)d" — one double argument, returns double (e.g. sqrt)
  • "(p)L" — one string argument, returns unsigned long (e.g. strlen)
  • "(ppi)i" — two strings + int, returns int
  • "()i" — no arguments, returns int (e.g. getpid)

Library Discovery

use Peta::FFI qw(scan);

my $libs = scan();
# Returns hashref: { "libz.so.1" => "/usr/lib/libz.so.1", ... }

for my $soname (sort keys %$libs) {
    print "$soname => $libs->{$soname}\n";
}

scan() calls ldconfig -p to enumerate all shared libraries available on the system.

Layer 1: Pre-baked Bindings

Layer 1 modules provide Perl-native APIs for specific C libraries, with proper argument validation, return value conversion, and error handling. No type signatures needed — just call functions.

Peta::FFI::Libc

Direct bindings to libc functions, grouped by category:

Process: getpid, getppid, getuid, getgid, geteuid, getegid

Strings: strlen, strerror

Environment: getenv, setenv, unsetenv

Math: abs, labs

System: sleep, usleep, gethostname, uname

File operations: access, unlink, rmdir, mkdir, chmod, chown

use Peta::FFI::Libc qw(getpid gethostname uname);

print "PID: ", getpid(), "\n";
print "Host: ", gethostname(), "\n";

my @info = uname();
print "OS: $info[0] $info[2] ($info[4])\n";

Peta::FFI::UUID

Bindings to libuuid (must be installed on the system):

use Peta::FFI::UUID qw(uuid_generate uuid_generate_random);

my $uuid = uuid_generate();           # e.g. "550e8400-e29b-41d4-a716-446655440000"
my $rand = uuid_generate_random();    # random-based UUID

Functions: uuid_generate, uuid_generate_random, uuid_generate_time, uuid_parse, uuid_is_null, uuid_unparse.

If libuuid is not installed, use Peta::FFI::UUID dies with a diagnostic message.

Peta::FFI::GMP (Math::GMP)

Arbitrary-precision integer arithmetic via libgmp. Integrates with the Math::GMP class name for compatibility with CPAN’s Math::GMP:

use Math::GMP;

my $big = Math::GMP->new("123456789012345678901234567890");
my $sum = $big + 42;
print "$sum\n";

GMP integers are heap-allocated mpz_t values stored as blessed references. Operator overloading (+, -, *, /, %, **, <=>, "") works identically to the XS module.

If libgmp is not installed, use Math::GMP dies with a diagnostic message.

When to Use FFI vs Native Modules

ScenarioRecommendation
Common CPAN module (List::Util, etc.)Native module (already built in)
System library not yet wrappedLayer 0 raw FFI
Frequent calls to same libraryRequest Layer 1 pre-baked binding
One-off experimentLayer 0 raw FFI

Layer 0 FFI has per-call overhead from argument marshalling and libffi dispatch. Layer 1 pre-baked bindings call Rust/C directly and are faster. Native modules are fastest — same speed as built-in operators.

JIT Compilation

PetaPerl includes a JIT (Just-In-Time) compiler based on Cranelift, the same backend used by Wasmtime. The JIT compiles hot loops into native machine code, delivering performance that can exceed perl5 by orders of magnitude.

What Gets JIT-Compiled

The JIT targets loopsfor and while — that contain arithmetic and comparison operations. Specifically:

Integer For-Loops

my $sum = 0;
for my $i (1..1_000_000) {
    $sum += $i;
}

The JIT detects the integer accumulator pattern and compiles the loop body to native code. The loop variable ($i) and accumulators ($sum) are held in CPU registers.

While-Loops with Float Arithmetic

while ($y < $max_y) {
    my $cx = $x * $scale;
    my $cy = $y * $scale;
    # ... computation ...
    $y += $step;
}

Floating-point arithmetic (+, -, *, /), comparisons (<, >, <=, >=), and control flow (if/last) within while-loops are compiled to native SSE/AVX instructions.

Nested Loops

The JIT handles nested loops up to 5 levels deep. Each nesting level generates its own set of Cranelift basic blocks with proper phi-node connections for variables that flow between levels.

String Operations (via Extern Calls)

The JIT supports .= (concat-assign) and $x = "" (clear) operations on string variables through extern function calls from JIT-compiled code back into the Rust runtime.

What Is Not JIT-Compiled

  • Subroutine calls — function call overhead dominates, JIT benefit is marginal
  • Regex operations — the regex engine has its own optimization path
  • I/O operations — I/O-bound code doesn’t benefit from JIT
  • Complex data structure access — hash/array operations with dynamic keys
  • String-heavy computation — string building is handled by the PvBuf optimization instead

Code that isn’t JIT-compiled still runs on the interpreter, which has its own fast paths for common operations.

Performance

Ackermann Function

The interpreter fast path (not JIT) handles recursive integer arithmetic:

RuntimeTimeSpeedup
perl5630ms1.0x
pperl (interpreter)14ms45x faster

Mandelbrot Set (1000x1000)

JIT compilation of nested while-loops with float arithmetic:

RuntimeTimeSpeedup
perl512,514ms1.0x
pperl (JIT only)163ms76x faster
pperl (JIT + parallel)29ms431x faster

How It Achieves This

  1. Register allocation: Loop variables in CPU registers instead of interpreter stack
  2. Type specialization: Variables proven to be integer or float use native instructions directly
  3. Branch elimination: Constant conditions removed at compile time
  4. No dispatch overhead: Native code replaces the interpreter dispatch loop entirely within JIT’d regions

CLI Control

# Default: JIT enabled
pperl script.pl

# Disable JIT (interpreter only)
pperl --no-jit script.pl

The test harness runs with --no-jit by default to test interpreter correctness. JIT-specific tests in t/62-jit/ override this.

Architecture

Compilation Pipeline

Loop detected → Analyze variables and types → Build JIT IR
  → Compile via Cranelift → Cache compiled function → Execute native code

Caching

Compiled functions are cached by the enterloop op’s ID in the op arena. A CachedWhileLoop stores:

  • The compiled native function pointer
  • Variable type information (float vs integer vs string)
  • Constant string pool (for string operations)
  • Metadata for parallel dispatch eligibility

Subsequent iterations of the same loop reuse the cached compilation.

JIT IR

The JIT uses its own intermediate representation (JitIr) that maps Perl operations to Cranelift operations:

  • JitIr::WhileLoop { condition_ir, body_ir } — recursive for nesting
  • JitIr::FloatVar / JitIr::IntVar — typed variable access
  • JitIr::BinOp — arithmetic and comparison
  • JitIr::ExitIfFalse / JitIr::ExitIfTrue — loop exit and last

Variable Types

The JIT tracks two variable types:

  • JitType::F64 — floating-point values held in an f64 buffer
  • JitType::Ptr — string values accessed through extern calls to the Rust runtime

Variables are typed based on their usage pattern in the loop body. Mixed-type variables fall back to the interpreter.

Parallel Execution

PetaPerl automatically parallelizes eligible loops using Rayon, a work-stealing thread pool. Combined with JIT compilation, this enables dramatic speedups for compute-heavy workloads.

How It Works

When PetaPerl encounters a parallelizable loop, it:

  1. Analyzes the loop body for side effects and shared mutable state
  2. Identifies reduction variables (accumulators like $sum += ...)
  3. Distributes iterations across threads using Rayon’s work-stealing scheduler
  4. Combines results using the detected reduction operations

Each thread gets its own copy of loop-local variables. Reduction variables are combined after all threads complete.

What Gets Parallelized

JIT’d While-Loops

When the JIT compiles a while-loop and the analysis detects:

  • A counter variable with known bounds
  • Reduction variables (accumulate-only pattern)
  • No I/O or side effects in the loop body

Then the loop body is compiled once and executed in parallel across threads.

Built-in Functions

map and grep with pure callbacks can execute in parallel:

my @results = map { expensive_computation($_) } @large_array;
my @filtered = grep { complex_test($_) } @large_array;

Parallelization requires:

  • No side effects in the callback
  • No shared mutable state
  • Collection size above the parallelization threshold

CLI Control

# Default: parallelization enabled
pperl script.pl

# Disable parallelization
pperl --no-parallel script.pl

# Explicitly enable (default)
pperl --parallel script.pl

# Set thread count (default: number of CPU cores)
pperl --threads=4 script.pl

# Set minimum collection size for parallelization
pperl --parallel-threshold=1000 script.pl

The test harness runs with --no-parallel by default to ensure deterministic test output.

Performance

Mandelbrot Set (1000x1000)

ModeTimevs perl5
perl512,514ms1.0x
pperl interpreter~3,500ms3.6x faster
pperl JIT163ms76x faster
pperl JIT + parallel (8 threads)29ms431x faster

Scaling

The work-stealing scheduler provides near-linear scaling for embarrassingly parallel workloads:

ThreadsMandelbrot 4000x4000Scaling
1baseline1.0x
2~50% time~1.9x
4~25% time~3.8x
8~13% time~5.2x

Scaling is sub-linear due to memory bandwidth, cache effects, and reduction overhead.

Limitations

String Operations

String operations (.= concat, string building) are not parallelized. The JIT’s string support uses extern calls back to the Rust runtime, which requires mutable access to shared state. When the JIT detects string variables in a loop, parallel dispatch is disabled.

Side-Effect Detection

The parallelization analyzer is conservative. Any of these disqualify a loop:

  • I/O operations (print, open, file reads)
  • Global variable writes
  • Subroutine calls (unless proven pure)
  • Regex operations with side effects (s///)

False negatives (missed parallelization opportunities) are safe — the loop simply runs sequentially. False positives (incorrect parallelization) would be bugs.

Determinism

Parallel execution may change the order of side effects. For this reason, parallelization is only applied when the analysis proves the loop body is free of observable side effects.

Output order is preserved for map and grep — the results array maintains the same element ordering as sequential execution.

How Reduction Detection Works

The analyzer identifies reduction variables by scanning for accumulation patterns and subtracting reset patterns:

# Detected as reduction: $sum accumulates, never reset in loop
my $sum = 0;
for my $x (@data) {
    $sum += $x;
}

# NOT a reduction: $temp is reset each iteration
for my $x (@data) {
    my $temp = $x * 2;  # reset (my declaration)
    $sum += $temp;       # $sum is still a reduction
}

The formula: reductions = accumulations - resets. This prevents false positives where a variable is both accumulated and reset within the loop body.

PetaPerl Testing

PetaPerl uses conformance testing: each .t file runs under both perl5 and pperl, and passes when output matches exactly.

Quick Reference

# Run specific tests
t/TEST 05-op                          # Tests matching "05-op"
t/TEST 25-regex/010-basic.t           # Single test file
t/TEST -v 05-op                       # Verbose (show diffs)

# Check results without re-running
grep "FAIL" t/.results/LATEST.txt

# Full regression check (coordinate with team)
t/TEST --skip=bytecode,tier,debug

Detailed Documentation

Test Format

Tests use raw TAP (Test Anything Protocol). No test framework, no shared library:

#!/usr/bin/perl
# Description of what this tests
print "1..3\n";
my $t = 1;
sub ok { my ($c,$n) = @_; print $c ? "ok $t - $n\n" : "not ok $t - $n\n"; $t++; }

ok(1 + 1 == 2, "addition");
ok("a" eq "a", "string eq");
ok(defined 0, "zero is defined");

Concurrency Warning

Multiple developers may share this machine. Run only targeted tests during development. Full-suite runs should be coordinated.

Running Tests

Test Harness: t/TEST

PetaPerl’s test harness is t/TEST, a Perl 5 script that runs each .t file under both perl and pperl, then compares output. A test passes when pperl’s output matches perl5’s output exactly.

t/TEST [OPTIONS] [FILTERS...]

The harness is executable — run it directly, never as perl t/TEST.

Filter Semantics

Positional arguments are substring filters with OR logic:

t/TEST 00-base                    # Run tests matching "00-base"
t/TEST 00-base 05-op              # Run tests matching "00-base" OR "05-op"
t/TEST 25-regex/010-basic.t       # Run a specific test file

Negative filtering via --skip:

t/TEST --skip=bytecode,tier,debug # Run all except these categories

When using a positive filter, --skip is unnecessary — the filter already limits scope.

Options

OptionDescription
-v, --verboseShow diffs on failure
-m, --monitorShow memory/time stats per test
-c, --compareCompare against previous run in t/.results/
--skip=PATTERNSkip tests matching substring (comma-separated)
--no-bailContinue even if 00-base tests fail
--no-saveDon’t save results to .results/
--timeout=SECSTimeout per test (default: 5)
--memory=MBMemory limit per test (default: 512)
--pperl-flags=STROverride default pperl flags
-h, --helpShow help

Default pperl Flags

The harness runs pperl with --no-jit --no-parallel by default. This ensures tests exercise the interpreter, not the JIT or parallelization engine.

Override per-test via a comment header in the test file:

# pperl-cli: -(--no-jit)

This removes --no-jit from the flags, enabling JIT for that test. Override globally:

t/TEST --pperl-flags="--no-timeout"

Result Tracking

Results are automatically saved to t/.results/YYYYMMDD-HHMMSS.txt. The most recent results are also symlinked as t/.results/LATEST.txt.

Compare against the previous run:

t/TEST --compare --skip=bytecode,tier,debug

Check current pass/fail status without re-running:

grep "FAIL" t/.results/LATEST.txt
grep "05-op" t/.results/LATEST.txt

This is significantly faster than re-running tests and should be preferred when checking status.

Bail-Out Behavior

If any test in 00-base/ fails, the harness stops by default (bail-out). This gate ensures fundamental functionality works before running the larger suite. Override with --no-bail.

Concurrency Warning

Multiple developers (or Claude instances) may share the same machine. Running the full test suite without coordination risks interference. Always use targeted filters during development. Full-suite runs (t/TEST --skip=bytecode,tier,debug) should be coordinated.

Writing Tests

Format: Raw TAP

Tests use raw TAP (Test Anything Protocol). No test framework, no shared library. Each .t file is a self-contained Perl script that produces TAP on stdout.

#!/usr/bin/perl
# Description of what this tests
print "1..3\n";
my $t = 1;
sub ok { my ($c,$n) = @_; print $c ? "ok $t - $n\n" : "not ok $t - $n\n"; $t++; }

ok(1 + 1 == 2, "addition");
ok("a" eq "a", "string eq");
ok(defined 0, "zero is defined");

The plan line (1..N) must be the first line of output and declares how many tests to expect.

Diagnostics

Lines starting with # are diagnostics. The harness ignores them, but they appear in verbose mode:

print "# debug: x=$x\n";

Test Helper Pattern

Most test files define a local ok sub. Keep it minimal:

my $t = 1;
sub ok { my ($c,$n) = @_; print $c ? "ok $t - $n\n" : "not ok $t - $n\n"; $t++; }

For tests that need is (value comparison with diagnostic on failure):

sub is {
    my ($got, $expected, $name) = @_;
    if ($got eq $expected) {
        print "ok $t - $name\n";
    } else {
        print "not ok $t - $name\n";
        print "# got:      '$got'\n";
        print "# expected: '$expected'\n";
    }
    $t++;
}

Per-Test Configuration

pperl CLI Flags

Override default harness flags via comment header:

# pperl-cli: -(--no-jit)

This removes --no-jit, enabling JIT for the test. Other examples:

# pperl-cli: -(--no-parallel)           # enable parallelization
# pperl-cli: --timeout=60               # longer timeout
# pperl-cli: -(--no-jit) --timeout=120  # JIT + long timeout

Timeout

The default per-test timeout is 5 seconds. For slow tests:

# pperl-cli: --timeout=30

File Naming

  • Files end in .t
  • Numbered prefix: NNN-name.t (005, 010, 015, … — gaps for insertion)
  • Perl5 ports use -perl5.t suffix: 010-array-perl5.t

Placement

Place tests in the appropriate numbered directory based on category. See Test Structure for the directory layout.

New tests for bug regressions during development go in 99-debug/ initially. Once the bug is fixed and tests pass, move them to the appropriate category directory.

What Makes a Test Pass

The harness runs each .t file under both perl (system perl5) and pperl. A test passes when pperl produces identical output to perl5. This means:

  • The TAP output (ok/not ok lines) must match
  • Diagnostic output must match
  • Warnings on stderr are compared separately
  • Exit codes are compared

This is a conformance test: it verifies pperl behaves like perl5, not that a particular result is correct in isolation.

Test Structure

Directory Layout

Test directories are numbered and run in order. The numbering provides natural ordering and allows insertion of new categories.

DirectoryPurposeSpeed
00-base/Gate tests (must all pass)Fast
01-parsing/Parser testsFast
02-builtins/Builtins registryFast
05-op/OperatorsFast
10-cmd/Control flowFast
15-sub/SubroutinesFast
15-modules/Module systemFast
20-data/Data structuresFast
22-unicode/Unicode handlingFast
25-regex/Regular expressionsFast
30-special/Special variablesFast
40-package/Packages and OOPFast
45-builtin/Built-in functionsFast
50-context/Context handlingFast
55-io/I/O operationsFast
60-parallel/Auto-parallelizationMedium
62-jit/JIT compilationMedium
64-auto-ffi/Auto FFIMedium
66-bytecode/Bytecode testsSlow
68-daemon/Daemon modeMedium
70-core-tier0/Core module integration (tier 0)Slow
71-core-tier1/Core module integration (tier 1)Slow
72-core-tier2/Core module integration (tier 2)Slow
80-complex/Integration testsMedium
81-xs-native/XS-native module replacementsMedium
82-native/Native module testsMedium
99-debug/Bug regression / active debuggingFast
benchmarks/Performance benchmarks (not TAP)Varies

Gate Tests (00-base)

The 00-base/ directory contains fundamental tests. If any fail, the harness stops (bail-out). These verify:

  • Basic parsing works
  • print produces output
  • Variables and assignment work
  • Basic operators function

These must always pass before any other testing is meaningful.

Tier 0 Core Modules (70-core-tier0/)

Tier 0 tests exercise core CPAN modules. They are subdivided:

RangePurposeCount
005-030Native pass modules~55 modules
035-050Fallback pass modules~20 modules
055TAP::* modules~12 modules
057Newly passing misc~3 modules
060Remaining failures~4 modules

Naming Conventions

  • Directories: NN-name/ — numbered with gaps (00, 01, 02, 05, 10, 15, …)
  • Test files: NNN-name.t — numbered within directory (005, 010, 015, …)
  • Perl5 ports: *-perl5.t suffix indicates a test ported from perl5-upstream
  • Gaps: Leave room for insertion (use increments of 5)

The 99-debug Directory

99-debug/ is a workspace for active bug investigation. Tests here follow the “complex case” debugging workflow:

  1. Create synthetic tests isolating a hypothesis
  2. Fix the bug
  3. Verify the fix
  4. Move passing tests to the appropriate category directory
  5. Clean up 99-debug

Tests should not accumulate in 99-debug permanently.

Benchmarks

The benchmarks/ directory contains performance tests. These are not TAP tests — they measure execution time and are not run by the standard harness. Key benchmarks:

  • 006-mandelbrot-bench.pl — JIT + parallelization scaling test
  • Various arithmetic and string operation benchmarks

Failure Assessment

Understanding Test Failures

A test fails when pperl’s output differs from perl5’s output. The harness compares stdout (TAP lines), stderr (warnings), and exit codes.

Interpreting Results

Verbose Mode

Use -v to see the diff on failure:

t/TEST -v 05-op/010-arithmetic.t

The diff shows exactly where pperl’s output diverged from perl5.

Result Files

Check t/.results/LATEST.txt for the most recent full run:

grep "FAIL" t/.results/LATEST.txt      # All failures
grep "05-op" t/.results/LATEST.txt     # Status of operator tests

Common Failure Patterns

Wrong numeric output: pperl’s numeric formatting differs from perl5 (e.g., trailing zeros, precision differences). Check if the underlying computation is correct.

Missing warning: perl5 emits a warning that pperl doesn’t. Often "Use of uninitialized value" or similar. The test itself may pass, but stderr comparison fails.

Extra output: pperl emits debug output or extra warnings. Check for leftover trace output.

Timeout: Test exceeded the per-test timeout (default 5s). May indicate an infinite loop in pperl, or the test genuinely needs more time.

Parse error: pperl’s parser doesn’t handle a construct that perl5 accepts. Common with edge-case syntax.

Missing feature: The test uses a feature pperl hasn’t implemented. Check the error message.

Regression Detection

Compare the current run against the previous one:

t/TEST --compare --skip=bytecode,tier,debug

The comparison report shows:

  • New failures (regressions) — previously passing tests that now fail
  • New passes (improvements) — previously failing tests that now pass
  • Stable failures — tests that were already failing

Regressions are the critical concern. A code change should never introduce new failures.

Triage Categories

Must Fix (Regressions)

Tests that previously passed but now fail. These indicate a bug introduced by recent changes.

Should Fix (Known Failures)

Tests in the expected failure set that should eventually pass as features are implemented.

Won’t Fix (Intentional Differences)

Tests that fail because pperl intentionally differs from perl5 (e.g., always-enabled modern features, no XS support). These should be documented in the test file with a comment explaining why.

Timeout Failures

May need --timeout override in the test header, or may indicate a real performance regression.

Perl5 Comparison Testing

Methodology

PetaPerl’s test methodology is conformance testing: every test runs under both perl5 and pperl, and pperl passes only when its output matches perl5 exactly. This ensures behavioral compatibility without encoding assumptions about “correct” behavior — perl5 is the authoritative reference.

How It Works

For each test.t:
  1. Run under perl5 → capture stdout, stderr, exit code
  2. Run under pperl → capture stdout, stderr, exit code
  3. Compare all three
  4. PASS if identical, FAIL if any differ

This approach has several advantages:

  • No false positives: If perl5 and pperl agree, the behavior is correct by definition
  • Self-documenting: The expected output doesn’t need to be hardcoded
  • Catches subtle differences: Numeric formatting, warning text, edge cases
  • Easy to extend: Write a Perl script, run it — no test framework needed

System Perl Requirement

The test harness requires a system perl5 (5.42+ recommended) to generate reference output. This is the only runtime dependency on perl5 — pperl itself runs independently.

Edge Cases

Warnings

Perl5’s warning messages include specific text that may differ between versions. Tests should be written to avoid triggering version-dependent warnings where possible.

Random Output

Tests involving rand, time, or other non-deterministic functions need special handling. Either seed the RNG (srand 42) or structure the test to compare properties rather than exact values.

Platform-Dependent Behavior

Some perl5 behaviors vary by platform. Since pperl targets Linux only, ensure the reference perl5 also runs on Linux to avoid false failures.

Numeric Precision

Floating-point output formatting may differ at the last decimal place. PetaPerl aims to match perl5’s sprintf formatting exactly, but edge cases exist.

Version Targeting

PetaPerl targets perl 5.42 compatibility. The reference perl5 used for comparison testing should be version 5.42.x. Using a different perl5 version may produce false failures due to behavioral differences between perl versions.

PetaPerl does not attempt backward compatibility with older Perl versions. use v5.18 and similar declarations are parsed but do not change behavior — pperl always behaves like 5.42.

Upstream Test Mapping

Perl5 Test Suite

The perl5 source tree (perl5-upstream/t/) contains thousands of tests organized by category:

perl5-upstream/t/
  base/     - Fundamental tests
  cmd/      - Control flow
  comp/     - Compilation
  io/       - I/O operations
  op/       - Operators
  re/       - Regular expressions
  run/      - Running perl
  uni/      - Unicode
  lib/      - Library/module tests
  ...

Mapping to PetaPerl Tests

PetaPerl’s test directories correspond to perl5-upstream categories, though the mapping is not 1:1:

perl5-upstreamPetaPerlNotes
t/base/00-base/Fundamental tests
t/comp/01-parsing/Compilation/parsing
t/op/05-op/Operators
t/cmd/10-cmd/Control flow
t/io/55-io/I/O operations
t/re/25-regex/Regular expressions
t/uni/22-unicode/Unicode handling
t/lib/70-core-tier*/Module tests

Porting Process

When porting a test from perl5-upstream:

  1. Convert to raw TAP: Remove use Test::More, use test.pl, and other test framework dependencies. Replace with inline ok sub and print "1..N\n" plan.

  2. Suffix with -perl5.t: Indicates the test originated from perl5-upstream.

  3. Add source comment: Document where the test came from:

    # Ported from perl5-upstream/t/op/arith.t
    
  4. Verify under both runtimes: Run with t/TEST and confirm both perl5 and pperl produce identical output.

  5. Handle unsupported features: If the test uses features pperl doesn’t support, either:

    • Skip those specific subtests with comments explaining why
    • Split the test to separate supported from unsupported portions

re_tests

The regex engine has a specialized test suite based on perl5’s re_tests format. PetaPerl passes 1959/1972 tests (99.3% of perl5-passing tests). The 13 remaining failures are in:

  • Self-referential captures (3 tests)
  • local in code blocks (2 tests)
  • Multi-character case folding (2 tests)
  • Branch reset references (5 tests)
  • String interpolation edge case (1 test)

Coverage Philosophy

PetaPerl does not aim to run the entire perl5 test suite verbatim. Instead:

  • Port selectively: Focus on tests that cover features pperl implements
  • Write new tests: For pperl-specific features (JIT, parallelization, daemon mode)
  • Conformance over coverage: A smaller set of passing tests is better than a large set with many expected failures