-
Notifications
You must be signed in to change notification settings - Fork 0
perl Namespace
Derek Snider edited this page May 19, 2026
·
1 revision
21 Perl-style string and array functions. Features chop/chomp, regex-powered grep and split, file glob, and array manipulation.
See also: Namespaces Overview | Other namespaces
| Function | Description | Example |
|---|---|---|
chop(str) |
Remove last character, return it as int | ch = perl::chop(s) |
chomp(str) |
Remove trailing newline(s), return count | n = perl::chomp(s) |
lc(str) |
Lowercase entire string | perl::lc(s) |
uc(str) |
Uppercase entire string | perl::uc(s) |
ucfirst(str) |
Capitalize first character | perl::ucfirst(s) |
lcfirst(str) |
Lowercase first character | perl::lcfirst(s) |
reverse(str) |
Reverse string in place | perl::reverse(s) |
index(str, substr) |
Find first position (-1 if missing) | i = perl::index(s, "foo") |
rindex(str, substr) |
Find last position (-1 if missing) | i = perl::rindex(s, "foo") |
length(str) |
String length | n = perl::length(s) |
substr(result, str, offset, len) |
Extract substring | perl::substr(r, s, 2, 5) |
| Function | Description | Example |
|---|---|---|
split(arr, delim, str) |
Split string by regex delimiter | perl::split(a, ":", path) |
join(result, sep, arr) |
Join array into string | perl::join(s, ",", a) |
push(arr, str) |
Append element | perl::push(a, "val") |
pop(result, arr) |
Remove + return last element | perl::pop(s, a) |
shift(result, arr) |
Remove + return first element | perl::shift(s, a) |
unshift(arr, str) |
Prepend element | perl::unshift(a, "val") |
scalar(arr) |
Number of elements | n = perl::scalar(a) |
grep(dest, pattern, src) |
Filter array by regex match | perl::grep(matches, "err", lines) |
glob(arr, pattern) |
File globbing | perl::glob(files, "*.mad") |
splitandgrepusestd::regexinternally. If the pattern is not a valid regex, they fall back to substring matching.
| Operation | Perl | PHP |
|---|---|---|
| Remove last char | perl::chop(s) |
php::chop(s) (strips trailing whitespace!) |
| Split string | perl::split(a, d, s) |
php::explode(a, d, s) |
| Join array | perl::join(r, s, a) |
php::implode(r, s, a) |
| Count elements | perl::scalar(a) |
php::count(a) |
#include <iostream>
int main()
{
// grep: filter log lines
array lines;
php::array_push(lines, "INFO: all good");
php::array_push(lines, "ERROR: disk full");
php::array_push(lines, "INFO: started");
php::array_push(lines, "ERROR: timeout");
array errors;
string pattern = "ERROR";
perl::grep(errors, pattern, lines);
// errors now contains the 2 ERROR lines
// glob: find test files
array tests;
string glob_pat = "tests/test*.mad";
perl::glob(tests, glob_pat);
return 0;
}