@JAPH[1]

Okay boys and girls, time for another JAPH. This one will be a little trickier, but conceptually very similar to the previous one. Here it is:

$_="krJhruaesrltre c a cnP,ohet";
$_.=$1,print$2 while s/(..)(.)//;

Like we've seen before, this JAPH makes use of the $_ variable. You'll find that JAPHs or otherwise obfuscated/optimized code love using the $_ variable because it allows you to use functions like print and regex substitutions without having to pass in an argument.

The first line is a string of gibberish, but again there are some clues as to its true nature: the J and P are capitalized, there's a comma present, and there are 27 characters in total.

Wait, 27? Just Another Perl Hacker, only has 24. So there is more to this JAPH than just a simple reordering.
Looking at the second line, we see the .= operator. . is the string concatenation operator, so .= works just like += in that it's simply shorthand for $_ = $_ . . Here it appears that we are concatenating $_ and $1, which is another special variable in Perl. $[digit] contains the result of a regex pattern. If you look at the end of the line, you'll see a regex with two subpatterns, (..) and (.). So $1 matches the first one and $2 matches the second. We'll figure out what those patterns actually mean later.

Next comes a comma, which seems rather strange, because normally we would expect a semicolon. The comma operator? evaluates the left side, ignores the return value, and then evaluates and returns the statement on the right side.
The right side in question is a print statement and a while loop. Actually, it's a do while loop, because the conditional comes after the loop body. And the conditional in this case is a regex! This regex will return 1 if there's a match and 0 if there isn't. So this loop will terminate only when the regex doesn't match anything. So what exactly is the regex looking for?

In fact, it's not just looking. This is a substitution regex (as denoted by s), so whatever it finds will be replaced by what's between the second and third slashes, which is nothing. So its more of a deletion than a replacement.
Alright, enough stalling. What is being replaced or deleted or whatever? Well, in regex, a . matches any character. So the first subpattern matches groups of two characters, and the second subpattern matches any single character. So lets step through and see what this code is actually doing.

First, the regex will match krJ and delete it. Then kr will be appended to the end of the string, and J will be printed. On the second iteration, hru will be deleted, hr will be appended, and u will be printed. Do you see now why the concatenation is necessary? It essentially allows the regex to "loop" around the string. And you might be able to see why our gibberish string has 27 characters, too. Here's what happens to $_ as the code runs, with $2 enclosed in pipes:


kr|J|hruaesrltre c a cnP,ohet
hr|u|aesrltre c a cnP,ohetkr
ae|s|rltre c a cnP,ohetkrhr
rl|t|re c a cnP,ohetkrhrae
re| |c a cnP,ohetkrhraerl
c |a| cnP,ohetkrhraerlre
 c|n|P,ohetkrhraerlrec
P,|o|hetkrhraerlrec  c
he|t|krhraerlrec  cP,
kr|h|raerlrec  cP,he
ra|e|rlrec  cP,hekr
rl|r|ec  cP,hekrra
ec| | cP,hekrrarl
  |P|,hekrrarlec
,h|e|krrarlec c
kr|r|arlec c,h
ar|l|ec c,hkr
ec| |c,hkrar
c,|h|krarec
kr|a|recc,
re|c|c,kr
c,|k|rre
rr|e|c,
c,|r|r
rc|,|

 ////
 ///
 //
 /

Pierce the heavens.

Leave a Reply

Theme: Esquire by Matthew Buchanan.