Hello,
I have an issue which I would like an expert advise on.
I am "using existing" & "creating custom" Facebook modules, then wrapping them in a single module. The main issue is I don't want to create a bunch of accessor methods, I want to be able to call methods directly from the parent object (or not ?). The other issue is every module has its own constructor class. I have looked into multiple inheritance, but unable to find information regarding my specific requirements.
The only solution I have come up with is:
- Setup the objects I need in an init method in the wrapper module. Push them by priority into a list of objects.
- Use the AUTOLOAD method to loop through each object in the object list if the method cannot be found, testing whether the method can be successfully called using eval.
Does my solution seem feasible? I have produced an example below.
Thanks very much,
Chris
I have an issue which I would like an expert advise on.
I am "using existing" & "creating custom" Facebook modules, then wrapping them in a single module. The main issue is I don't want to create a bunch of accessor methods, I want to be able to call methods directly from the parent object (or not ?). The other issue is every module has its own constructor class. I have looked into multiple inheritance, but unable to find information regarding my specific requirements.
The only solution I have come up with is:
- Setup the objects I need in an init method in the wrapper module. Push them by priority into a list of objects.
- Use the AUTOLOAD method to loop through each object in the object list if the method cannot be found, testing whether the method can be successfully called using eval.
Does my solution seem feasible? I have produced an example below.
Thanks very much,
Chris
Code:
#! /usr/bin/perl
use strict;
use warnings;
{
package Foo;
sub new
{
my ($class) = @_;
my $self = { };
bless $self, $class;
return $self;
}
sub foo
{
return "Foo...\n";
}
}
{
package Bar;
sub new
{
my ($class) = @_;
my $self = { };
bless $self, $class;
return $self;
}
sub bar
{
return "Bar...\n";
}
}
{
package FooBar;
sub AUTOLOAD
{
my ($self, @args) = @_;
my $return = undef;
our $AUTOLOAD;
my ($method) = $AUTOLOAD =~ m/::(.+)$/;
return if ($method =~ m/DESTROY/); # Return if DESTROY method.
foreach my $object (@{$self->{objects}})
{
eval { $return = $object->$method(@args); };
last unless ($@);
}
return $return;
}
sub new
{
my ($class) = @_;
my $self = { };
bless $self, $class;
$self->init();
return $self;
}
sub init
{
my ($self) = @_;
my $foo = Foo->new();
my $bar = Bar->new();
$self->{objects} = [$foo, $bar];
}
sub foobar
{
return "FooBar...\n";
}
}
my $foobar = FooBar->new();
print $foobar->foobar();
print $foobar->bar();
print $foobar->foo();