On Jul 19, [EMAIL PROTECTED] said: >Whats the difference between class methods and instance methods on perl ?
Syntactically, nothing. In fact, if you see method $foo @args; or $foo->method(@args); you can't be sure whether it's a class method or an instance method being called, since $foo could be an object OR the name of a class. And they're both defined the same way, as a normal function. The only REAL difference is that a class method expects its first argument to be the name of a class, and an instance method expects its first argument to be an object. Here's a brief example; you should follow this up with the documentation already suggested to you. package Foo; my @instances; sub new { my $class = shift; my $obj = bless { count => scalar(@instances) }, $class; push @instances, $obj; return $obj; } sub get_count { my $self = shift; return $self->{count}; } sub get_instance { my ($class, $idx) = @_; # we don't really USE $class, # so this can be called by objects OR classes return $instances[$idx]; } sub all_instances { my $class = shift; # same here, we don't really NEED the argument we're passed return @instances; } This class could be used in the following way: use Foo; my $first = Foo->new; # $first's count is 0 my $second = Foo->new; # $second's count is 1 my $third = Foo->new; # $third's count is 2 print $second->get_count; # 1 print Foo->get_instance(2)->get_count; # 2, but we already knew that # these two return the SAME list of objects my @objs = Foo->all_instances; my @same = $second->all_instances; Although I don't see it done often, you can write a function that behaves different if it receives a class rather than an object: sub get_count { my $self = shift; # if it was $obj->get_count(), return $obj's count return $self->{count} if ref $self; # otherwise, return the requested object return $instances[shift]; } The two ways to call this are: # returns the object held in $third my $specific_instance = Foo->get_count(2); # returns 1, the count of $second my $c = $second->get_count; I wouldn't recommend this practice, though, because it can be difficult to tell what's going on. I would suggest having separate names of class methods and instance methods. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]