I’m attempting to create a small ORM library for use in a Mojolicious web-application. I’ve grown very fond of Ruby’s Datamapper library and would like to emulate some of its behaviour if possible.
In Datamapper you can mixin Resource, and then have methods added to your class such as ‘all’, etc:
# User.rb
class User
include Datamapper::Resource;
end
...
# Application.rb
users = User.all
For my library I’m attempting to add some package level functionality to modules that inherit from a base Model in order to achieve a similar behaviour.
In essence, I would like to be able to do something approximating the following:
# User.pm
package User;
use base Model;
...
# Application.pm
my @users = User::all();
I’ve had a look around for examples of meta-programming in perl and haven’t found anything immediately helpful.
What I’m after is the following:
- Alternate perl patterns that achieve similar elegance in a more idiomatic fashion
- Ability to inherit subroutines on the package level, as well as the object level
- Ability to execute code on ‘use’ in the scope of the current package, or
- Have the current package passed to code that is executed on ‘use’
- A guide to meta-programming in Perl
- A existing declarative ORM library that supports easily creating mock-adapters as well as DB2, and MySQL
Ideally I would like to avoid running eval on large strings as much as possible.
Any help would be greatly appreciated 🙂
Roles surpass mixins.
Roles are normally consumed on the package level, but with trickery also can be applied to an instance only. (FIXME how?)
importAll parameters on the
usestatement are passed intoimportas arguments.Moose::Manual, Moose::Cookbook
DBIx::Class