zaven has asked for the wisdom of the Perl Monks concerning the following question:
I have come up with an interesting solution (I hope) to a tricky problem. I've almost got it but just need some advice.
I have a class, let's call it BigDB. I would like to dynamically generate object method names for a BigDB instance based upon some stuff read in from a file. Within the BigDB constructor, I say:
And within makeCommandSub(), I am creating a code ref and returning it:my $self = {}; # hash ref ... foreach my $method (@method_names_from_file) { $self->{$method} = makeCommandSub($stuff); }
So, I go ahead and create my new BigDB() object and everything seems to go fine. My debugger shows me that my new object $myDB now has CODE type fields:my $code = sub { my $self = shift; print "Dang, it worked.\n"; }; return $code;
Alas, when I try to run these CODE blocks as I would a method, it doesn't work.$myDB <hash> ->{method1} <CODE> ->{method2} <CODE>
yields...$myDB->method1();
I have also tried &{$myDB->method1}. I AM able to run normal subroutines within the BigDB class.Can't locate object method "method1" via package "BigDB" at line...
So what's the proper way (if any) to execute these CODE refs in my object/hash ?
Much thanks
-zaven.pl
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamically Creating (and Calling) Object Methods
by gaal (Parson) on Jan 07, 2007 at 08:33 UTC | |
by zaven (Sexton) on Jan 07, 2007 at 09:16 UTC | |
by gaal (Parson) on Jan 07, 2007 at 10:03 UTC | |
by ysth (Canon) on Jan 07, 2007 at 17:57 UTC | |
|
Re: Dynamically Creating (and Calling) Object Methods
by friedo (Prior) on Jan 07, 2007 at 09:04 UTC | |
|
Re: Dynamically Creating (and Calling) Object Methods
by merlyn (Sage) on Jan 07, 2007 at 16:57 UTC |