Friday, July 27, 2007

Javascript Minfier

As Yahoo! suggested, minified js would improve your site performance.
http://developer.yahoo.com/performance/rules.html#minify

Luckily, Perl has JavaScript::Minifier.
but be careful, cpan will fail, u need download from web then perl Makefile.PL.

I wrote a script to minify all js files under a dir.

@Enjoy.

Labels:

Friday, July 20, 2007

Nerver/Ever do this: my $foo = $bar if ($cond);

well, I met this a few days ago.
my $foo = $bar if ($cond);
makes a lot of trouble for me.

I view that this is talked in Catalyst maillist.
http://lists.scsys.co.uk/pipermail/catalyst/2007-July/thread.html#14484

And just for your attention, be careful!

example from mst:
sub foo { my $foo if 0; $foo++; }
print foo(), foo(), foo();


guess what it is?

DO NEVER try code like this, instead try my $foo = ($cond) ? $bar : undef;

@Enjoy;

Catalyst debug trick

well, we always meet this when we develop a Catalyst application:
* we want -Debug and StackTrace or DBIC::Schema::Profiler when we develop the Catalyst App.
* we don't want -Debug like in the production server because it costs much.

Sometimes u may want a config name like debug_mode to control whether we want or not.
Here comes my solution:
__PACKAGE__->setup();

__PACKAGE__->log->levels('error', 'fatal'); # for real server
if( __PACKAGE__->config->{debug_mode} ) {

__PACKAGE__->log->enable('debug', 'info', 'warn'); # for developer server
{
# these code are copied from Catalyst.pm setup_log
no strict 'refs';
my $class = __PACKAGE__;
*{"$class\::debug"} = sub { 1 };
}

my @extra_plugins = qw/ StackTrace DBIC::Schema::Profiler /;
__PACKAGE__->setup_plugins( [ @extra_plugins ] );
}
Full code please check Foorum.pm

now u can set debug_mode: 1 in _local.yml when u develop and forget it in production server.

Explanation:
* why we write code after __PACKAGE__->setup(); is because we want __PACKAGE__->config->{debug_mode}. config is setup in ->setup().
* *{"$class\::debug"} = sub { 1 }; means $c->debug is on. code from Catalyst.pm
* __PACKAGE__->setup_plugins( [ @extra_plugins ] ); will setup extra_plugins whenever u want.

@Enjoy;

Labels: ,

Wednesday, July 18, 2007

Perl ssh `` tip

my $result = `ssh $IP 'ls -ls /root;exit'`;
print "result = $result";