I am sure that this is described in the documentation, but I could not find it ... I am looking for syntactic sugar that will allow a method to be called in a class whose name is stored in a hash (as opposed to a simple scalar):
use strict; use warnings; package Foo; sub foo { print "in foo()\n" } package main; my %hash = (func => 'foo'); Foo->$hash{func};
If I first copy $hash{func} into a scalar variable, then I can just call Foo->$func ... but what is missing to enable Foo->$hash{func} ?
(EDIT: I'm not going to do anything special by calling the method of the Foo class - it can be an easy and blessed object (and in my actual code), it was just easier to write a standalone example using the class method.)
EDIT 2: Just for the sake of completing the comments below, this is what I am actually doing (this is in the Muse sugar attribute library created using Moose :: Exporter ):
# adds an accessor to a sibling module sub foreignTable { my ($meta, $table, %args) = @_; my $class = 'MyApp::Dir1::Dir2::' . $table; my $dbAccessor = lcfirst $table; eval "require $class" or do { die "Can't load $class: $@" }; $meta->add_attribute( $table, is => 'ro', isa => $class, init_arg => undef,
I replaced the marked line above:
my $pk_accessor = $this->meta->find_attribute_by_name($args{primaryKey})->get_read_method_ref; my @args = ($args{primaryKey} => $this->$pk_accessor);
PS. I just noticed that the same technique (using the Moose metaclass to search for a codeword, rather than accepting its naming convention) cannot be used for predicates either, since Class :: MOP :: Attribute does not have a similar accessory get_predicate_method_ref .: (
syntax reference perl attributes moose
Ether 02 Dec '09 at 22:06 2009-12-02 22:06
source share