"Class methods" can mean two things, depending on which flavor of OO you grew up with.
In the Smalltalk world, "class methods" are methods that are called without needing to have an instance of the class.
$baz = Foo::bar();
Methods invoked thusly don't have access to any instance data, but do have access to "class globals".
Other schools of OO use the term to mean methods that are called via an instance of the class.
$baz = $instanceOfFoo->bar();
(Astute readers will note that may CGI.pm methods "switch hit", and can be called either way.)
From your question, I'm guessing you mean the latter usage, and that you're looking for a way of doing "protected" methods, which are instance methods that are only visible (and hence callable) from an instance of the class, and not for anyone outside of the class.
Perl doesn't directly support protected methods. But there are conventions. One convention is to prepend '_' to the method name to indicate that it's for internal use.
| [reply] [d/l] [select] |
I prefer the Smalltalk usage because it is extremely
descriptive of what is happening.
In Smalltalk, classes are objects. As objects they have
methods. Those methods are the class methods.
Instances of the class are objects. As objects they have methods. Those methods are instance methods.
Since the class and the instance are different objects,
and moreover different kinds of objects, it comes as no
surprise that they have different methods.
None of this is applicable in Perl, of course, since Perl
packages are not objects. (Simon Cozens wanted to make
that possible, but last I heard it got vetoed as
potentially breaking too much old code.) But in languages
where classes are themselves objects, the Smalltalk
terminology is how you should think about things. And
people who come from languages like that (for instance
merlyn) tend in Perl to like distinguishing between
methods that are meant to be called through the name of
the class (eg new) and those that are called from an
instance (everything else).
Which is why I, along with merlyn, dislike the
construct you sometimes see of:
my $pkg = ref($self) || $self;
Its purpose is to blur the distinction between methods of
the class and methods of instances of the class, and we
see no reason that that distinction is a bad thing to
have. | [reply] [d/l] |
What distinction is that? Unless you're carrying around instance data pointed to by your blessed referent (which is the problem with CGI.pm), what's the effective difference?
package MyClass;
sub new {
my $class = shift;
bless(\$class, $class);
}
sub do_it {
print "Doing something!\n";
}
package main;
my $c = MyClass->new();
my $class = 'MyClass';
$c->do_it();
$class->do_it();
Now I'm open to the argument that having good access to data in $self is a big help, but method dispatch works either way. (I just re-read perltootc last night.) | [reply] [d/l] |
Perl doesn't really have a distinction between class methods and instance methods as an implementation: only as a convention. What doesn't work about simply using $self->method(@args)?
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |