Hello,
I'm trying to utilize Class::MethodMaker for my open source shipping
module (http://www.kavod.com/Business-Shipping).
I would like to extend it so that it will call SUPER in the methods
generated by the MethodMaker "grouped_fields" configuration parameter.
I've built my own class that inherits from Class::MethodMaker, but when I
try to get it to call SUPER::$method_name(), I get:
Can't locate auto/CustomMethodMaker/SUPER/required.al in @INC
(Where $method_name happened to be "required" at the time).
So, obviously, something isn't right. I think it's because SUPER only works
depending on the 'package' type, or something.
Here is what I did to the grouped_fields configuration parameter:
--- grouped_fields.orig 2003-07-05 14:37:06.000000000 -0700
+++ grouped_fields.new 2003-07-05 15:22:56.000000000 -0700
@@ -1,10 +1,21 @@
-sub grouped_fields {
+sub grouped_fields_inherit {
my ($class, %args) = @_;
my %methods;
foreach (keys %args) {
my @slots = @{$args{$_}};
$class->get_set(@slots);
- $methods{$_} = sub { @slots };
+
+ my $method_name = $_;
+ $methods{$_} = sub {
+ my $self = shift;
+ my @parent_slots = ();
+ @parent_slots = $self->SUPER::$method_name();
+ return ( @parent_slots, @slots );
+ };
+
}
$class->install_methods(%methods);
}
I've tried to reduce all of the code down to the smallest example possible
that still exhibits the problem, and pasted it below my sig.
I would greatly appreciate any comments.
Cheers,
--
Dan Browning, Kavod Technologies, <[EMAIL PROTECTED]> 360.843.4074x217
6700 NE 162nd Ave, Ste 611-210, Vancouver, WA. Random Fortune:
Consultant, n.:
An ordinary man a long way from home.
# Separate into four files: CustomMethodMaker.pm, Parent.pm, Child.pm, and
# test.pl
############################################################################
## CustomMethodMaker.pm
############################################################################
package CustomMethodMaker;
use strict;
use warnings;
use base ( 'Class::MethodMaker' );
=head2 grouped_fields_inherit
Works like grouped_fields, except that it also calls the parent. For
example:
grouped_fields => [
required => [ 'r1', 'r2' ],
optional => [ 'o1', 'o2' ]
];
$self->optional() results in $self->SUPER::optional(), then
$self->optional().
=cut
# Slightly modified 'grouped_fields' function
sub grouped_fields_inherit {
my ($class, %args) = @_;
my %methods;
foreach (keys %args) {
my @slots = @{$args{$_}};
$class->get_set(@slots);
my $method_name = $_;
$methods{$_} = sub {
my $self = shift;
my @parent_slots = ();
my $to_exec = "SUPER::$method_name";
@parent_slots = $self->$to_exec();
return ( @parent_slots, @slots );
};
}
$class->install_methods(%methods);
}
1;
############################################################################
## TestParent.pm
############################################################################
package TestParent;
use CustomMethodMaker
new_hash_init => 'new',
grouped_fields_inherit => [
required => [ 'parent_required_1', 'parent_required_2' ],
optional => [ 'parent_optional_1', 'parent_optional_2' ]
];
1;
############################################################################
## TestChild.pm
############################################################################
package TestChild;
use vars ( '@ISA' );
use TestParent;
@ISA = qw( TestParent );
use CustomMethodMaker
new_hash_init => 'new',
grouped_fields_inherit => [
required => [ 'child_required_1', 'child_required_2' ],
optional => [ 'child_optional_1', 'child_optional_2' ]
];
1;
#!/usr/bin/perl
#
############################################################################
## Test.pl
############################################################################
#
#
use strict;
use warnings;
use TestChild;
my $test_custom_method_maker = TestChild->new();
# Should print:
# "parent_required1, parent_required2, child_required1, child_required2
print join( ', ', $test_custom_method_maker->required() ) . "\n";
# Should print:
# "parent_optional1, parent_optional2, child_optional1, child_optional2
print join( ', ', $test_custom_method_maker->optional() ) . "\n";
if ( $test_custom_method_maker->can( "SUPER::required" ) ) {
print "yes, it can execute SUPER::required()\n";
}
if ( $test_custom_method_maker->can( "SUPER::new" ) ) {
print "yes, it can execute SUPER::new()\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]