[Dancer-users] Dancer::Plugin best practices.

Carlos Ivan Sosa gnusosa at gnusosa.net
Sat Feb 19 00:48:35 CET 2011


I don't know if these is really a query about Dancer::Plugin or simple
Perl module
usage in which I'm a bit confused. But ever since I started my
mini-blog engine with
several of Dancer::Plugin's, it has grown to long to follow, so I
decided to separate
it by routes just like the cookbook suggested.
http://search.cpan.org/dist/Dancer/lib/Dancer/Cookbook.pod#Using_the_prefix_feature_to_split_your_application

My problem comes when I want to separate functions, and interexchange
them between each
different prefix route file in which my routes are divided.

For instance, in my feed routes file I call a function that creates a
database handler from Dancer::Plugin::Database,
and from most of my routes files I use this database handler. But each
section does a different SQL query to a different
table depending on the entry.

So I came up with a package that calls Dancer::Plugin:Database, and
works around with the two tables logic.

package log::db;

use strict;
use Dancer::Plugin;
use Dancer::Plugin::Database;

register _connect_db => sub {
	my $db = database();
	return $db;
};

register _sql => sub {
	my ($table, $url) = @_;
	my $db = _connect_db();
	my $sth = $db->quick_select($table, { url => $url})
	or die $db->errstr;
	return $sth;
};

register_plugin;

1;
...

This is the feed route file:

package log::feed;

use Dancer ':syntax';
use log::db;
use Dancer::Plugin::Feed;
use DateTime::Format::Strptime;

sub _feed_sql {
	my $db = _connect_db();
	my $sql = 'select id, title, url, text, datetime from entries order
by id desc';
	my $sth = $db->prepare($sql) or die $db->errstr;
	$sth->execute or die $sth->errstr;
	my $entries = $sth->fetchall_arrayref({});
	return $entries;
}
...

So I don't know if I'm doing the best practice habit, because that is
my work around. So I'm asking what would be the
best way or the "Perl way" for a kwalitee package. I find these more
clean and helpful than doing a full-on Perl module,
which never seem to work for me. Modules are still too abstract for me.

My apologies for a huge mail.

Cheers.


More information about the Dancer-users mailing list