Archive for the ‘tips’ Category

Apply a role to a Moose object

Sunday, July 26th, 2009

You can apply a role to a Moose object. You can do something like

#!/usr/bin/perl -w
use strict;
use feature ':5.10';
 
{
    package foo;
    use Moose::Role;
    sub baz { say 'i can haz baz'; }
 
    package bar;
    use Moose;
    1;
}
 
my $test = bar->new;
say "i can't haz baz" if !$test->can("baz");
foo->meta->apply($test);
$test->baz;

with the following output

i can't haz baz
i can haz baz

Or from the object

#!/usr/bin/perl -w
use strict;
use feature ':5.10';
 
{
    package foo;
    use Moose::Role;
    sub baz { say 'i can haz baz'; }
 
    package bar;
    use Moose;
    sub apply_a_role { my ($self, $role) = @_; $role->meta->apply($self); }
    1;
}
 
my $test = bar->new;
say "i can't haz baz" if !$test->can("baz");
$test2->apply_a_role('foo');
$test2->baz;

With the same results.

everyday Perl : CPAN and auto-install

Tuesday, July 7th, 2009

When you install a module from the CPAN, and this module requires other modules, the cpan shell will ask you if you want to install them. When you are installing Catalyst, it may take a while, and you may not want to spend your afternoon in front of the prompt answering “yes” every 5 seconds.

The solution is to set the environment variable PERL_MM_USE_DEFAULT. Next time you want to install a big app:

PERL_MM_USE_DEFAULT=1 cpan Catalyst KiokuDB

and your done.

How to prevent some components to be loaded by Catalyst

Thursday, June 25th, 2009

Let’s say you have a “large” Catalyst application, with a lot of compoments. When you deploy your application, or when you want to test it while your developping, you may not want to have some of thoses components loaded (you don’t have all the dependencies, they are incompatible, etc…). Catalyst use Module::Pluggable to load the components, so you can easily configure this. In your application’s configuration, add:

setup_components:
  except:
    - MyApp::Model::AAAA
    - MyAPP::Model::BBBB::REST
    ...

Module::Pluggable have some other interesting features. You may have a second Catalyst application, and want to use one or more components from this one. You can easily do this:

setup_components:
  search_path:
    - MyApp
    - MyOtherApp::Model

controll xmms2 from vim

Saturday, April 25th, 2009

a really basic way to controll xmms2 from your vim session:

map <leader>xn <Esc>:!xmms2 next<CR><CR>
map <leader>xb <Esc>:!xmms2 previous<CR><CR>
map <leader>xP <Esc>:!xmms2 pause<CR><CR>
map <leader>xp <Esc>:!xmms2 play<CR><CR>
map <leader>xs <Esc>:!xmms2 stop<CR><CR>

now, type ,xn in vim, and xmms2 will start to play the next track from your playlist.

git and prove

Tuesday, April 14th, 2009

A little trick to force you to run your tests before a commit:

in a repositorie, create the following file .git/hooks/pre-commit with this content:

#!/bin/sh
if [ -d t ]; then
    res=`prove t`
    if [ $? -gt 0 ]; then
        echo "tests fails"
        exit 1
    fi
fi
if [ -d xt ]; then
    res=`prove xt`
    if [ $? -gt 0 ]; then
        echo "tests fails"
        exit 1
    fi
fi

and don’t forget to chmod with +x.

Now, when you will do your next commit, your test suit will be executed. If the tests fails, the commit will be rejected.