This page shows how to build a typewriter effect in Perl using only a regex substitution with the e flag. The trick pairs Perl's pattern engine with code evaluation so each matched character runs a tiny piece of code before printing.
Part 1: THE SETUP - AUTOFLUSH
Before the substitution runs, you need to disable output buffering. Perl holds printed text in a buffer by default, which would print the whole string at once instead of character by character.This cryptic incantation sets the output autoflush flag. The flag tells Perl to flush the output buffer after every print call. The$|++;
++ operator just bumps the value from zero to one, which is a true value in Perl.
Part 2: THE MAGIC REGEX
Here's where the real wizardry happens:The substitution runs once for every character in the string, and each run executes the code block between the backticks.$text =~ s`.` select(undef, undef, undef, rand(0.05)); print $& `sger;
Part 3: THE PATTERN - MATCHING EVERYTHING
The pattern is a single dot, which matches any one character. Thes modifier at the end of the substitution changes how that dot behaves.
Without thes - Makes . match newlines too (normally it doesn't) g - Global matching - process EVERY character e - Evaluate the replacement as Perl code r - Return the result instead of modifying in-place
s modifier, the dot would not match newline characters, so line breaks would skip the delay. The modifier forces the dot to match newlines too, so every character in the string gets the typewriter treatment.
Part 4: THE REPLACEMENT - CODE AS TEXT
Thee flag is where Perl gets magical. Instead of a literal replacement string, we're running actual code for each match:
Theselect(undef, undef, undef, rand(0.05)); print $&
select() call is a standard Perl way to sleep for a fraction of a second. The rand(0.05) argument gives a random delay between zero and fifty milliseconds, which creates that authentic mechanical typewriter feel. Some keys print faster, some keys print slower.
The $& variable holds whatever the regex just matched. In this case, it holds one character at a time. We print it, pause, print the next character, pause, and repeat until the string is done.
Part 5: PUTTING IT ALL TOGETHER
Here's a complete working example:Run it and watch your text appear character by character with realistic timing variations.#!/usr/bin/perl use strict; use warnings; $|++; # Autoflush! my $text = "Hello, world!\nThis is a typewriter effect.\n"; $text =~ s`.` select(undef, undef, undef, rand(0.05)); print $& `sger;
Part 6: WHY THE BACKTICKS?
You might have noticed I used backticks as the regex delimiter instead of the usual forward slashes. This is purely stylistic. When your replacement contains a lot of code, using different delimiters can make things more readable. Perl lets you use almost any paired characters as delimiters.These are all equivalent:
Pick whatever makes your code clearest.s/pattern/replacement/ s`pattern`replacement` s{pattern}{replacement} s|pattern|replacement|
This technique showcases what makes Perl special. The language lets you blur the line between data and code, between matching and transforming. A simple regex becomes a complete animation system.
Happy typing!
perl.gg