Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: Unlocking the Dark Mysteries of Acme::Bleach by demerphq
in thread Unlocking the Dark Mysteries of Acme::Bleach by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-26 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found