Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Pe(a)rls in line noise

by Roger (Parson)
on Sep 15, 2003 at 06:28 UTC ( #291495=note: print w/replies, xml ) Need Help??

in reply to Pe(a)rls in line noise

Hi Liz, this is my attempt at decoding this clever JAPH, in fact my first attempt at decoding any JAPH, so please correct (and forgive) my analysis if wrong!

@; = (491036,8051555,4785250,3341086,912133,189089,691108);
# This is just an eye candy, an array placeholder of 7 elements
%; = qw/ He didn't,Just do to,Perl didn't understand,another it would,hacker! /;
# This constructs the %; hash variable with 4 entries, namely He, do, didn't, it:
He => didn't,Just do => do,perl didn't => understand,another it => would, hacker!
%: = qw/ ================================= ================================== Randal said it would be tough to do in sed. He didn't say he didn't understand sed. Randal understands sed quite well. Which is why he uses Perl. :-) -- Larry Wall in <7874@jpl-devvax.JPL.NASA.GOV> ================================= ================================== /;
This builds another hash lookup - %:
{ # required by redo, where to start for the redo /./ ? eval(join 't, ', qw:prin exi delphi:) : /./;
This is really (translated to):
/./ ? { print, exit, delphit, } : /./;
or just
/./ ? { print, exit, } : /./;
Clever, the first time this loop is enterred, $_ is undef, so the print and exit is not performed. delphit would have no effect (after the exit).
$:{do} = 'to';
# Another clever bit, insert the required 'do=>to' lookup pair into the hash table. How this is used will be explored later.
@; = map { join ',', '', $_, '' } values(%;);
Translates the values to the %; hash to ",xxx,", required by the lookup below
This translation is clever:
The key-value pairs in the %: hash so far
He => didn't didn't => understand do => to # manually inserted before it => would
And then... Modify the hash table %:, with values from the %; lookup.
eval '$:{q?'.$_.'?}=~s'.shift@; for keys(%;);
So that:
He => Just didn't => another do => Perl it => Hacker!
This next bit just creates the string to be printed. :-)
$_ = qq{@{[@:{qw{ He didn't do it }}]}};
The next bit goes back to the beginning of the loop, with $_ set to "Just another Perl hacker!", and of cause the print, exit, code will take over from there...
redo } x:

Replies are listed 'Best First'.
Re: Re: Pe(a)rls in line noise
by liz (Monsignor) on Sep 15, 2003 at 08:12 UTC first attempt at decoding any JAPH...

    Well, you're in good company then, because this was the first JAPH I ever made myself. Honest!

    @; = (491036,8051555,4785250,3341086,912133,189089,691108);
    Yep, that's just eye candy. There have been some JAPH's recently that used magical increment for an X number of times, and I was going for similarly looking numbers. The:
    ================================= ==================================
    and at the end: x: are also just eye candy. I tried several Larry Wall quotes while building this japh, and the line with '=====' made it possible for me to easily switch between keys and value in the hash (change the space between the "==="'s to "=" and all keys are values and vice versa in the hash).

    $:{do} = 'to';
    was actually a kludge. Originally I had the "to do" exchanged in the quote. But I found the error in the quote too obvious.

    eval '$:{q?'.$_.'?}=~s'.shift@; for keys(%;);
    This actually works because whatever order hash keys have in a hash, the order returned by the "keys()" and "values()" functions is the same. And for some reason, I _had_ to put parentheses with keys() and values() because otherwise I was getting the warning that the call was ambiguous. That's still on my list to check out later.

    $_ = qq{@{[@:{qw{ He didn't do it }}]}};
    is what you call an interpolated anonymous hash slice ;-) Working from the outside in: the qq{...} creates an interpolated string, which will put spaces between the elements because $" was not changed. The @{...} dereferences an array reference. The [...] create a reference to an array. The @:{...} creates a hash slice on %:. And finally the qw{...} creates the array of keys for the hash slice.

    ...cause the print, exit, code will take over from there...

    I don't know if that is what you meant to say, but after the "exit" there is no code to execute anymore. So the "x:" is never reached, it's just eye candy.


      Hi Liz, thanks for your reply and detailed analysis!

      The biggest benefit I got from this is the anonymous hash slice bit. Normally I would have written something that joins the values of a hash as

      $_ = join " ", map { $:{$_} } qw/ He didn't do it /;

      Now I have learnt an ideomatic way to do it. Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://291495]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-24 14:47 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.