Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: fun with subroutines

by LAI (Hermit)
on Apr 16, 2003 at 16:52 UTC ( [id://250968]=note: print w/replies, xml ) Need Help??


in reply to fun with subroutines

Okay, I'll give this a shot. My first longish obfu deconstruction:

Here's the obfu, deparsed:

sub a { my(%v) = map({$w[$_], $_;} @w); my $n; sub { $v{$b} <=> $v{$a}; ++$n; } ; } sub _ { my $v = shift @_; sub { $\ = $v; $_ = ''; } ; } @w = sort({&a->($a, $b);} map({_ $_;} split(//, "erl hc\nker.aPust ano +ther J", 0))); foreach $_ (@w) { &$_; print $_; } testfile_orig syntax OK

The first thing that happens is a funny map. A string get split up and passed through map, passing each letter through sub _. This returns a coderef (more on that one later), so that we have at the end of the map an array of coderefs.

Next the array gets "sorted". Funny thing, though -- the sort routine always returns 1. This is where the thing gets further into internals than I can get, but maybe someone can clarify this bit in my stead. It seems to rely on the internals of sort, and how it deals with ordering stuff. It is fooled into always thinking that $a > $b.

Anyway, @w is then iterated over, dereferencing the coderef each time (told you I'd get to it). This sets the output record separator to whichever letter was originally mapped in, then sets $_ to an empty string. The empty string is printed, then the ORS, printing out the whole string.

LAI

__END__

Replies are listed 'Best First'.
Re: fun with subroutines
by jonadab (Parson) on Apr 16, 2003 at 17:18 UTC
    Yes, that's it pretty much exactly.
      Playing with it was inspiring, and i attempted to roll my own a bit. Very fun!

      For portabilities sake you might want to use sort '_qsort', which fixed it for me, if perl 5.8.0. It's 3am, and i've just had one of the weirdest sober nights in my life, so i'm in no condition to think of how to selectively load pragmatic modules right now.

      ++ once more... and good night.

      -nuffin
      zz zZ Z Z #!perl

        I've found a more portable way to abuse sort: use a sort function that does result in a defined order, but obfuscate it. For a (quite rudamentary) example, see my new signature:

        {my$c;$ x=sub{++$c}}map{$ \.=$_->()}map{my$a=$_->[1]; sub{$a++ }}sort{_($a->[0 ])<=>_( $b->[0])}map{my@x=(& $x( ),$ _) ;\ @x} split //, "rPcr t lhuJnhea eretk.as o";print;sub _{ord(shift)*($=-++$^H)%(42-ord("\r"))};

        Of coures, for serious obfuscation something much more obscure than modular arithmetic would be called for in the sort function, but this is just a JAPH.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found