$class vs. $self.
Consider this most trivial class:
package Trivial;
sub new {
my $class = shift @_;
...
return bless $self, $class;
}
sub some_method {
my $self = shift @_;
...
}
...
# in your program that uses Trivial:
my $instance = Trivial->new(..);
my $instance->some_method(..);
In the constructor (new), @_ is shifted into $class. This is the name of the package, in this case Trivial.
When calling some_method(), @_ is shifted into $self. Here, $self is a reference to the calling object, $instance, which is of class Trivial.
Although i didn't specifically look, there is surely much more detailed documentation on perlmonks. In any case, a good way to remember what is being sent, look at what is to the left of the arrow "->" operator.
But really, that is a quick and dirty answer that doesnt really tell whats going on. So check out the docs, or Damian Conway's Object Oriented Perl for some very detailed explanations.
| [reply] [d/l] |
Thanks again for the input. I'm familiar with everything you said, and I do have Damian Conways' OOP (although I haven't read it all yet).
In fact, without using Class::MethodMaker, I can write everything by hand and it works like I want it to. Here is (nearly) the same code, but without MethodMaker:
#!/usr/bin/perl
use strict;
use warnings;
#####################################################################
## Bug
#####################################################################
package Bug;
sub new { bless {}, shift }
sub required
{
my @required = ( 'id', 'type', 'description' );
return @required;
}
#####################################################################
## FixedBug
#####################################################################
package FixedBug;
use base ( 'Bug' );
sub new { bless {}, shift }
sub required
{
my @required = (
'date_fixed', 'repairer',
shift->SUPER::required()
);
return @required;
}
#####################################################################
## Main
#####################################################################
package Main;
my $fixed_bug = FixedBug->new();
print join( ', ', $fixed_bug->required() ) . "\n";
See?
-Dan | [reply] [d/l] |