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
In reply to Dynamically Creating (and Calling) Object Methods by zaven
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |