5 posts tagged “perl”
I will show how I load config data with catalyst.
AIM
Load config data not only for the catalyst application(MyApp::Web) but also CLI , an another Catalyst Application(e.g.. MyApp::AdminWeb) , etc...
MyApp::Config
This is how I implement config class. there are 2 points. and after set up this way, you can use this class whereever you want. By the way, I am using Config::Multi which is just my choice , you can chose something else you prefere .
- using Class::Singleton
- using MyAPp::Utils instead of Catalyst::Utils
package MyApp::Config;
use strict;
use warnings;
use Config::Multi;
use MyApp::Utils;
use base 'Class::Singleton';
our $FILES ;
sub _new_instance {
my $cm = Config::Multi->new(
{
dir => MyApp::Utils::path_to('conf')->stringify ,
app_name => 'myapp' ,
extension => 'yml'
});
my $config = $cm->load();
$FILES = $cm->files;
return $config;
}
sub files {
return $FILES;
}
1;
MyApp::Utils
If I use Catalyst::Utils then the MyApp::Config has dependancy with Catalyst which I want to avoid it so I use this class instead.
package MyApp::Utils;
use warnings;
use strict;
use Path::Class::Dir;
use Path::Class::File;
use FindBin;
sub home {
return $ENV{MYAPP_HOME} || Path::Class::Dir->new( $FindBin::Bin, './../' );
}
sub path_to {
my ( @path ) = @_;
my $path = Path::Class::Dir->new( &home , @path );
warn $path;
if ( -d $path ) { return $path }
else { return Path::Class::File->new( &home, @path ) }
}
1;
MyApp::Plugin::Config
package MyApp::Plugin::Config;
use strict;
use warnings;
use MyApp::Config;
use NEXT;
our $VERSION ='0.02';
sub setup {
my $c = shift;
my $config = MyApp::Config->instance();
if( $c->debug ) {
my $files = MyApp::Config->files();
for my $file ( @{$files} ) {
$c->log->debug( 'Load Config ' . $file );
}
}
$c->config( $config ) ;
$c->NEXT::setup( @_ );
}
1;
MyApp::Web
package MyApp::Web;
use strict;
use warnings;
use Catalyst::Runtime '5.70';
use Catalyst qw/+MyApp::Plugin::Config/;
our $VERSION = '0.01';
__PACKAGE__->setup;
1;
conf/myapp_web.yml
---
name: Config Sample
MyApp::Web::Controller::Root
package MyApp::Web::Controller::Root;
use strict;
use warnings;
use base 'Catalyst::Controller';
__PACKAGE__->config->{namespace} = '';
sub default : Private {
my ( $self, $c ) = @_;
$c->response->body( $c->config->{name} );
}
sub end : ActionClass('RenderView') {}
1;
Conclusion
when I use Catalyst::Plugin::Config** I could not use the module from outside of catalyst
but with this implementation I can use MyApp::Config whereever I want! hehehe.
perl-mongers.org < same article in japanese.
I released Net::Twitter::Diff at CPAN.
What you can do with this module are...
- Use this module when you want to know all your followers who you are not following.
- Use this module when you want to compare following with your twitter friend.
- Use xfollowing and xfollowers instead of Net::Twitter->following(), Net::Twitter->followers() when you have more thatn 100 twitter's. following and follower methods have 100 limitation because of Twitter API. xfolloing() and xfollowers() is support more than 100 but it is unofficial use , the way I implement is not on the Twitter API Document. You can find how if you ask to google :-)
use Net::Twitter::Diff;
use Data::Dumper;
my $diff = Net::Twitter::Diff->new( username => '******' , password => '******');
print Dumper $diff->diff();print Dumper $diff->comp_following( 'somebody_twitter_name' );
print Dumper $diff->xfollowing();
print Dumper $diff->xfollowers();
- You must know one thing : you have limitation for 70 times per hour for Twitter API request. so be aware.
Catalyst::Model::DBIC::Schema make our life so easy. I am loveing it!
But one thing I do not like is missing POD. This module generate Schema without POD.
You know we can set COMMENT for each table and field with SQL. So if the module read SQL comments and set it as POD then it will be really nice . Of course having more information such as type , unique ,etc... is better.
Perl Hacks Book introduce Pod::Webserver but I recomend Pod::ProjectDocs .
That is why I update Pod::Html::HtmlTree and recommend Pod::ProjectDocs in the document , cause Pod::ProjectDocs is better module but hard to find.
Before this pluign come out , I was a Matrix Guy who looking at Green text on Black backgorund... Many girls who like Matrix ask me out when I am looking at sexy green and black display but no more! I want to live in real world! I need more color!!!
What this plugin does is simple. just add color for your log message with $c->log->color() method. When you use this color() method , the log should be wrotten as debug mode.
I also thought it's good idea to have option to set color for every log methods such as debug, info , error ,critical...
we did implement couple more modues at the Newbiethon party , I think masap@cpan soon release it. One is related Catalyst one is for Cache.