Best way to pass module creator reference

Discussion in 'Perl Misc' started by Drago, Nov 12, 2004.

  1. Drago

    Drago Guest

    I have a module which I would like to be able to create instances of a
    class it knows nothing about. Is there a neat way short of an anonymous
    subroutine, like this example:

    use Foo;
    use Bar;
    # this works, but seems overly verbose
    Bar->new(sub { Foo->new(@_) } );

    I note that

    $c = \&Foo;
    print ref($c); # says CODE

    but within my module, one cannot then say

    $c->new(...);

    So I am not sure what \&Foo is giving me (i.e. a code reference to
    what?). So is there a better way than the way I did it in the first example?
    Drago, Nov 12, 2004
    #1
    1. Advertising

  2. use Foo;

    sub make_obj {
    my $class = shift;
    return $class->new(@_);
    }

    my $foo = make_obj("Foo", arg1 =>12, arg2 =>13)'

    Probably this is what you looking for.

    Regards, Andrew

    Drago wrote on 12 ÐоÑбрь 2004 01:04:

    > I have a module which I would like to be able to create instances of a
    > class it knows nothing about. Is there a neat way short of an anonymous
    > subroutine, like this example:
    >
    > use Foo;
    > use Bar;
    > # this works, but seems overly verbose
    > Bar->new(sub { Foo->new(@_) } );
    >
    > I note that
    >
    > $c = \&Foo;
    > print ref($c); # says CODE
    >
    > but within my module, one cannot then say
    >
    > $c->new(...);
    >
    > So I am not sure what \&Foo is giving me (i.e. a code reference to
    > what?). So is there a better way than the way I did it in the first

    example?

    --
    Andrew
    Andrew Tkachenko, Nov 12, 2004
    #2
    1. Advertising

  3. Drago wrote:
    > I have a module which I would like to be able to create instances of a
    > class it knows nothing about. Is there a neat way short of an anonymous
    > subroutine, like this example:
    >
    > use Foo;
    > use Bar;
    > # this works, but seems overly verbose
    > Bar->new(sub { Foo->new(@_) } );
    >
    > I note that
    >
    > $c = \&Foo;
    > print ref($c); # says CODE
    >
    > but within my module, one cannot then say
    >
    > $c->new(...);
    >
    > So I am not sure what \&Foo is giving me (i.e. a code reference to
    > what?). So is there a better way than the way I did it in the first
    > example?


    I am not sure how clean this is, but you could pass a string with the
    name of the class that you want to instantiate.

    use strict;
    use warnings;
    my $foo = Foo->new('Bar');
    print ref($foo), ",", ref($foo->{object}), "\n";

    package Foo;
    sub new {
    my ($class, $param) = @_;
    my $this;
    eval ( "\$this->{object} = $param->new('test');");
    return bless($this, $class);
    }

    package Bar;
    sub new {
    my $class = shift;
    my $this = { value => shift };
    return bless($this, $class);
    }


    --
    Andrés Monroy-Hernández
    Andres Monroy-Hernandez, Nov 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,892
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    537
  3. Mr A
    Replies:
    111
    Views:
    2,059
  4. Rafe
    Replies:
    3
    Views:
    271
    Jeremy Sanders
    Dec 1, 2008
  5. 12 34
    Replies:
    14
    Views:
    201
    Siedentopf
    Aug 3, 2007
Loading...

Share This Page