Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Declarative Objects

by einhverfr (Friar)
on Feb 27, 2014 at 14:29 UTC ( #1076401=CUFP: print w/replies, xml ) Need Help??

In a recent blog post I put together, I added a code sample I am expecting to release in LedgerSMB 1.5 (code under the GPL v2+, etc). The sample shows what I think is really cool about Perl 5 as a language, namely the ability to change the semantics enough to build dialects for specific things in the code.

This example extends some of the things that Moose or Moo does to show declaratively defined object methods (in this case wrapping a service locator for PostgreSQL stored procedures.

package LedgerSMB::Currency; use Moose; with 'LedgerSMB::PGOSimple::Role', 'LedgerSMB::MooseTypes'; use PGObject::Util::DBMethod; sub _set_prefix { 'currency__' } has id => (is => 'rw', isa => 'Int', required => '0'); has symbol => (is => 'ro', isa => 'Str', required => '1'); has allowed_variance => (is => 'rw', isa => 'LedgerSMB::Moose::Number', coerce => 1, required => 1); has display_precision => (is => 'rw', isa => 'Int', required => '0'); has is_default => (is => 'ro', isa => 'Bool', required => '0'); dbmethod list => (funcname => 'list', returns_objects => 1 ); dbmethod save => (funcname => 'save', merge_back => 1); dbmethod get => (funcname => 'get', returns_objects => 1, arg_list => ['symbol']); dbmethod get_by_id => (funcname => 'get_by_id', returns_objects => 1, arg_list => ['id']); __PACKAGE__->meta->make_immutable;

This code allows me to do something like:

my $usd = LedgerSMB::Currency->get('USD'); my @currencies = LedgerSMB::Currency->list; $usd->{allowed_variance} = 0.2; $usd->save;
These get mapped back to stored procedures with names like "currency__get" and "currency__list" with argument names mapped in dynamically based on stored procedure argument names.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1076401]
Approved by Corion
Front-paged by Old_Gray_Bear
[Corion]: marto: As am I ;) And we got two interested locations for next year, Munich and Cologne maybe, which is great as well. Not Berlin though ;)
[marto]: damn
[choroba]: Munich is kind of close from here
[Corion]: marto: Maybe in three years it'll be Berlin again ;)
[marto]: in 3 years I may be able to go :P
[Corion]: choroba: Both teams will ponder on whether/where/how to organize things. The workshop will be mainly in German though
[Corion]: marto: Hehe ;)
[marto]: my out of working hours time is very scarce at the moment
[marto]: unfortunately for you guys I can still access PM during the day :P

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-06-29 08:17 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (655 votes). Check out past polls.