Ho un oggetto base chiamato RuleObject e un oggetto che eredita da quello chiamato RuleObjectString. Ho un nuovo metodo in RuleObjectString che voglio chiamare nel mio codice che usa quell'oggetto. Ma ottengo l'errore. 'Impossibile trovare il metodo dell'oggetto "compare" tramite il pacchetto "RuleObject" in ./testobject.pl riga 10. " Ma non sto creando un RuleObject. Sto creando un RuleObjectString. Cosa sto facendo di sbagliato qui?Estensione di un oggetto in Perl
testobject.pl
1 #! /usr/bin/perl
2
3 use strict;
4
5 use RuleObjectString;
6
7 my $s = RuleObjectString->new();
8 $s->value('stuff goes here');
9
10 if ($s->compare('stuff')){
11 print "MATCH!\n";
12 }else{
13 print "no match :(\n";
14 }
RuleObject.pm
package RuleObject;
our @ISA = qw/Exporter/;
our @EXPORT = qw/new/;
use strict;
sub new{
my $class = shift;
my $self;
$self->{value} = undef;
bless $self;
return $self;
}
sub value{
my $self = shift;
my $value = shift;
if ($value){
$self->{value} = $value;
}else{
return $self->{value};
}
}
RuleObjectString.pm
package RuleObjectString;
our @ISA = qw/RuleObject/;
our @EXPORT = qw/compare/;
use strict;
sub compare{
my $self = shift;
my $compareto = shift;
return $self->value() =~ /$compareto/;
}
Non si dovrebbe '@ EXPORT' i metodi della classe e dell'istanza ei moduli in genere non dovrebbero ereditare' Exporter' a meno che non abbiano funzioni procedurali in buona fede da esportare. – pilcrow
E per i casi in cui è necessario esportare * funzioni *, 'Sub :: Exporter' è molto più interessante rispetto a' Exporter'. Ma probabilmente dovresti evitare di esportare le funzioni dai pacchetti che sono anche definizioni di classi. ' –