Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Very nice effect! I just had to take a stab at this one... and here it is:

Disclaimer: I might be using the terminology for references and/or typeglobs wrongly here, please correct me if that is the case.

First, some reformatting:

$|++; $.="Just another Perl hacker "; while(!select $',$&,$`,1/200) { (*_,*") = \(substr($.,0,1), substr$.,1,$,++%24); print+qq'\r$"'; $. .= chop }
Then it is time to disect. :)
$|++;
Just turns off buffering.
while(!select $',$&,$`,1/200)
Is just the same thing as:
while(!select undef,undef,undef,1/200)
simply creating an infinite loop with a delay of 0.005 seconds.

  (*_,*") = \(substr($.,0,1), substr$.,1,$,++%24);
takes a reference the first letter of $. (notice the backslash) and puts it into $_ (via the *_ typeglob), while

  (*_,*") = \(substr($.,0,1), substr$.,1,$,++%27);
takes (a reference to) letters 1 to ($, % 27) and puts it into $" (again, via typeglob). It also increases $, everytime, so this expression will go from 0 to 26 and then around again. Compare for(0..26). Also note how I colored the paranthesis, for clarity.
print+qq'\r$"'
prints a carriage return, which puts us back to the start of the line, and then prints the contents of $", which was set on the line above.
$. .= chop
then simply chops the last (and only) character from $_ and puts it first in $. - remember that $_ was made a reference to the first letter in $., so what happens is that the first letter is what is actually chomped. This causes the string in $. to rotate.

So, essentially, we have one loop that rotates "Just another Perl hacker" around, and then an inner loop of sorts, that prints a part of it. The effect is given by the fact that the first character of output is overwritten everytime, while subsequent characters are written much more rarely. We could even rewrite it to the more readable(??):

while(1) { for(0..26) { # go back to start of line print "\r"; # print substring of current $. # using our iterating $_ print substr($.,1,$_); # rotate $. putting first character last $. .= substr($.,0,1,''); # Pause for 0.005 seconds select undef,undef,undef,1/200 } }
to get the same effect...

This was a very nice effect, I must say. I had no idea you could reference a part of a substring that way. Well, you learn something new every day. Thanks for the amusement!


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

In reply to (Solution?) Re: Variations on substr (2) by Dog and Pony
in thread Variations on substr (2) by BooK

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2014-07-12 18:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (240 votes), past polls