go ahead... be a heretic | |
PerlMonks |
When CGI->new doesn't return a CGI objectby Juerd (Abbot) |
on Jan 07, 2003 at 00:48 UTC ( [id://224805]=perlmeditation: print w/replies, xml ) | Need Help?? |
Note: All variables used or mentioned here are package global variables, not lexicals. *foo is a typeglob. Using it, you can access @foo, $foo, %foo and &foo. You probably know that. You can get a reference to @foo by using \@foo, but also by using *foo{ARRAY}.
So far, so good. If you want to throw away our old global variable and replace it with another, you assign a reference to *foo. Perl will use the appropriate slot, *foo{THING} itself is read only.
References are being assigned. Nothing special. Note that no data is copied. If you have $foo, that is a reference to an array, and assign it to *foo, both @$foo and @foo are the same array. And $foo and \@foo are references to that array. References can be blessed. And because the slots in a typeglob are references, they can be blessed too. Just like you would bless anything else. And because if you reference something that is dereferenced, you get the same reference you dereferenced, the reference would still blessed, if it was originally.
As you can see, no scalar variable is needed to store an object in. You can use the slots of a typeglob as well. Remember how \@foo == *foo{ARRAY}? This means \@foo can be an object!
The parens are needed because \ has lower precedence than ->. Perhaps you think this is nice and all, and think you will never use objects that aren't stored in normal scalar variables. Well, think again. File and directory handles, found in the IO slot, are blessed references. That's right, filehandles are objects. Blessed to IO::Handle. Even if you're not using that module.
What's going on here, why doesn't Perl look for an autoflush method in a class named STDOUT? *STDOUT{IO} is a blessed IO reference. When a string is used in an OO manner (the left hand side of the arrow operator), it's used as a filehandle if the IO slot is used. If it there's no IO slot, the bareword is supposed to be a class. With foo->method(), IO::Handle::method is used if foo is a filehandle, but foo::method is used if it is not. "foo"->method() is no different. Consider the following code:
What is $cgi here? Is it a CGI object, as the guy I was helping expected? No. Is it an IO::Handle object, as I expected? No. It is a GLOB reference, blessed to GLOB. I'd like to know why, because even *CGI{IO}->new would have returned an IO::Handle. Can anyone explain please what happens in CGI->new here? Things I learned so far:
Back to
Meditations
|
|