Hi Tom. You're a bit off-track here with your appreciation of object methods, but I'll try to put you straight as well as answer your questions as they are.
Tom Norwood wrote: > Cheers Rob, below is the clone() from URI::WithBase > (the base class of URI::URL). > > sub clone { > my $self = shift; > bless [$self->[0]->clone, $self->[0]], ref($self); > } > > Which is the last package included with 'use' in my > original example piece of code, so I assume this > clone() is the prevalent version. There is not a 'prevalent' version of a method. Perl's very like any other OO language in that an object's method will be a routine which depends on the class of that object. If I say my $url = URI::URL->new( .. ); then I am calling the routine URI::URL::new (or perhaps one that URI::URL has inherited from a base class, such as URI::WithBase::new). Similarly with my $clone = $url->clone; I am calling URI::URL::clone or an inherited clone() subroutine. In the same way, objects of class HTTP::Request or LWP::UserAgent can have their own 'new' and 'clone' methods. If they don't then these methods can't be called through such objects - the existence of a method of the right name in a different class is irrelevant. > This is what I have as my constructor: Getting into the actual code of object methods is nearly always unnecessary: the interface is all you should be interested in, which should be fully explained in the POD within the module and therefore accessible via perldoc URI::URL or the equivalent. The author is always free to change the way his methods work as long as the interface remains invariant. > > # *(A)* > > sub new { > my($class, $init) = @_; > my $self; > if (ref $init) { > $self = $init->clone; > } else { > $self = bless { > 'id' => undef, > 'username' => undef, > 'contents' => {}, > # *(B)* > 'status' => 'B', > 'updated' => undef, > 'created' => undef, > }, $class; > } > } > > My next question is: What is the difference between > variables declared (A) outside the constructor Variables can be declared at *(A)* which belong to the package as a whole, and therefore apply to all instances of this class, but again you won't need to know about them unless the documentation tells you how to use them. Interaction with an object's internal data is nearly always done entirely through the method calls. > and those included (B) in the blessing within the > constructor? The bless function (a Perl built-in function) simply associates a data reference with a package name to so that method calls can be resolved. The line in your constructor above says $self = bless { .. }, $class; which is blessing an anonymous hash reference into the package named in $class (presumably 'URI::URL') and assigning the result to $self. All of the hash contents are specific to this instance of the class, as a second call to the constructor will create a new, separate anonymous hash. > I guess I really just don't understand the > documentation on bless, I used to do a quite bit of > C++ a few years ago and like the OO approach but Perl > is so ambiguous and cryptic at times it turns my > brain inside out. All bless does is to associate a package name with a data reference. However, unless you're writing an object module you shouldn't need this level of knowledge, which will only confuse you. Read the documentation to find how to use an object's method calls, and _remember_ that methods with the same name from different classes could do entirely different things. > How I understand bless is that it tells Perl to treat > an item pointed to by a ref as an object. But what > does that mean in actual terms, the access method for > package variables doesn't change. No, the access method for package variables doesn't change, but DON'T ACCESS THEM! As I've pointedly said already, you almost never need to do anything that can't be done through method calls. All you need to know should be in the documentation. Don't take advantage of the fact that Perl classes are distributed in source form: pretend they're compiled into a binary object library! I hope this helps. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]