For true-blue Perl geeks like myself, the Perl Monks web site is an interesting place which is really worth a regular visit. They have this section called Obfuscated code in which you can discover amazing little jewel snippets. Like this one, for example:
So what does it do? How does it work? What does it mean?
You now have two choices: 1/ Take the challenge, figure it out yourself, and verify you have the correct answer by clicking the button below, or 2/ Be impatient, immoral, and outcasted from the Perl community by hitting the answer button without raising a pinkie.
1.What does it do?
Prints the following message: "Just another Perl Hacker".
2.How does it work?
In order to understand this, you first need to know what AUTOLOAD is. Simply stated, if Perl sees a subroutine that it cannot resolve, it first looks in the current file to see if a subroutine called AUTOLOAD exists. If it does, then it will call it, passing the name of the unknown subroutine (as $AUTOLOAD) along with the parameters (as @_).
In the code above, the author is just trying to confuse us a bit more by pointing AUTOLOAD to the subroutine _().
You also need to understand the meaning of certain special variables used by Perl, which are listed in the following table:
symbol
meaning
$&
Last successful pattern match (/:+/)
$`
String preceding match
$'
String following match
$_
Hey, figure this out yourself
$,
Output field separator (default ",")
Now we can put all of the pieces together, and show what happens at each step of the journey, e.g. when the AUTOLOAD is called and what it is doing to these special characters before the print statement takes place.
called
$&
$`
$'
$_
$,
1.
::
main
Just
main::Just
Just
2.
::
main
another
main::another
Just another
3.
::
main
Perl
main::Perl
Just another Perl
4.
::
main
Hacker
main::Hacker
Just another Perl Hacker
You should now have enough information to figure out the rest. If not, then I suggest you run on down to the nearest bookstore right away and purchase the famous "camel book", even if it means scraping up the very last cent you can find.
3.What does it mean?
I leave that up to you as an extra exercise for late-evening entertainment some time. Sorry to disappoint you. Perhaps I will be nice and reveal the answer in a later blog entry.