Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Best way to pass module creator reference

Reply
Thread Tools

Best way to pass module creator reference

 
 
Drago
Guest
Posts: n/a
 
      11-12-2004
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?



 
Reply With Quote
 
 
 
 
Andrew Tkachenko
Guest
Posts: n/a
 
      11-12-2004
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
 
Reply With Quote
 
 
 
 
Andres Monroy-Hernandez
Guest
Posts: n/a
 
      11-12-2004
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);
}


--
Andrs Monroy-Hernndez
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to dynamically get an attribute from a module from withinthe same module Rafe Python 3 12-01-2008 11:53 AM
difference between pass by address and pass by reference!! blufox C Programming 2 04-03-2006 02:53 PM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM
Pass by Pointer * or Pass by Reference &? Robert C++ 10 08-24-2005 04:15 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM



Advertisments