Self-Modifying Code in Perl

By: w1ldc4rd-w1z4rd
[ BACK... ]

Today, we’re diving into a fascinating and somewhat esoteric corner of Perl programming: self-modifying code. While not commonly used in production environments, self-modifying code can be an interesting exercise in understanding Perl’s capabilities.

Let’s examine this intriguing script:

#!/usr/bin/env perl

use feature 'say';

say "Count: 0";

$/=undef;

my $self = do { local @ARGV = ($0); <> };

$self =~ s~Count: \K\d+~$& + 1~e;

@ARGV = ($0);
$^I = qq|.${\time}.bak|;

say $self while <>;

This script does something quite unusual - it modifies its own source code each time it runs. Let’s break it down:

We start by printing Count: 0.

$/=undef sets the input record separator to undef, allowing us to read the entire file at once.

The script reads its own content into $self:

my $self = do { local @ARGV = ($0); <> };

Here, $0 is the name of the script file.

It then uses a substitution to increment the count:

$self =~ s~Count: \K\d+~$& + 1~e;

The \K in the regex discards everything matched so far, and the e flag evaluates the replacement as Perl code.

Finally, it sets up in-place editing with a backup:

@ARGV = ($0);
$^I = qq|.${\time}.bak|;

This creates a backup file with the current timestamp.

The modified content is then written back to the file:

say $self while <>;

Each time you run this script, it will increment the count in its own source code and create a backup of the previous version.

While this is a clever demonstration of Perl’s power, it’s important to note that self-modifying code can be hard to maintain and debug. It’s generally best avoided in production code. However, understanding how it works can deepen your knowledge of Perl’s file handling and regex capabilities.

Remember, with great power comes great responsibility. Use wisely!

Copyright ©️ 2024 perl.gg