Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

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

Very cool! It intrigued me enough to try to figure out how it worked - I hope I got close (this is my first attempt at a de-obfuscation):

Spoiler in readmore!

Walking through the code after a bit of reformatting, with some comments:

# Use a hash slice to copy all the keys in the symbol table to %s, # and set each key to undef. @s{keys %::}=(); # Bless a hashref into the current package, thereby creating # AUTOLOAD and DESTROY entries in symbol table. bless{};; # Pull out any keys in %s that are not in %:: (AUTOLOAD and DESTROY), # sort them, and take the second element (DESTROY). # Assign an anonymous sub to the DESTROY typeglob. *{ (sort grep{ !exists $s{$_} } keys %:: )[1] } = sub { $n = 0.5; { # Print a carriage return (sends cursor to beginning of line). # Split the JAPH text into single characters, then for each # character, choose a random number between 0 and 100, # compare to $p and print the character or a space. # Note: as $p increases there is a better chance of # displaying the character (and visa versa). # Question: is the comma in the JAPH text intentional? print "\r", map { rand(100) <= $p ? $_ : ' ' } split //, "Just another Perl hacker,"; # Stop loop when $p < 0 ($n is 0.5 or -0.5, so always < 1). $p < 0 && $n < 1 && last; # Use $n to increase or decrease $p by 0.5 # $p increases from 0.5 to 110.5, then decreases to -0.5 $n *= $p > 110 ? -1 : 1; # change sign of $n if $p > 110 $p += $n; # increase/decrease $p by $n # Use the four-arg form of select (ready file descriptors): # $q is undef, and the timeout value is set to 0.04 sec # This is equivalent to sleep 0.04 with better resolution. select $q, $q, $q, .04; # Redo this block redo } }; # This can be rewritten as 'bless \$0; $| = 1;' # Bless the scalar $0, but since we didn't save a reference to the # newly created object it is immediately destroyed - the DESTROY # method is called on the object, and the sub above is executed. # This can be observed by explicitly declaring an alternative # DESTORY method, or by adding 'print "destorying $_[0]\n";' # to the first line of the subroutine. bless \$|++;
Update: the original code was tweeked, but I don't think it changes this explanation.

In reply to Re: Clean up and shimmer by bobf
in thread Clean up and shimmer by davido

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 goofing around in the Monastery: (7)
As of 2024-04-19 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found