[JaxPM] Yo !

greg at turnstep.com greg at turnstep.com
Mon Sep 30 08:12:40 CDT 2002


On the jacksonville-pm-list; Jax.PM'er greg at turnstep.com wrote -


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Here is my attempt to explain the following code. Don't read this 
if you want to try and figure it out on your own.

$_=ref\@_;print lc substr(ref\*STDOUT,-2,++$|).$/ if print chop;


First, let's rearrange it a little more logically. This is 
really two statements, separated by a semicolon. The second 
half is of the format "do x if y" which is really just another 
way of saying:

if (x) {
  do y
}

I'll tell you up front that the "x" in this case is true, so we 
can start by rewriting the whole thing like this:

$_ = ref \@_;
print chop;
print lc substr (ref\*STDOUT,-2,++$|) . $/;

- From the top: The first line assigns the word "ARRAY" into the 
default variable, aka "dollar underscore". The @_ is the default 
array: it's not seen as often as $_, but it is always there. We 
don't really care what is inside of it, we merely care that it 
is an array. The "ref" function is usually used in perl to do 
a simple test of whether or not something is a reference. However, 
it also returns the type of reference, in uppercase letters, such 
as "SCALAR", "HASH", "ARRAY", etc. Since we put a backslash in 
front of the @_ array, we passed a reference to an array to the 
ref function, which returns the string "ARRAY". This string is 
put into the variable $_;

Next is the statement "print chop." I really like the poor little 
chop function. It's usually overshadowed by it's newer cousin chomp, 
but it, like ref, returns a value that is seldom used. Chop returns 
the letter that it chopped, in this case the letter "Y" from "ARRAY" 
If you looked at the default string, it would now contain the 
string "ARRA". Since we are chopping the default variable, we can 
avoid saying "print chop $_" and simply say "print chop". Since this 
statement evaluates to true (i.e. returns a non-zero, non-null value) 
we run the other print statement next. Recall that the original statement 
said "print foo... if print chop" - perl will actually run the second 
half first, check the result, and only run the first part afterwards. 
You'll often see sytax like this in code where you wish to emphasis 
the result more than the test, such as:

die "Not a valid number!\n" if $number !~ /^\d+$/;

Thus, at this point in the code, we have printed a capital letter "Y", 
and can start the final bit of code:

print lc substr (ref\*STDOUT,-2,++$|) . $/;

First, let's normalize things a little bit. The substr call is 
getting everything from the second letter from the end of the string, 
up to one character beyond that. The $| variable starts at 0, so 
we increment it by one before passing it into substr. The $/ is 
another seldom used special variable, but it almost always defaults 
to a newline. If it doesn't, you have a very unusual perl installed! 

Now we can rewrite it as:

print lc substr (ref \*STDOUT, -2, 1); print "\n";

Here we see ref again, this time used to see what type of reference 
*STDOUT is. STDOUT is another predefined variable (much like @_). In this 
case, we are throwing a star before it, which means we are getting a 
typeglob, which is basically a container of all variables named "STDOUT" 
In other words, the STDOUT typeglob contains $STDOUT, @STDOUT, %STDOUT, 
and some others without signs (such as filehandles). The inly important 
part is that it is a typeglob, which ref reports as being a "GLOB". Thus, 
we are using substr to get the second letter from the end of the 
word GLOB, in this case an uppercase "O". Finally, we lowercase 
our result, and get the final message: "Yo\n";

Greg Sabino Mullane  greg at turnstep.com
PGP Key: 0x14964AC8 200209271928


-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

iD8DBQE9mE0YvJuQZxSWSsgRArznAKCdrjvniWYYdD6dQ572A3CILG9TUACgg127
47pKAW8h530hfr4bUaE+g5M=
=NzUl
-----END PGP SIGNATURE-----



Jax.PM Moderator's Note:
This message was posted to the Jacksonville Perl Monger's Group listserv.
The group manager can be reached at -- owner-jacksonville-pm-list at pm.org
to whom send all praises, complaints, or comments...




More information about the Jacksonville-pm mailing list