Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I would like to do this stuff on runtime. Is there documentation about this stuff ?

If you are designing the objects, then look at Class::Prototyped.

my $cp = Class::Prototyped->new(); my $mirror = $cp->reflect(); $mirror->addSlots( field1 => 'foo', sub1 => sub { print "this is sub1 printing field1: '".$_[0]->field1."'\n"; }, ); $mirror->deleteSlot('sub1');

There are others, too, like Class::Object or Class::Decorator that do things like this in various ways.

If you didn't design the object, then you could use Class::Adapter to wrap the object while adding your own methods.

As more of a hack, if you really need to do it on the fly, you can rebless the object into a dynamically generated subclass that has the methods you want. E.g.

# Create an object of Some::Class my $obj = Some::Class->new(); $obj->foo(); # where foo() is a method of Some::Class # Create the new "class" by assigning @ISA and a method in the new nam +espace @Some::Class::Extended::ISA = qw/Some::Class/; *Some::Class::Extended::bar = sub { my $self = shift; print "Hello, Ba +r" }; # Rebless the object to the new class bless $obj, 'Some::Class::Extended'; $obj->foo; # From Some::Class $obj->bar; # From Some::Class::Extended


Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

In reply to Re^3: Adding functionality to an Object by xdg
in thread Adding functionality to an Object by jeanluca

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (6)
    As of 2021-04-12 22:41 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found