Hello folks,

I'm planning a little module ( an interactive cli using Term::ReadLine ) but I want make possible for the module user to add some method in the constructor, no Moo* usage is planned so I'd like to see basic perl approaches. At the moment I have soemthing like:

sub new{ my $class = shift; my %opts = @_; unless ( defined $opts{dumper} ){ require Data::Dumper; # + () to avoid Name "Data::Dumper::Dumper" used only +once: possible typo at # full qualified name to avoid runtime error $opts{dumper} = sub{ print +Data::Dumper::Dumper(\$_[0 +]) } } return bless { %opts }, $class; }

dynamic method creation

I can build up a method dynamically with *{"${class}::${meth}"} = sub { my $self = shift; ... } I imagine I can do this directly in the new constructor, right? There are drawbacks or pitfall I overlooked in this approach?

dispatch table

The above code for new adds not a method but a property of the object actually being a callable sub: after I can access it like in  $obj->{dumper}->([1,2,{a=>42}])

I can expand this providing to the module user a method to add commends: use Data::Dump; $obj->add_command( {descr=>'a dumper', command => sub{ dd $_[0] } )

This seems to me a simpler approach and I cant imagine drawbacks using it. The only one I can imagine is that not being a method I cannot have $self available inside this calls (even if I dont know atm if will be needed).

pseudo methods?

But I can craft execute_command to pass $self as first argument:

sub execute_command{ my $self = shift; my $wanted_command = shift; my @whole_data = @_; $self->{ $wanted_command }->( $self, @whole_data ); }

This leads to something like a pseudo method; is this too dirty? Has other drawbacks? Do you expulse me from perlmonks if I write it down? :)

In addition: if new will be as above in the final module, should I add Data::Dumper to prerequisites? I'd say yes but... if not Data::Dumper but Math::Mersenne::Primes was the case? I must add it to prerequisites even if there is the chance it will be never loaded?

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to dynamic method creation, dispatch table or pseudo methods? by Discipulus

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.