Perl: Pure Perl version of xxHash64
I whipped up a pure Perl version of xxHash64. Written in a way to make it easy to copy and paste into another script. For any serious use of xxHash please use Crypt::xxHash.
Tags:I whipped up a pure Perl version of xxHash64. Written in a way to make it easy to copy and paste into another script. For any serious use of xxHash please use Crypt::xxHash.
Tags:I need a quick way to measure milliseconds of duration in a Perl script. This is what I came up with:
$st = stopwatch(); # Start the stopwatch
$x = some_slow_code();
$ms = stopwatch(); # Return milliseconds since start
# Stopwatch function: returns milliseconds
sub stopwatch {
require Time::HiRes;
state $start = 0;
if (!$start) {
$start = Time::HiRes::time();
return $start;
} else {
my $ret = (Time::HiRes::time() - $start) * 1000;
$start = 0; # Reset the start time
$ret = int($ret);
return $ret;
}
}
See also: micros()
Tags:If you use Bash at the command-line you have probably found a need for some shortcut keys. If you have a long command like:
ls -la --size /some/long/path/to/file.txt
Often in a follow up command you will want to reference the previous command's arguments. In this example if you want to reference the file from the previous command you either have to type it all out again, copy and paste the previous argument, or use a Bash shortcut.
| Shortcut | Description |
|---|---|
!! |
entire previous command |
!$ |
last argument of previous command |
!^ |
first argument of previous command |
!:2 |
second argument |
!:1-3 |
arguments 1 through 3 |
!* |
all arguments |
You could use the !$ shortcut to reference the last parameter from the previous command
vim !$
This is a lot less typing, and quicker. The ^ and $ also line up nicely with the logic from regular expressions: first and last. Another real world example that I run into is creating a directory and then changing into it:
mkdir /var/tmp/user/path
cd !$
Tags:
I'm a big fan of YAML and use it extensively with PHP. The native YAML extension requires the ability to install a system-level library, which isn't always possible in shared hosting or restricted environments. To address this, I wrote a pure-PHP polyfill that provides the same functionality without requiring any system-level dependencies. The polyfill is a single file with no dependencies. This makes it very easy to install.
Simply require_once() the polyfill to gain access to the yaml_parse() and yaml_emit() functions. The polyfill also plays nicely with the native extension, so it's safe to include in your project even when the native YAML library is already installed.
Just came across this quick command to show running services.
systemctl list-units --type=service --state=running
Tags:
Splitting a string on a delimiter, such as a comma or semicolon, is a common task. However, if the delimiter appears inside a quoted string, you may need to preserve it rather than split at that point. I wrote a simple function that handles this scenario and covers the majority of common use cases.
quote_split('foo, bar, baz'); # ('foo', 'bar', 'baz')
quote_split("foo, 'bar, baz'"); # ('foo', 'bar, baz')
# Split a string on commas, but respect single and double quotes with commas
sub quote_split {
my ($str, $separator) = @_;
$separator //= ",";
if (!length($separator)) {
die("quote_split separator cannot be empty\n");
}
my $separator_re = quotemeta($separator);
my @items;
while ($str =~ /\G\s*(?:
"((?:\\.|[^"\\])*)" # Double-quoted
| '((?:\\.|[^'\\])*)' # Single-quoted
| ((?:(?!$separator_re)[\s\S])+) # Unquoted
)\s*(?:$separator_re|\z)/gcx) {
my $item = $1 // $2 // $3;
push(@items, $item);
}
return @items;
}
Tags:
I need very basic YAML parsing in Perl, mostly for reading configuration files. There aren't any great options in Perl core, and I only need a subset of YAML (basic scalars, arrays, and hashes), so I worked up a simple copy/pasteable function.
my $x = yaml_parse($yaml_str);
This implementation supports: nesting, scalars, arrays, and hashes, but is missing null / ~, booleans, and complex quotes.
sub yaml_parse {
return {} unless defined $_[0] && length $_[0];
my %data; my $root = \%data; my @st = ([-1, \$root]);
for my $line (split /\n/, $_[0]) {
$line =~ s/\r$//;
next if $line =~ /^\s*(?:$|---\s*$|#)/;
if ($line =~ /^(\s*)-\s*(.*)$/) {
my ($n, $v) = (length($1), $2);
pop @st while $n < $st[-1][0]; my $cur = ${$st[-1][1]};
die "yaml_parse: array without parent: $line" if @st == 1;
$cur = ${$st[-1][1]} = [] if ref $cur eq 'HASH' && !%$cur;
die "yaml_parse: mixed array/hash" if ref $cur ne 'ARRAY';
$v =~ s/^\s+|\s+$//g; $v =~ s/^(['"])(.*)\1$/$2/s;
$v += 0 if $v =~ /^-?\d+(?:\.\d+)?$/; if ($v =~ /^([\w\-\.\/]+):\s*$/) {
push(@$cur, my $el = {$1 => {}}); push(@st, [$n + 2, \$el->{$1}]); next; }
push(@$cur, $v);
} elsif ($line =~ /^(\s*)([\w\-\.\/]+)\s*:\s*(.*)$/) {
my ($n, $key, $v) = (length($1), $2, $3);
pop @st while $n <= $st[-1][0]; my $cur = ${$st[-1][1]};
die "yaml_parse: '$key' under array" if ref $cur eq 'ARRAY';
$v =~ s/\s+$//; $v =~ s/^\s+//; $v =~ s/^(['"])(.*)\1$/$2/s;
if ($v =~ /^\[(.*)\]$/) {
my @a = grep { length } map { s/^\s+|\s+$//gr =~ s/^(['"])(.*)\1$/$2/sr } split /,/, $1;
for (@a) { $_ += 0 if /^-?\d+(?:\.\d+)?$/ } $v = \@a;
} else { $v += 0 if $v =~ /^-?\d+(?:\.\d+)?$/; }
if (ref $v or length $v) { $cur->{$key} = $v }
else { $cur->{$key} = {}; push(@st, [$n, \$cur->{$key}]) }
} else { warn "yaml_parse: ignoring: $line\n" }
} return \%data;
}
YAML::XS is definitely the best Perl YAML parser, but it can be overkill if all you need is simple parsing.
Update: I wrote some unit tests to go along with this.
Tags:I ported my Sluz templating engine from PHP to JavaScript. It's now possible to run a full Sluz installation entirely in-browser. As a proof-of-concept I ported the Sluz Sandbox to use the JS version of library. Now you can test and validate Sluz code 100% in-browser.
Tags:AI models like Claude Opus and ChatGPT 5.6 are called "frontier" (i.e. top of the line). There are several fully open source frontier level AI models that are available to download for free. I ran the specs on what it would take to run a frontier level model.
For inference of a frontier model (400B-1T+ params):
- Minimum (4-bit quantized): ~200-500GB VRAM -> 4-8x H100 80GB GPUs
- FP16/half precision: ~800GB-2TB VRAM -> 10-32x H100 80GB GPUs
- System RAM: 512GB+
- Interconnect: NVLink or InfiniBand between GPUs
- Storage: Several hundred GB for model weights For training: 10,000-100,000+ GPU-hours on H100-class hardware, multi-million dollar cluster, weeks of continuous run time. No single consumer GPU can run a frontier model - even an RTX 4090 (24GB) is about 10-30x short of VRAM needed for even a heavily quantized frontier model.
If you just want to host a "good" AI model it requires 8x GPUs with 80GB of VRAM each.
It's about $300k to get in the door for a SINGLE server to run AI you can ask questions
Tags:Another day, another PRNG ported to Perl. Today is romuduojr from romu-random.org. Pretty simple 64bit PRNG.
Tags:I have a small Perl script that defines a handful of functions. Perl allows you to make a library that doubles as a script when called directly. Using caller() you can determine if your script was loaded via a require() call, or called directly. This allows you to export functions if called as a library, but run code if called directly via: perl my_lib.pl.
# my_lib.pl
if (!caller()) {
say greet("Scott");
}
sub greet {
my $name = shift();
return "Hello $name";
}
1; # Required if you load as a library
Use a require() call to load the module and get access to the greet() function.
# main.pl
require("/path/my_lib.pl");
say greet("Foo");
Tags:
I have a Git repository with a primary branch that I maintain actively. I also have a separate feature branch that I'd like to keep in sync with the main branch, and alert if there are any conflicts immediately. Using Git hooks you can script automatically pulling each new commit from the primary branch to the feature branch.
Create a .git/hooks/post-commit file and put these contents in it:
#!/bin/sh
SRC="main"
DST="feature"
branch=$(git branch --show-current)
if [ "$branch" = "$SRC" ]; then
commit=$(git rev-parse HEAD)
git checkout $DST &&
git cherry-pick "$commit" &&
git checkout main
fi
If there are conflicts, Git errors out immediately and leaves you on the feature branch to manually resolve the conflict.
Tags:I have two directories with (mostly) the same content that I want to keep in sync. Specifically I want to make sure that the newest version of each file is synced to the other directory. This allows me to update a file on either side, and that version will propagate to the other. You can do this with a bi-directional rsync command:
rsync --update -av /dir/a/ /dir/b/
rsync --update -av /dir/b/ /dir/a/
Using --update tells rsync to skip files on the receiving side that are newer. If you sync a -> b and then b -> a you end up with both locations having the newest copy of each file.
ULID's are an interesting way to generate globally unique identifiers. Here is a quickie Perl implementation to generate ULIDs. This implementation does not include the intra-millisecond monotonic increment however. If that feature is important to you consider checking out a more full-featured implementation like ULID::Tiny.
for (1 .. 5) {
say(ulid());
}
sub ulid {
my $ts = $_[0] // time() * 1000;
my $bytes = substr(pack("Q>", $ts), 2, 6);
# Append 10 random bytes
$bytes .= pack 'C*', map { int(rand(256)) } 1 .. 10;
# base32 encoding
my $bits = '00' . unpack('B*', $bytes);
# Chars to use for base32
my @chars = split(//, '0123456789ABCDEFGHJKMNPQRSTVWXYZ');
my $result = '';
for (my $i = 0; $i < 130; $i += 5) {
my $index = oct('0b' . substr($bits, $i, 5));
$result .= $chars[$index];
}
return $result;
}
See also: UUIDv7
Tags: