http://www.perlmonks.org?node_id=270026


in reply to Unlocking the Dark Mysteries of Acme::Bleach

Line 11 seems crucial: the content of $shirt is evaluated (i.e, executed) if the source code is not dirty (bleached or whitened). But why does he also test if the $shirt is dressed? I don't see the purpose of the dress subroutine.

Im pretty sure the dress routine checks to see if a header value ($tie) has be prefixed to the data following the use statement. (Note the $tie.$_ as the last statement of whiten()) Its used to determine if the use Acme::Bleach; is at the top of file that needs to be bleached and then printed out, or if its at the top of a file that needs to be brightened (as in its already been whitened) and then executed.

dirty() tells him if the text below the use line contains non spaces (thus indicating its not been compressed), and dress() tells if the data contains the header value. If the data is both dirty() and not dress()ed then it cant have been bleached already.

Why does he use "local" so much?

Because hes messing around with $_ and regexes. Thus its easier to say

sub foo { local $_=shift; s/.../.../g; }

than to say

sub foo { my $str=shift; $str=~s/.../.../g; }

I believe that in lines 12 and 13, the source code is encoded (bleached), BUT why does he print "use Acme::Bleach" before he whitens?

As I mentioned before the use line cause both compression and decompression. So of course it has to be output. Furthermore if he output the use line encoded perl would just ignore the whitespace.

BTW: I personally think it pays to decompress his code a bit

package Acme::Bleach; $VERSION = '1.12'; my $tie = " \t" x 8; sub whiten { local $_ = unpack "b*", pop; tr/01/ \t/; s/(.{9})/$1\n/g; $tie . $_; } sub brighten { local $_ = pop; s/^$tie|[^ \t]//g; tr/ \t/01/; pack "b*", $_; } sub dirty { $_[0] =~ /\S/; } sub dress { $_[0] =~ /^$tie/; } open 0 or print "Can't rebleach '$0'\n" and exit; ( my $shirt = join "", <0> ) =~ s/.*^\s*use\s+Acme::Bleach\s*;\n//sm; local $SIG{__WARN__} = \&dirty; do { eval brighten $shirt; exit } unless dirty $shirt && not dress $shirt; open 0, ">$0" or print "Cannot bleach '$0'\n" and exit; print {0} "use Acme::Bleach;\n", whiten $shirt and exit;

HTH


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...