The Japh Tradition
THE ONE-LINERLooks like someone sneezed on a keyboard, right? But run it and you get:print'4A75737420616E6F74686572205065726C206861636B6572' =~s~.{2}~"qq(\\x{$&})"~regex;
Run the code and you get:Just another Perl hacker
I will walk through each piece of the one-liner in order.
Part 1: THE HEX BLOB
That's not random garbage. It's hex-encoded ASCII.4A75737420616E6F74686572205065726C206861636B6572
That sequence is hex-encoded ASCII text.4A = J 75 = u 73 = s 74 = t 20 = (space) ...and so on
Each pair of characters maps to one letter. The entire blob spells out "Just another Perl hacker" once you decode it.
You can verify the decoding in Perl with this command:
perl -e 'print pack("H*", "4A757374")' # outputs: Just
Part 2: THE TILDE DELIMITERS
Normal regex uses slashes:A normal substitution uses slashes for delimiters:s/old/new/
But Perl lets you use almost any character as a delimiter. We use tildes:
Perl lets you pick almost any character as the delimiter instead. We use tildes here:s~old~new~
Why? Our replacement string has slashes and quotes in it. Tildes keep things clean. No escaping nightmare.
We choose tildes because the replacement string already contains slashes and quotes. Tildes keep the syntax clean and avoid an escaping nightmare.
Also looks cooler. Fight me.
It also reads a bit cleaner.
Part 3: THE PATTERN
Dead simple. Match any two characters. That's it..{2}
The pattern matches any two characters. That is the entire match rule.
So we're grabbing the hex blob two characters at a time:. = any character {2} = exactly two of them
The substitution walks the hex blob two characters at a time:4A, 75, 73, 74, 20, 61, 6E, 6F...
Each pair becomes one letter after we transform it.
Each pair turns into one letter once the substitution finishes.
Part 4: THE REPLACEMENT STRING
Here's where it gets spicy:The replacement string does the heavy lifting:"qq(\\x{$&})"
Let's unpack this onion.
I will unpack the pieces one at a time.
$& is a special variable. It holds whatever the regex just matched. So when we match "4A", $& contains "4A".
The variable $& holds the text the regex just matched. When the pattern matches "4A", $& contains "4A".
The replacement builds a string that looks like:
The replacement builds a string that looks like this:qq(\x{4A})
That double backslash (\\) becomes a single backslash in the output.
The double backslash collapses into a single backslash in the output.
So after the FIRST eval pass, we have:
After the first eval pass, the string becomes:qq(\x{4A})
Which is valid Perl code. A qq() string containing a hex escape.
Which is valid Perl code. A qq() string containing a hex escape.
Part 5: THE MODIFIERS (THE REAL TRICK)
Look at those modifiers. Now look again.~regex
They spell "regex". While being regex modifiers. In a regex.r - e - g - e - x
Your brain should hurt a little. That's normal..--. |o_o | |:_/ | // \ \ (| | ) /'\_ _/`\ \___)=(___/
But here's what they actually DO:
The x is just along for the ride. It's there to spell the word.r = Return the result (don't modify original string) e = Evaluate replacement as Perl code g = Global (do it for every match, not just the first) e = Evaluate AGAIN (the second e, hidden in "regex") x = Extended mode (allows whitespace in pattern - unused here)
Part 6: THE DOUBLE EVAL (ee)
This is the actual sorcery. Most people miss it.See those two e's in "regex"? That's double eval.
Here's how it flows for the hex pair "4A":
One e would just give you the literal text "qq(\x{4A})". Two e's actually runs it and converts the hex.MATCH: "4A" FIRST e (eval): Evaluates "qq(\\x{$&})" as code $& is "4A" Result: the STRING qq(\x{4A}) SECOND e (eval again): Evaluates qq(\x{4A}) as code Perl sees \x{4A} inside quotes Interprets it as hex escape Result: "J" OUTPUT: J
Part 7: PUTTING IT ALL TOGETHER
Step by step:print'4A75737420616E6F74686572205065726C206861636B6572' =~s~.{2}~"qq(\\x{$&})"~regex;
WHY "regex" INSTEAD OF "geer"?1. Start with hex blob 2. Match every two characters 3. For each pair, build qq(\x{XX}) 4. First eval creates the qq() string 5. Second eval interprets the \x{} escape 6. r modifier returns the transformed string 7. print sends it to screen Output: Just another Perl hacker
The normal way to write this would be:
Same result. But where's the fun in that?s~.{2}~"qq(\\x{$&})"~geer
Spelling "regex" with the modifiers is the whole point. It's self-referential humor. The modifiers describe what they are. It's a JAPH - it's supposed to make you smile when you figure it out.
The x modifier does nothing useful here. It's just completing the word. That's the joke.
TRY IT YOURSELF
Save this as japh.pl and run it:
Or one-liner it:#!/usr/bin/perl print'4A75737420616E6F74686572205065726C206861636B6572' =~s~.{2}~"qq(\\x{$&})"~regex;
(shortened hex for readability - try the full blob yourself)perl -e 'print q{4A7573742061}=~s~.{2}~"qq(\\x{$&})"~regex'
Homework: encode your own
Want to make your own message? Here's how to get the hex:Then wrap it in the same pattern. Welcome to the club.perl -e 'print unpack("H*", "Your message here")'
perl.gg_____ / \ | () () | \ ^ / ||||| ||||| "Now you know. And knowing is half the battle." - Some 80s cartoon, probably