Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Unlocking the Dark Mysteries of Acme::Bleach

by demerphq (Chancellor)
on Jun 29, 2003 at 21:36 UTC ( [id://270026]=note: print w/replies, xml ) Need Help??


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...

Replies are listed 'Best First'.
Re: Re: Unlocking the Dark Mysteries of Acme::Bleach
by Anonymous Monk on Jun 29, 2003 at 22:46 UTC
    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.

    What? How can a Perl script be executed without the hash line (#!/usr/bin/perl)? I sorta understand the first bleaching, but how can the script work on subsequent occasions without the hash line?!?

      Blink. Blink. Blink.

      What? How can a Perl script be executed without the hash line (#!/usr/bin/perl)? I sorta understand the first bleaching, but how can the script work on subsequent occasions without the hash line?!?

      Where to start. Ok, at the Acme::Bleach level, he doesnt care. The file hes produced, only will ever make sense if perl itself reads it and runs it. So the presence or absence of the shebange line is neither here nor there. Its up to the user to make that happen as far as hes concerned. (Although the code is patchable to preserve any existing shebang line, perhaps you should prepare a patch?)

      Second, of course its possible for the script to be executed without a shebang line. perl script.pl or perl script.pm works perfectly fine, and on many of the OS's that Perl (and Acme::Bleach) run on shebang lines dont mean anything at all (except that Perl itself pretends like they do). On Win32 for instance file associations are via the registry.

      I think he expects the line to be the top one, (I've not read the docs, only the code you posted), and that if you want a shebang line or anything else used first that you'll put it in after the use Acme::Bleach does the encode.

      But I dont think you should take the failings of the module too seriously, I dont think he took any of it seriously :-)


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
        Whoa! The shebang line is optional? I always thought that the shebang was very important and necessary, at least that is what I was taught. I never knew that it was optional. On Win32, you put #! perl.exe as the first line. . . I guess you learn something new everyday on this site!
Re: Re: Unlocking the Dark Mysteries of Acme::Bleach
by Anonymous Monk on Jun 30, 2003 at 13:20 UTC
    If eloquent code is the mark of a master programmer, Conway has proven himself....a hack.
      Acme::Bleach is elegant, not eloquent. Reading it is, to me, like tasting a fine wine. It's like golfing. Do you put golfed code into production? No. Is it neat to play with? Yes. Don't confuse items meant for one as items meant for the other. Eloquence isn't always the goal.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Ah, now if I could be but a 10th of the hacker that he is....


      ---
      demerphq

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found