×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Creating a wrapper module (multiple inheritance)

Creating a wrapper module (multiple inheritance)

Creating a wrapper module (multiple inheritance)

(OP)
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

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();

RE: Creating a wrapper module (multiple inheritance)

(OP)
Hello,

I've figured that a better way to call child methods would be to create object accessor methods in the parent class, i.e. :

CODE

sub foo_object
{
    return $_[0]->{objects}->[0]; # better if objects stored in hash ref.
}

sub bar_object
{
    return $_[0]->{objects}->[1]; # better if objects stored in hash ref.
}

CODE

print $foobar->bar_object->bar();
print $foobar->foo_object->foo();

This removes use of a clunky AUTOLOAD method.

Chris

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close