Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have a Perl OO problem in need of some brevity. Basically, I have three classes:

  1. Foo::Container : Holds a bunch of Foo::Record objects in a non-trivial organizational structure.
  2. Foo::Record : Individual records have no reference to their parent container.
  3. Foo::Transform : Each Transform object ($tr) is initialized with a series of custom transformations. $tr->transform($container) would then apply the custom transformations on the given Container, and would be called for many, many different Containers in its lifetime. This class will be extended via inheritance to support several different classes of transformations.

Container and Record are straightforward, and writing each transformation is relatively easy as well, but I'm struggling to come up with a concise Perl syntax to express each transformation. For example, I have something like this:

# Error checking omitted for brevity... package Foo::Transform; my %transformations = ( # TODO - What goes here? ); sub new { my ($proto, @xforms) = @_; my $class = ref($proto) || $proto; bless({ xforms => [ @xforms ] }, $class); } sub transform { my ($self, $container) = @_; for my $xform (@{$self->{xforms}}) { my $foo = get_foo(); my $bar = get_bar(); # TODO - Apply transformation named $xform # on $container, with $foo and $bar # available as well. } }

I could of course do something like:

my %transformations = ( 'xform1' => sub { my ($cont, $foo, $bar) = @_; ... } );

... and then call $transformations{$xform}->($cont,$foo,$bar) in transform(), but the very thing I'm trying to avoid is the needless duplication of that boilerplate in every transformation. Most transformations will just be one or two statements, but some will be rather more complex.

I would like to have $cont, $foo, and $bar automatically available in lexical scope of each transformation, but I don't know if that's possible without using eval EXPR (as opposed to BLOCK syntax), and if I do that I lose compile-time checks, not to mention syntax highlighting. :-)

If there's a better OO/Perl pattern I'm missing here, please let me know! This doesn't smell right.


In reply to Perl OO: Need a concise way of representing operations on an object by wanna_code_perl

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found