A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Perl: Debugging DBIx::Class with DBI

If your code uses DBIx::Class, but you want to debug using DBI:

     while (my ($name, $s) = each %{{
         'MySQL' => ...->get_schema(),
         'SQLite' => ...->get_schema(),
     }}) {
         diag("\n\n>> $name schema\n\n");
         foreach my $table qw(a b c) {
             my $r;
             eval { $r = $s->storage->dbh->selectall_arrayref("SELECT * FROM $table"); };
             if ($@) { note("No table $table"); }
             else { note(scalar(@$r)." rows in $table"); }
         }
     }

My .bashrc config

# general:
export PATH="~/scripts:$PATH"

alias ls="ls -F --color"
alias grep="grep --color=auto"

# git aliases
function gitdiff() {    git diff --no-ext-diff -w "$@" | vim -R -
}

function gitlog() {
    git log --name-only "$@"
}

function gitcommit() {
    git --no-pager diff --no-ext-diff
    echo
    read -p "Are you sure you want to commit these changes? " yn
    case $yn in
        [Yy]* ) git commit "$@"; break;;
    esac
}

# git prompt
host_colour="01;34"
export PS1="\[\033[${host_colour}m\]\h\[\033[00m\]/\u \t \w \$ "

function __git_commits_behind {
    if [ -d .git ]
    then
        git st | perl -ne'm{Your branch is behind.+by (\d+) commit} && print "behind $1< "'
    fi
}

function __git_commits_ahead {
    if [ -d .git ]
    then
        git st | perl -ne'm{Your branch is ahead of.+ by (\d+) commit} && print "ahead $1> "'
    fi
}

function update_prompt {
    local branch=$(git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1) /')
    export PS1="\[\033[${host_colour}m\]\h\[\033[00m\]/\u \[\033[${branch_colour}m\]${branch}$(__git_commits_behind)$(__git_commits_ahead)\[\033[00m\]\t \w \$ "
}
export PROMPT_COMMAND=update_prompt

Switch for Perl done right

Instead of the horribly flaky 'Switch' module, from Perl 5.10 you can now do:

use feature "switch";

given($_) {
    when (/^abc/) { $abc = 1; }
    default { $nothing = 1; }
}
 
See http://perldoc.perl.org/perlsyn.html#Switch-statements

vimdiff fun with git

Step 1: add this to your .gitconfig
[diff]
  external = git_diff_wrapper
[pager]
  diff =


Step 2: create a file named git_diff_wrapper, put it somewhere in your $PATH
#!/bin/sh
vimdiff "$2" "$5"


Still have access to the default git diff behavior with the --no-ext-diff flag. Here’s a function to put in your bash configuration files:
function git_diff() {
  git diff --no-ext-diff -w "$@" | vim -R -
}


Source: http://technotales.wordpress.com/2009/05/17/git-diff-with-vimdiff/

Git setup

git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com

git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

git config --global core.editor vim