use 5.010; use strict; use warnings; sub Base::set_id {$_[0]{id} = $_[1]; $_[0]} sub Base::id {$_[0]{id}} sub Name::set_name {$_[0]{name} = $_[1]; $_[0]} sub Name::name {$_[0]{name}} sub Age::set_age {$_[0]{age} = $_[1]; $_[0]} sub Age::age {$_[0]{age}} sub gimme_object { state $class = "MyClass00000"; $class++; no strict 'refs'; @{"${class}::ISA"} = (Base => @_); bless {}, $class; } my $obj1 = gimme_object qw[Name]; my $obj2 = gimme_object qw[Age]; my $obj3 = gimme_object qw[Age Name]; $obj1->set_id(1)->set_name("Foo"); $obj2->set_id(2)->set_age(42); $obj3->set_id(3)->set_name("Bar")->set_age(15); printf "Object %d has name %s\n", $obj1->id, $obj1->name; printf "Object %d has age %d\n", $obj2->id, $obj2->age; printf "Object %d has name %s and age %d\n", $obj3->id, $obj3->name, $ +obj3->age; __END__ Object 1 has name Foo Object 2 has age 42 Object 3 has name Bar and age 15

In reply to Re: Adding a method to an existing object by JavaFan
in thread Adding a method to an existing object by forgot_other_usrname

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.