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

DBI connection code


use DBI;

my $dsn = "dbi:Pg:dbname=xxxxxx";
my $dbh = DBI->connect($dsn, 'postgres', '');
                                                                               
my $statuses = $dbh->selectall_hashref("SELECT id, status, name FROM statuses", "id");

Rollback within a txn_do for DBIC


try {
    $self->schema->txn_do(sub {
        # something went bad
        die 'argh';
    });
}
catch ($e) {
    # log me
    # no roll back needed
}

Bash parameter expansion

Have you ever seen ## (hash hash/pound pound) or %% (percent percent) inside a bash script and wondered what it means? It's a form of parameter expansion that allows you to manipulate your strings using regexes.

All the following commands work with a variable called $string.
'pattern' means bash pattern, or can also be an ordinary string:

  • String length: ${#string}
  • Extract a substring: ${string:position}
  • Extract a substring, specifying length: ${string:position:length}
  • Delete shortest match of substring from the beginning of string: ${string#substring}
  • Delete shortest match of substring from the end of string: ${string%substring}
  • Delete longest match of substring from the beginning of string: ${string##substring}
  • Delete longest match of substring from the end of string: ${string%%substring}
  • Find and replace first substring: ${string/pattern/replacement}
  • Find and replace all substrings: ${string//pattern/replacement}

Thanks to TheGeekStuff

vim tabs and buffers basics


in vim type  ":grep -Ri search_string *"
...it executes... press enter to get back to vim
then type ":copen" to open a window with the search results in
and you can browse the matches really easily

:help grep

then if you press enter to open one of the files found,
to get back to your original file type :buffers or :ls

:tabe #[number against file] will open the original file in a new tab
:sp #[number against file] will open the original file in a new window

:buffer [number against original file] will open the original file again
and you could make the F keys load the buffers

I prefer tabs over buffers because they're more visible
in bash vim -p file1 file2 will open 2 files in two different tabs
gt and gT will cycle through the files (I have that mapped to alt+left and alt+right)
:tabe will open a file in a new tab


Thanks Phill