Thursday, September 03, 2009

use Moose; or no Moose;

recently "Moose or no Moose" is spreading to the Perl world.

Moose or No Moose
Command Line Moose or Knowing what You Don't know.
Re: Moose Or No Moose
Take arms against a sea of troubles
Re: Re: Moose Or No Moose
Moose Startup Time over Time

basically I agree with Adam Kennedy that Array::Compare shouldn't based on Moose. Test::Warn is a popular module and Array::Compare without Moose can totally ease the install of the Test::Warn.

in the meantime, I would agree with Stevan Little that we shouldn't hold the usage of the Moose. it's a great module with great idea and great contributors. I use it in my daily life and it works great. once more and more people/module use Moose, the problem wouldn't become a problem.

I think Padre should use Moose, I know Adam would reject that, but I insist on that Padre will benefit from Moose even Class::XSAccessor is the fastest accessor module now. Padre will be in a better structure and much more easy to read if it's coded with Moose. Plugin/Document/Highlighter would be easily written and plugged.

another thing is that I met the problem before. Long time ago people rate down WWW::Contact because it uses Moose. but I think it's not true. Moose helps me to build the module.

at last and in short, I think simple module (like only use the accessor of Moose) shouldn't use Moose but large software/module like Padre should use Moose.

Thanks. just personal thoughts.

Labels:

Friday, January 09, 2009

Learn Moose X: handles

Writing Moose is really very fun and happy.

I'm happy that sunnavy picked up the Moose for Net::Google::Code.

Today I'm implementing some attributes for the Net::Google::Code. the code would be something like
    use Net::Google::Code;
my $project = Net::Google::Code->new( project => 'net-google-code' );
print join(', ', @{ $project->owners } );
so in old plain Perl code, you must write code in Net/Google/Code.pm or else you need sub owners { Net::Google::Code::Home->new()->owners }, something like that.

with Moose, with hanldes, we can simply write code like
has 'home'  => (
isa => 'Net::Google::Code::Home',
is => 'ro',
lazy => 1,
default => sub {
require Net::Google::Code::Home;
Net::Google::Code::Home->new( parent => $_[0] );
},
handles => [ 'owners', 'members' ],
);

in this way, it makes the code really clean and small.
http://code.bestpractical.com/bps-public/Net-Google-Code/trunk/lib/Net/Google/Code.pm
http://code.bestpractical.com/bps-public/Net-Google-Code/trunk/lib/Net/Google/Code/Home.pm

the magic is $project->owners == $project->home->owners, more example can be found: http://search.cpan.org/src/DROLSKY/Moose-0.64/t/020_attributes/010_attribute_delegation.t.

if you never use Moose before, you SHOULD use Moose today!

Thanks.

Labels:

Sunday, October 19, 2008

Acme::PlayCode and MooseX::Types::IO

I wrote Acme::PlayCode and MooseX::Types::IO and uploaded them to CPAN today.

Acme::PlayCode is just from:
learn PPI 1: double to single
Learn PPI 2: exchange in condition
I don't think it's useful now so I put it as Acme:: for fun.
Acme::PlayCode use MooseX::Object::Pluggable, so everyone can write a plugin for it.
the idea is directly from Devel::REPL. if you want to learn Moose, you should read the source. it's pretty elegant and clean. I think I'll write a plugin for it some days later.

MooseX::Types::IO is IO and IO::All type for Moose. I thought I would use it in Acme::PlayCode so I wrote it, but I didn't use it at last. hmm. but anyway, I'll use it one day later.

Have fun. Thanks.

Labels: , ,

Tuesday, October 14, 2008

Catalyst on Moose

http://search.cpan.org/~mramberg/Catalyst-Runtime-5.8000_01/

try it. it's new and on Moose.

$ cpan M/MR/MRAMBERG/Catalyst-Runtime-5.8000_01.tar.gz

ok, it fails, but u can download the source code and view a bit. I'm sure the next developer version will be kicked out ASAP.

Enjoy!

=updated

$ cpan parent
$ cpan M/MR/MRAMBERG/Catalyst-Runtime-5.8000_02.tar.gz

it should be working.

Thanks.

Labels: ,

Saturday, October 11, 2008

Learn Moose 4: augment and 0.02 IRC::Bot::Log::Extended

augment can be used as trigger in sub, to let user do something he wants.

for example, I want to let user custom $message before insert into $file in IRC::Bot::Log::Extended.
so that he can skip some lines or URI::Find some URLs.

full code are:
http://fayland.googlecode.com/svn/trunk/CPAN/IRC-Bot-Log-Extended/examples/02b_with_filter.pl
http://fayland.googlecode.com/svn/trunk/CPAN/IRC-Bot-Log-Extended/examples/03advanced.pl

I also write a simple test script:
http://fayland.googlecode.com/svn/trunk/script/learn_moose/008_augment.pl

hmm, that's simple. just think about u defined a virtual sub in parent pm, then user can override it in child pm.
nothing special.

Thanks.

Labels: ,

Friday, October 10, 2008

IRC::Bot::Log::Extended

well, I do NOT want to miss the irc talk history when I go sleep. I want to store the talk into plain text, and read it when I'm free.

that's a simple and straight. IRC::Bot is good. really fast and have a logger built-in.
The only thing I don't like IRC::Bot is that it stores all talk history into one file channel.log regardless channel and day. I want to split them, to ease the reading.

so that's why IRC::Bot::Log::Extended is kicked out.
and the example script is http://fayland.googlecode.com/svn/trunk/script/web/irclog.pl

so it's logging for me. all #catalyst #dbix-class #tt #moose talk history.

Thanks.

Labels: , ,

Monday, September 29, 2008

My 3rd CPAN module Today: Sphinx::Control

the code and idea is directly from Lighttpd::Control and Nginx::Control.

Sphinx::Control controls Sphinx searchd.

Thanks.

Labels:

Thursday, September 18, 2008

Learn Moose 3: around

for example I want a shuffled list everytime when I can $t->shuffled_list;
I know I can use a sub to do the request. but with Moose, it's much more elegant.

the main code is like:
around 'shuffled_list' => sub {
my $next = shift;

my $ret = $next->(@_);
$ret = [ shuffle( @$ret ) ];

return $ret;
};

full source code can be found in http://fayland.googlecode.com/svn/trunk/script/learn_moose/002_around.pl

the output looks like:
1, 10, 6, 4, 2, 7, 9, 8, 3, 5
3, 4, 7, 2, 1, 9, 5, 6, 8, 10
Here we use 'around' instead of 'before' or 'after' because 'before' doesn't work according to this FAQ: http://search.cpan.org/~drolsky/Moose-0.57/lib/Moose/Cookbook/FAQ.pod#How_can_I_affect_the_values_in_@__using_before?

the example above is a case of 'deflate' as http://search.cpan.org/~drolsky/Moose-0.57/lib/Moose/Cookbook/FAQ.pod#How_can_I_get_Moose_to_inflate/deflate_values_in_the_accessor?

please read the FAQ and Cookbook for more details.

Thanks.

Labels:

Thursday, September 11, 2008

MooseX::TheSchwartz

I just released a new version of MooseX::TheSchwartz today.

it's written on the way when I learn Moose. and most of the code is copied from TheSchwartz and TheSchwartz::Simple.
yet that's cool to write such things with Moose.

Moose rocks!

Labels: ,

Friday, September 05, 2008

Learn Moose 2: trigger

I want a scoreboard on MooseX::TheSchwartz.
so I defined one like "has 'scoreboard' => ( is => 'rw', isa => 'Str' );", but that's not what I want for all, I want more.
I want it to be a temp file related to $$ when I set it as 1 or 'on'.
I want I can custom a temp file for it.

I know I can write a method like the one in TheSchwartz. but with Moose, it's much more elegant as what u want!
has 'scoreboard'  => (
is => 'rw',
isa => 'Str',
trigger => sub {
my ($self, $dir) = @_;

return unless $dir;
# no endless loop when it's a file
if ($dir =~ /\/theschwartz\/scoreboard\./is) {
# get the real dir from $dir regardless a file
my (undef, $dir) = File::Spec->splitpath( $dir );
unless (-e $dir) {
mkdir($dir, 0755) or die "Can't create scoreboard directory '$dir': $!";
}
return;
}

# They want the scoreboard but don't care where it goes
if (($dir eq '1') or ($dir eq 'on')) {
$dir = File::Spec->tmpdir();
}

$dir .= '/theschwartz';
unless (-e $dir) {
mkdir($dir, 0755) or die "Can't create scoreboard directory '$dir': $!";
}

$self->{scoreboard} = $dir."/scoreboard.$$";
}
);
now I have all I want.

just be careful, when u want to change itself in trigger like "$self->{scoreboard} = 'xxx'" or "$self->scoreboard('xxx');", u must make sure there isn't a endless loop since change itself will call trigger again.

test code can be found here: http://fayland.googlecode.com/svn/trunk/script/learn_moose/001_attribute_trigger.pl

Thanks.

Labels:

Thursday, September 04, 2008

Learn Moose 1: subtype

finally I wrote my first Moose script.
mainly I want to write a MooseX::Schwartz if I have enough time.

Moose is very powerful.
I want a common function like set a flag to debug, besides that, we can define debug logger sub ourself.
something like:
->verbose(1);
->verbose(0);
->verbose( sub {
my $msg = shift;
$msg =~ s/\s+$//;
print STDERR "[CUS] $msg\n";
} );
# then call ->debug depends on ->verbose.

final code is here:
http://fayland.googlecode.com/svn/trunk/script/learn_moose/007_subtype.pl

I'm not a Moose guru, but Moose sounds pretty exciting!

Labels: