Dear fellows,

I'm making a computational-linguistic application (finding clauses in sentences). The whole thing is a perl module and it has a "train_model" function, which will accept training data and train and save a model. The model itself is pluggable. It's a class, the name of which is in a configuration file or provided to the "new" method. The training itself is then delegated to the model class by calling its train method.

However, the model may need to peek at the various configuration I have stored in the main object. So, when calling the model's train method, I would like its first parameter to be actually the main object. The model class is not instantiated (it needn't even provide a "mew" method - just "train" and "tag") and I only have the name of the class.

I could either make the object think he's an instance of the model class (by blessing its copy or setting local @ISA) or I could explicitly call the train function by no strict 'refs'; &"$model_class::train"($self, training_data) or make a change to the overall design.

What do you think?

A skeleton of the layout:

directory content:

MainApp.pm MyModel.pm config

config:

model:MyModel option:Value

MyModel.pm:

sub train { my ($himself, $training_data) = @_; my $option = $himself->{'option'}; ... }

in MainApp.pm:

sub new { my ($class, $conf_file) = @_ my $options = read_conf($conf_file); # now $options == {model=>'MyModel',option=>'Value'} eval "require $options->{model}"; return bless $options, $class; } sub train_model { my ($self, $raw_data) = @_; my $data = do_magic_with($raw_data); #Either: no strict 'refs'; &"$self->{model}::train"($self, $data); #Or: my $model = $self; bless $model, $self->{'model'}; $model->train($data); #Or: { local @ISA = $self->{model}; $self->train($data); } }
use strict; use warnings; print "Just Another Perl Hacker\n";

In reply to OO Design Question by Sixtease

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.