note
btrott
Well, the comments describe what the code actually does,
but I guess you're asking *why* it does what it does.<p>
Let's take one particular statement and go over it--since
they all have a similar structure, I think that'll help
you to figure out the rest.<p>
Let's take this one:
<code>
# to squish a string
y sssscccc;
</code>
The comments provided say that this "squishes" a string.
If you actually try this out in a little test program,
you'll see exactly what it does:
<code>
$_ = "bookkeeper";
y sssscccc;
print;
</code>
This prints "bokeper"--in other words, it gets rid of
consecutive letters ("oo" => "o").<p>
Why does it do that? Let's write the statement in a
slightly different way:
<code>
tr///scccc;
</code>
This is the same statement, because:<p>
"y" is a synonym for "tr";<p>
and the "tr" operator can use any character to
separate the SEARCHLIST from the REPLACEMENTLIST.<p>
So in the obfuscated example, "tr" is "y", and the
separator is "s". We can replace those to make it look
more "normal", just as we did above.<p>
Why all the "c"'s at the end, though? Well, we can
take most of those off, too:
<code>
tr///sc;
</code>
And now we no longer have obfuscated code: this is
just the regular "tr" operator, working as advertised.<p>
The "c" tells "tr" to complement the SEARCHLIST; the
SEARCHLIST is an empty string, so the complement is
any character--so we're searching for any character.
The "s" tells "tr" to "squish" the string--
from [perlman:perlop]:
<code>
If the /s modifier is specified, sequences of characters
that were transliterated to the same character are
squashed down to a single instance of the character.
</code>
In other words, "oo" => "o", and so on.<p>
The final thing going on is that if the REPLACEMENTLIST
is empty (as it is here), the SEARCHLIST is replicated
(so the SEARCHLIST and REPLACEMENTLIST are the same).<p>
So we're translating every character (the complement
of an empty SEARCHLIST) into itself, *unless* there's
a sequence of characters that translate to the same
character, in which case we "squish" the sequence down
to one instance of that character.<p>
So, I hope this helps. Try to figure out the rest
yourself, using some test programs and [perlman:perlop].<p>
And if I've made any mistakes, someone please correct
them. :)
3224
3861