Hi Monks,

I would like to evaluate a method belonging to a module named within a xml configuration file with each block being:
<word method="MyModule::MethodName()"> MyWord </word>
The module "MyModule" will have been initialised previously by the calling script and passed as within an array reference to the method which reads the configuration file.
my $mod = MyModule->new(); $mod->initialise_stuff("settings");
Then it is passed to the Method which manages the configuration in this manner (the $TPL below is from the excerpts in my implementation, it is an instance of a template module).
$TPL->load_XMLkeywords([ $mod ], cwd()."/../conf/keywords.xml");
Out $TPL->load_XMLkeywords method looks like this (I'm using XML::Twig to parse the XML).
sub load_XMLkeywords { my $self = shift; my $modules = shift; my $filename = shift; warn "Loading XML Keywords\n" if (qw/DEBUG/); my $X = XML::Twig->new(); $X->parsefile($filename); my $root = $X->root(); my @words = $root->children("word"); warn "Element Count ".scalar @words ."\n" if (qw/DEBUG/); foreach my $word(@words) { # Include this word as a keyword. my $key = $word->text(); $key =~ s/\n//g; $self->add_keyword($key); warn "Keyword: ".$key."\n" if (qw/DEBUG/); # Now we need to evaluate the method or query. # and create a field set within our T_KEYWORDS reference. if ($word->att("query")) { # In this case this is a singular database query. my $qq = $word->att("query"); my $sth = $self->dbh()->prepare($qq); $sth->execute(); my $val = $sth->fetchrow(); $self->{T_KEYWORDS}{$key} = $val; warn $key." = $val\n" if (qw/DEBUG/); $X->flush(); } elsif ($word->att("method")) { my $invoke = $word->att("method"); # find a corresponding class. foreach my $class(@$modules) { my $type = ref $class; if ($invoke =~ /$type/) { warn "Found Type $type\n" if (qw/DEBUG/); $invoke =~ s/$type:://g; } my $val; my $statement = '$val = $class->$invoke'; eval $statement; $self->{T_KEYWORDS}{$key} = $val; warn $key." = $val\n" if (qw/DEBUG/); } $X->flush(); } } }
What I am having trouble with is the eval statement where the "method" attribute is set on the word. I am not sure whether I can join a live instance of our $class with the method named in the config file and whether during evaluation I am actually able to evaluate the method of the $class that I am passing this method. I'll still be messing with this but I thought I'd ask the monks to see if anyone had some suggestions.

Thanks,

Chris.


In reply to Joining Module reference with its method named as string. by ceedee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.