@JAPH[3]

Let's do a few simple ones. I found some music to listen to as we go.


$_='x"Just ";"x\"another \";\'x\\"Perl \\";x\\"hacker,\\"\'"';
s/x/print/g; eval eval eval;

Pretty straightforward. We see that print is being substituted for x in the $_ string. eval? is then used to tell the interpreter to treat the string like code. Can you see why multiple evals are required? It's a bit of a brain twister. Keep in mind that eval only returns the last thing it evaluated. Here's a hint: Using just print eval returns:

Just print"another ";'print"Perl ";print"hacker,"'

and print eval eval returns:

Just another print"Perl ";print"hacker,"

There's a good explanation of it here if you're stuck.



Next up we have:

$Old_MacDonald = q#print #; $had_a_farm = (q-q:Just another Perl hacker,:-);
s/^/q[Sing it, boys and girls...],$Old_MacDonald.$had_a_farm/eieio;

This rather humorous JAPH by merlyn makes extensive use of q, which can be used to define strings. Placing q in front of a special character, like # or %, causes it to behave like '. If the character is a bracket, then the string will also end if the opposite bracket is matched. Note that - and : are used in tandem to create the :-) emoticon that Larry Wall is so fond of.
On the first line we see that two strings are defined. The second line contains a substitution regex, which matches the beginning of the string. What string? Well, $_ of course. But $_ is blank (or undefined, rather), so its value is simply set to the latter half of the regex.
The replacement is a string followed by a comma followed by a concatenation. The key is the e modifier, which evaluates the resulting string. Note that two are necessary here, because just one results in:

print q:Just another Perl hacker,:

The other modifiers are just for comedic effect. I get the feeling that it was this combination of modifiers that inspired the JAPH in the first place.



Okay, okay, one more.

@a=split(/(\d)/,"4Hacker,2another3Perl1Just");
shift(@a);%a=@a;print"@a{1..4}";

We've seen split before; here it's splitting the string whenever it matches a digit. Then shift? is applied to the resulting array, because the first element will be blank. Next, the array is converted to a hash. This means that the words and their corresponding numbers will be paired up as hash values and keys. So the final print statement prints the strings that correspond to the numbers 1 through 4.

That was a little brief because I don't have a great grasp of hashes yet :/
Next time I'll cover my most recent JAPH and we'll do some golfing.

Leave a Reply

Theme: Esquire by Matthew Buchanan.