Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Token Abuse Japh

by chargrill (Parson)
on Feb 10, 2006 at 21:15 UTC ( #529472=note: print w/ replies, xml ) Need Help??


in reply to Token Abuse Japh

I was also surprised that $ = was legal. Unfortunately,

perl -MO=Deparse

spoiled that fun, as well as teaching me to look out for that in the future :)

Being thoroughly impressed by this (as well as the rest of liverpole's JAPH's, I thougt I'd go ahead and post a detailed de-obfuscation of this particularly clever (to me, at least) JAPH.

First, the obfuscated code:

#!/usr/bin/perl + $ == _ eq _; $ -= ( ) = qw; ( );; $ *= $ -* $ -* $- ; $ /= $ -+ $ *; _: $ ".= chr $ /+ $ *** $ -; $ /= $ / >> $ = || $ ** $ -; $ /= $ / &~ $ */ $ - and goto _; + print $ "," "

Pretty confusing! But let's run it through -MO=Deparse, and we get:

$= = '_' eq '_'; $- = () = ('(', ')'); $* = $- * $- * $-; $/ = $- + $*; _: $" .= chr $/ + $* ** $-; $/ = $/ >> $= || $* * $-; $/ = $/ & ~$* / $- and goto _; print $", "\n";

Still pretty confusing, but at least now I can parse it a little better.

$= = '_' eq '_';

$= normally defaults to 60 as the current page length of the selected output channel, but this just sets it to 1.

$- = () = ('(',')');

$- = () = qw; ( );; is like $- = () = ('(',')');

('(',')') is a two element list, and the =()= idiom Perl Idioms Explained - my $count = () = /.../g shows that $- receives the count of elements in the list, in other words 2.

$* = $- * $- * $-;

Equivalent to $* = 2 * 2 * 2, or 8.

$/ = $- + $*;

Sets $/ to 10.

_: $" .= chr $/ + $* ** $-;

_: is a line label, and I thought (as Errto noted above) it had to be at the begining of a line. $=, the list separator for list values interpolated into a double quoted string, defaults to a single space. The statement is equivalent to $" =  " " . chr 10 + 8 ** 2 , and ** binds tighter than + , therefore $" = " " . chr 10 + 64, or rather
$" = " J".

$/ = $/ >> $= || $* * $-;

This is: $/ = 10 >> 1 || $* * $-;

10 >> 1 will evaluate true (non-zero), so the right half doesn't gets evaluated, and $/ is subsequently set to 5.

$/ = $/ & ~$* / $- and goto _;

This can be written as $/ = 5 & ~ 8 / 2 , or a bit more clearly, $/ = 5 & ( ( ~ 8 ) / 2 ). This has the net effect of setting $/ to 1.

Going to the "_" line label, $= (currently " J") has chr 1 + 64, or "A" appended to it. then 1 >> 1 evaluates to 0, so this time the right hand of the expression is evaluated, and $/ is set to 8 * 2, or 16. Then $/ = $/ & ~ $* / $- can be restated as $/ = 16 & ( ( ~ 8 ) / 2 ), and sets $/ to 16.

Back to the "_" line label. $= (currently " JA") has chr 16 + 64, or "P" appended to it. 16 >> 1 is 8, so $/ is set to 8. and 8 & ( ( ~ 8 ) / 2 ) evaulates to 8.

Back to the "_" line label, $= (currently " JAP") has chr 8 + 64, or "H" appended to it. Then 8 >> 1 is 4, so $/ is set to 4. And what I think is the really clever "bit" ;) is that now 4 & ( ( ~ 8 ) / 2 ) evaluates to 0, so and goto  _; is skipped, proceeding to:

print $", "\n";

... which I think is pretty self-explanatory.

Whew! ++liverpole! That was a lot of fun!



--chargrill
$/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );


Comment on Re: Token Abuse Japh
Select or Download Code
Replies are listed 'Best First'.
Re^2: Token Abuse Japh
by liverpole (Monsignor) on Feb 10, 2006 at 22:52 UTC
    Thank you, chargrill ++ a delightful read.

    I'm glad you referenced Perl Idioms Explained - my $count = () = /.../g, because I actually hadn't intended to use it; I just came to a point where I realized I needed it.  So it was serendipitous that I had just read that node the other day.


    @ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://529472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (16)
As of 2015-07-29 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls