P is for Practical | |
PerlMonks |
Re: Re: Re: Classes Are Killing Meby Anonymous Monk |
on May 19, 2002 at 21:49 UTC ( [id://167727]=note: print w/replies, xml ) | Need Help?? |
You have a common problem. You had a small mistake, you
had a wrong idea about where the mistake was, came up with
a theory, and from there you have careened to a number
of erroneous conclusions. And now those conclusions leave
you unable to understand or accept good advice when you get
it. ariels is right. You bless $Person::list into no class at all. This blesses it into the empty class, which is the root of the entire tree of package namespaces, and is better known as main. This keeps you from calling methods normally, so you get around this by directly specifying the package to start the method search in. And somehow you convinced yourself that the object didn't have private data like the books says it should, so you started using globals. (Probably you just wrote an accessor wrong somewhere, and had a wrong conclusion.) Please accept that everything you think you know is probably wrong. It will make learning the truth much easier. Here is some of that truth. When a function receives a method call, @_ holds the callee and then the arguments to the call. The callee is what the method was called on, which can be a class (ie package name) or an object (ie blessed reference). Normally only constructors get package names, and they are supposed to construct an object and bless them into that package. A method call looks like $object->bar(@stuff) (with the function getting an @_ that looks like ($object, @stuff)). Now let's rewrite your code properly. First your module: And the driver code. Yes, the constructor that you thought didn't work, does. You typed it in wrong, blamed the wrong thing when strange stuff started to happen, and went further and further wrong from there. Points to take away.
In Section
Seekers of Perl Wisdom
|
|