Hi all. Whilst investigating Tangram, a thought occurred to me: wouldn't it be great to be able to specify the exact attributes that an object is allowed to have in the class definition, and have "standard" constructors, access and update methods use those as a master source for their actions?

You have to tell Tangram what attributes an object has, so that it knows how to store it in the database. So why not combine the two? Tao::Object facilitates just that. For instance, say you have a "Person" class that has attributes "name", "age" and "father". That would be represented in a Tangram schema as:

package Person; $schema = { fields => { string => [ qw(name profession) ], int => [ qw(age) ], ref => [ qw(father) ], } };

It's a simple data type, so what do you want to check, other than the name is a string short enough to go in the database, the age is an integer and "father"->isa "Person"?

Adding Tao::Object to @Person::ISA, you would be able to do the following without writing any class functions whatsoever.

my $father = new Person(name => "Charlie Vilain", age => 49, profession => "DMS II Guru"); my $self = new Person(name => "Sam Vilain", age => 22, profession => "Perl weenie", father => $father); print "My name is... " . $self->name() . "\n"; $self->name("Samuel"); # ok, sets name to "Samuel" $self->age("old"); # croaks, age is an int $self->father("Darth"); # croaks, "Darth" is not a blessed object print $self->mother(); # croaks, "mother" not recorded in schema

Now, I don't know about you lot, but I am finding this shortcut bloody useful, and it doesn't feel like a strict type straightjacket.

The meditiation is this: do you think that this is a useful shortcut, or will promote sloppy programming? Note: you can supply a custom function to check each attribute, and the destructor will automatically delete $obj->{father}, because it's a reference and that's generally a good idea to break potential circular references. See the POD for more.

In the Perl 6 PDF by Damian Conway, it mentions "More Declarative class specifications". Does anyone know offhand whether that will cover this sort of thing, or any other modules that I've missed that cover this sort of area?

I would also appreciate any peer review of the code, etc. If I get enough positive feedback from this, I may have to polish it up and CPAN it.

I can see other really useful uses for this, such as automatically generating a dia XML file from a set of module files that is a UML diagram representing your project. That would be ultra-cool, but would need some development to work.

srand 3.14159; print join("", sort{rand 1<0.5}map{$_^"\037"}split m{ }x,"qmptk|z~wOzm??l]pUqx^k?j"),",\n";

In reply to Too lazy for constructors by mugwumpjism

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.