K
koszalekopalek
Hello,
I would like to create a number of objects of the same class (mypack)
and specify a reference to a function in the constructor. I would like
to
let the module user either use a predefined function (e.g. test1)
provided in the class or let him write his own function and provide
a reference to it in the constructor.
The example below does just that.
I create object $obj1 of class mypack and use a predefined sub test1.
Then I create $obj2 and pass a reference to a custom function
customtest.
The problem is that the two functions
$obj1->riddle (1, 2, 3);
and
$obj2->riddle (1, 2, 3);
receive different arguments depending on where they come from.
The first argument passed to the function test1 is the name of the
class (and then comes the number, i.e. 1). If it is customtest, it
does
not get the name of the class.
This is the output of the attached program:
---- test1 args ----
mypack
1
---- customtest args ----
1
2
What would be the most elegant way to tackle this problem? I'd
rather
not shift arguments in test1 or do anything like that. Maybe there
is
a better way to pass function references in the constructor?
#!/usr/bin/perl
{
package mypack;
sub new {
my $class = shift;
$self = { @_ };
bless $self, $class;
return $self;
};
sub riddle {
my $self = shift;
my $testref = \&test;
my $testref = $self->{fun};
&$testref (@_);
};
sub test1 {
print "---- test1 args ----\n";
print @_[0], "\n";
print @_[1], "\n";
};
};
sub customtest {
print "---- customtest args ----\n";
print @_[0], "\n";
print @_[1], "\n";
}
my $obj1 = mypack->new(
fun => sub { mypack->test1 (@_) }
);
$obj1->riddle (1, 2, 3);
my $obj2 = mypack->new(
fun => \&customtest
);
$obj2->riddle (1, 2, 3);
I would like to create a number of objects of the same class (mypack)
and specify a reference to a function in the constructor. I would like
to
let the module user either use a predefined function (e.g. test1)
provided in the class or let him write his own function and provide
a reference to it in the constructor.
The example below does just that.
I create object $obj1 of class mypack and use a predefined sub test1.
Then I create $obj2 and pass a reference to a custom function
customtest.
The problem is that the two functions
$obj1->riddle (1, 2, 3);
and
$obj2->riddle (1, 2, 3);
receive different arguments depending on where they come from.
The first argument passed to the function test1 is the name of the
class (and then comes the number, i.e. 1). If it is customtest, it
does
not get the name of the class.
This is the output of the attached program:
---- test1 args ----
mypack
1
---- customtest args ----
1
2
What would be the most elegant way to tackle this problem? I'd
rather
not shift arguments in test1 or do anything like that. Maybe there
is
a better way to pass function references in the constructor?
#!/usr/bin/perl
{
package mypack;
sub new {
my $class = shift;
$self = { @_ };
bless $self, $class;
return $self;
};
sub riddle {
my $self = shift;
my $testref = \&test;
my $testref = $self->{fun};
&$testref (@_);
};
sub test1 {
print "---- test1 args ----\n";
print @_[0], "\n";
print @_[1], "\n";
};
};
sub customtest {
print "---- customtest args ----\n";
print @_[0], "\n";
print @_[1], "\n";
}
my $obj1 = mypack->new(
fun => sub { mypack->test1 (@_) }
);
$obj1->riddle (1, 2, 3);
my $obj2 = mypack->new(
fun => \&customtest
);
$obj2->riddle (1, 2, 3);