Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
hmm, i thought about that and made up some code, just for fun.
i don't know if it's really useful and correct, but it was interesting to think about, so i'm just posting this here.
we're talking just about the object-attributes and the get/set methods, i assume.
so, when i create a class i usually find myself defining an attribute method like:
sub _attr { my $self = shift; my $name = shift; return $self->{"_$name"} unless @_; $self->{"_$name"} = shift; } sub name { shift->_attr(name => @_); }

so that you can call $obj->name("new name");
(I know i could actually use Class::MethodMaker or something similar...)

now how could we make that more generic?
i thought about defining a pragma 'attribute' that you can use like that:

package Parent; use attribute sub { my $self = shift; my $name = shift; # we have an ordinary hashref for the object return \$self->{"_$name"}; }, [qw(city name)];
package Child; use base 'Parent'; use attribute sub { my $self = shift; my $name = shift; # object is arrayref, and the attributes are stored in element 0 return \$self->[0]->{"_$name"}; };

so with that, and the assumption that the module you inherit always uses methods to get/set attributes, you are independent from the implementation, right? here's attribute.pm:
package attribute; use strict; use warnings; sub import { my ($self, $sub, $names) = @_; my $class = caller(); no strict 'refs'; # set the general method *{$class . '::_attribute_ref' } = $sub; for my $name (@$names) { my $string = $class . '::' . $name; *{$string} = sub { my $self = shift; # set method for each attribute return ${ $self->_attribute_ref($name) } unless @_; ${ $self->_attribute_ref($name) } = shift; }; } } 1;
update: changed subname '_attribute_set' to '_attribute_ref'
moved readmore tag a little

In reply to Re: Why is a hash the default "object" in oo perl? by tinita
in thread Why is a hash the default "object" in oo perl? by theAcolyte

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (12)
    As of 2014-08-20 20:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (124 votes), past polls