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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.