Monday, February 23, 2009

Perl tip: can returns coderef

well, I just released the MooseX::Dumper today.

miyagawa on irc.perl.org #moose just told me that ->can() returns the coderef, so that I don't need to do follows:
        if ( $self->dumper_class->can($meth) ) {
my $class = $self->dumper_class;
my $val = eval "${class}::${meth}(\@_)"; # no critic
return $val;
}
Instead, just do:
        if ( my $dump_code = $self->dumper_class->can($meth) ) {
return $dump_code->(@_);
}
Yup, much more simpler and cleaner.

Thanks, miyagawa. 0.02 is on the way. :)
Sunday, February 22, 2009

step by step: make daydayup as a WinXP service

even it's very old, it's still very useful: http://www.perlguy.com/articles/nt_service.html

step 1, download the toolkit from http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

step 2, cmd: then
C:\Documents and Settings\Administrator>INSTSRV DayDayUp "C:\Program Files\Windows Resource Kits\Tools\srvany.exe"

The service was successfuly added!

Make sure that you go into the Control Panel and use
the Services applet to change the Account Name and
Password that this newly installed service will use
for its Security Context.

step 3, cmd, regedit, then find "DayDayUp"
* Right click on the key and choose New, then Key. This will create what looks like a new folder. Name the new key Parameters.
* Right click on Parameters and select New, then String Value. In the space provided, type Application.
* Again, right click on Parameters and select New, then String Value. In the space provided, type AppParameters.
* Right click on Application and choose Modify, then type in the path to your Perl binary like C:\strawberry\perl\bin\perl.exe
* Next, right click on AppParameters and select Modify. type E:\Fayland\gsvn\CPAN\day_day_up\bin\day_day_up daemon 3001

OK, everything is done now. if you go Services, you'll find DayDayUp in list, and you can start it (it will be started whenever computer gets start)

Thanks.

Labels:

today's module: Scope::Guard

Updated: Thanks for Eddy to point out the errors. :)

I have been very happy with Scope::Guard recently.

mainly the module can reduce the size of the code if you use it correctly.

for example, we have some piece of code like follows to deal with Sphinx error.
sub while_error {
my ( $ret ) = @_;

my $use_db = { use_db_please => 1 };

# 1, connection to {localhost}:{3312} failed: Connection refused
if ($ret->{error} =~ /Connection refused/is) {
faked_try_to_reconnect();
print STDERR "$ret->{error}\n";
return $use_db;
}
# 2, received zero-sized searchd response
elsif ($ret->{error} =~ /zero-sized searchd/) {
faked_try_to_re_search();
print STDERR "$ret->{error}\n";
return $use_db;
}
# 3, unknown local index
elsif ( $ret->{error} =~ /unknown local index/ ) {
faked_try_to_re_index();
print STDERR "$ret->{error}\n";
return $use_db;
}
# 4, recv: Connection reset by peer
elsif ( $ret->{error} =~ /Connection reset by peer/ ) {
print STDERR "$ret->{error}\n";
return $use_db;
}
return $ret;
}
OK, it's not so ugly.
but with Scope::Guard, we can rewrite it much more clear and small-size.
sub while_error {
my ( $ret ) = @_;

my $use_db = { use_db_please => 1 };

my $sg = Scope::Guard->new( sub {
print STDERR "$ret->{error}\n";
} );

# 1, connection to {localhost}:{3312} failed: Connection refused
if ($ret->{error} =~ /Connection refused/is) {
faked_try_to_reconnect();
return $use_db;
}
# 2, received zero-sized searchd response
elsif ($ret->{error} =~ /zero-sized searchd/) {
faked_try_to_re_search();
return $use_db;
}
# 3, unknown local index
elsif ( $ret->{error} =~ /unknown local index/ ) {
faked_try_to_re_index();
return $use_db;
}
# 4, recv: Connection reset by peer
elsif ( $ret->{error} =~ /Connection reset by peer/ ) {
return $use_db;
}

$sg->dismiss();

return $ret;
}
I'm not sure if you're happy with it or not, but I like it. :)
I had used it in http://code.fayland.org/fayland/CPAN/day_day_up/lib/DayDayUp/Backup.pm. that's just another example.

Thanks and Enjoy!
Sunday, February 15, 2009

CPAN Top 100

Adam Kennedy just published his "The CPAN Top 100" today.

I just found that I take 11 places in "CPANTS Heavy 100". one is the Catalyst App Foorum and the others are Padre plugins. well, that's not a prize, I admire those listed in "CPANTS Volatile 100". :)

Thanks.
Friday, February 13, 2009

linux tool: logrotate

logrotate is very useful tool in linux.

see I run sphinx searchd and it creates logs. it would become very large if I leave it alone.
and I have another cron script like theschwartz_worker.pl and it writes the debug info into file when I use $job->debug

those are very common situation. and with logrotate, it becomes simple.
place your files into /etc/logrotate.d/
like /etc/logrotate.d/searchd
/var/log/searchd/foorum.log {
weekly
rotate 5
compress
delaycompress
create 640 root root
postrotate
killall -SIGUSR1 searchd
endscript
}
like /etc/logrotate.d/theschwartz
/var/log/theschwartz.log {
weekly
rotate 10
compress
delaycompress
create 640 root root
}
After you write your conf, you can start test by:
$> logrotate -d /etc/logrotate.d/searchd
rotating pattern: /var/log/searchd/foorum.log weekly (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/searchd/foorum.log
log does not need rotating
not running postrotate script, since no logs were rotated

google around to find more details. or just man logrotate.

Thanks.

Labels: ,

Monday, February 09, 2009

moving fayland.org to dreamhost

due to finance issue, I decided to move fayland.org to my dreamhost server.
it's much cheap than the fee inside China. and now my dreamhost server has 3 sites in.

just a note.

mod_autoindex Options on irclog.foorumbbs

well, after a bit search, I'm very happy to see http://irclog.foorumbbs.com/ is behaving like what I want now.

no big magic, just read through http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html
then create a .htaccess in my dreamhost server.
IndexOrderDefault Descending Date
IndexOptions +FancyIndexing +FoldersFirst +NameWidth=*
just a note. :)

Labels: