Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-06-23 18:49 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.