difference between $c->user and $c->user->obj

17 June 2007


when we use Catalyst::Plugin::Authentication::Store::DBIC::User, we will have $c->user and $c->user->obj. but most of people are confusing with them I guess.

differences:
* $c->user is ISA Catalyst::Plugin::Authentication::Store::DBIC::User, while $c->user->obj is just something like sub obj in Catalyst::Plugin::Authentication::Store::DBIC::User. see:
__PACKAGE__->mk_accessors(qw/id config obj store/);
# .skip
bless {
id => $id,
config => $config,
obj => $user_obj,
}, $class;

* why $c->user->username is the same as $c->user->obj->username is because of
sub AUTOLOAD {
my $self = shift;
(my $method) = (our $AUTOLOAD =~ /([^:]+)$/);
return if $method eq "DESTROY";

$self->obj->$method(@_);
}
. when we call ->username of $c->user. it will call sub username in Catalyst::Plugin::Authentication::Store::DBIC::User. then sub AUTOLOAD works here, it call $self->obj->$method(@_) indeed. ($self is $c->user).

so there is something interesting..
* because we have sub for_session in Catalyst::Plugin::Authentication::Store::DBIC::User, we will have $c->user->for_session ( but not $c->user->obj->for_session if user table don't have "for_session" column ).
* when I see something "*user = \&obj;" in the code. I know I can use $c->user->user->username. it's the same as $c->user->obj->username. (JOKE!)

for most situation. I suggest use $c->user->obj->column_name because that's more direct (no AUTOLOAD involved).

Enjoy!.

Catalyst and DBIx-Class tip

17 June 2007


* Catalyst Tip: use set_authenticated instead of login when password is SHA1 hashed.

well, Foorum use SHA1 to encrypt the password. so no one knows the original password including me (db do not store original password).
so if u are trying $c->login($username, $password_sha1_hashed);, it will fail.
if u are checking the module of Catalyst::Plugin::Authentication::Credential::Password which sub login is there, we would know the solution:
$c->set_authenticated($user);

that does the same as login but no password check.

* DBIx::Class Tip: attribute offset.

sometimes when we run a SQL, that's not something like LIMIT 0,6 LIMIT 6,6
it might be something like LIMIT 5,6
well, the attributes of "rows" and "page" does something like LIMIT 6,6. but no luck with 5,6.
so if u want run a SQL with LIMIT 5,6, u might can try
search( {}, {
rows => 6,
offset => 5
} );
that would be what u want.

be careful, offset do no create a Data::Page object for you. I mean ->pager. u need create that yourself.

Enjoy!

be careful

06 June 2007


my @a = ( 'a', $c->req->param('a'), 'b', $c->req->param('b'), 'c', undef , 'd',, 'f' );$c->res->body(Dumper(\@a));
when we try "?b=1", output:
$VAR1 = [
'a',
'b',
'1',
'c',
undef,
'd',
'f'
];

see? $c->req->param('a') is NOT undef.
so when u try something like
$rs->update( {
column1 => $c->req->param('column1'),
column2 => $c->req->param('column2')
} );
then if column1 param is not specified, this would raise error because $rs try to update column1, column2, $c->req->param('column2'). we want 4 but u give me 3.

so man, be careful of that. :)

Foorum v0.03

20 May 2007


I just release another version of Foorum at http://code.google.com/p/fayland.

CHANGES:
- remove Plugin::Email. insert into table scheduled_email and use a cron script to send. (19/5/2007)
- remove Plugin::Scheduler. all cron scripts goes to /bin/cron (19/5/2007)
- bug fixes (19/5/2007)

I remove the Email Plugin and create a new table scheduled_email to contain the email content, then use /bin/cron/send_scheduled_email to send emails

and remove the Scheduler. I think crontab is better

and remove such files can reduce the memory usage under mod_perl I think

http://fayland.googlecode.com/files/Foorum-0.03.tar.gz

Enjoy!

Amazing Danga

13 May 2007


mm, after read the pdf of http://www.danga.com/words/2007_04_linuxfest_nw/linuxfest.pdf, I find myself really excited with the technology of Danga (now a part of Six Apart).

we use a lot of memcached in Zorpia.
but other technology like MogileFS, Perlbal and Gearman are TRULY exciting too.

I spent my whole weekend to try them and found that I do need recommend them. :)

Foorum v0.02

08 May 2007


I just release the new version of Foorum v0.02 at http://code.google.com/p/fayland/.

no big changes. u can download it from http://fayland.googlecode.com/files/Foorum-0.02.tar.gz

Thanks.

Catalyst PageCache with I18N

07 May 2007


well, I have something like as follows in the Foorum Root.pm sub auto:
 # internationalization
$c->stash->{lang} = $c->req->cookie('pref_lang')->value if ($c->req->cookie('pref_lang'));
$c->stash->{lang} ||= $c->user->lang if ($c->user_exists);
$c->stash->{lang} ||= $c->config->{default_pref_lang};
if (my $lang = $c->req->param('set_lang')) {
$lang =~ s/\W+//isg;
if (length($lang) == 2) {
$c->res->cookies->{pref_lang} = { value => $lang };
$c->stash->{lang} = $lang;
}
}
$c->languages( [ $c->stash->{lang} ] );

but the Catalyst::Plugin::PageCache does NOT get along well with the I18N's languages.

so that I patch this module for a bit. to create a new plugin based on that:
package Catalyst::Plugin::PageCacheWithI18N;

use strict;
use warnings;
use Class::C3;
use vars qw/$VERSION/;
$VERSION = '0.01';
use base qw/Catalyst::Plugin::PageCache/;

sub _get_page_cache_key {
my ($c) = @_;

my $key = $c->next::method(@_);
my $lang = $c->req->cookie('pref_lang')->value if ($c->req->cookie('pref_lang'));
$lang ||= $c->user->lang if ($c->user_exists);
$lang ||= $c->config->{default_pref_lang};
if (my $set_lang = $c->req->param('set_lang')) {
$set_lang =~ s/\W+//isg;
if (length($set_lang) == 2) {
$lang = $set_lang;
}
}
$key .= '#' . $lang if ($lang);
return $key;
}

1;


Links: http://fayland.googlecode.com/svn/trunk/Foorum/lib/Catalyst/Plugin/PageCacheWithI18N.pm

WWW::Mechanize::GZip

18 April 2007


well, for some web like mail.yahoo.com, it compressed using gzip.
so when we get the page content by WWW::Mechianize, it may return some content with gziped.

so I would suggest to use WWW::Mechanize::GZip. it handles gzip pretty well.

have fun.

Catalyst::Plugin::tv_interval

14 April 2007


I just create a new Catalyst plugin Catalyst::Plugin::tv_interval for profiling using.

this module is pretty simple. the main stuff is from Time::HiRes.
I use the "tv_interval" of Time::HiRes as this module name.

the usage is pretty simple. check the pod in CPAN.

have fun!

encrypt a file then decrypt it use key with Perl

05 April 2007


well, computer crashes without any reason sometimes, so we need save some important data into somewhere like Email or one subversion repos. but plain text is not so security, it would be better if we use one "key" to encrypt a file, then use this "key" to decrypt it when we need.

sure, I know winrar or something else can do that. but Perl can do that too. :)
just with Crypt::CBC.

I write a simple script to do this task. that's in http://fayland.googlecode.com/svn/trunk/script/misc/dencrypt.pl
pretty easy but useful for me.

try with:
perl dencrypt.pl encrypt mysecretkey E:/1.txt
then decrypt it use:
perl dencrypt.pl decrypt mysecretkey E:/1.txt.encrypt

That's it, enjoy!