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

Test MySQL date calculations

This is a way to test certain SQL queries involving dates. Say a query is supposed to select "Last Saturday", regardless of the date on which it runs:
SELECT DATE_SUB(CURDATE(), INTERVAL WEEKDAY(DATE_SUB(CURDATE(), INTERVAL 5 DAY)) DAY) AS 'Last Saturday';

How do you know if it really works on all days? This command will iterate through all the days in the month, and run the query with each date to see what it gets:

for day in $(seq 1 30)
do
    echo Nov $day, $(mysql -u sdkain -h db21 lf_wh -e"SELECT DATE_SUB('2010-11-$day', INTERVAL WEEKDAY(DATE_SUB('2010-11-$day', INTERVAL 6 DAY)) DAY) AS 'Last Sunday'")
done

Remember to test for when the year changes, and on leap years.

Automatically diagram an SQL database

Use cases:
  • Designing a new schema from scratch, using a GUI
  • Automatically rendering a diagram of an existing SQL schema

2010:
  • The least worst option is MySQL Workbench. The GUI is poor: http://wb.mysql.com/
  • This one uses Javascript. No features, but it works: http://code.google.com/p/database-diagram/

Making a Perl module

Object oriented convention in Perl:

sub new
{  
    my ($class, %params) = @_;
    my $self = \%params;
    bless $self => $class;
    return $self;
}

Use Firefox as if it were WWW::Mechanize::Shell

Depends on plugin Mozrepl: https://github.com/bard/mozrepl/wiki
Use WWW::Mechanize::Firefox

HTTPS in Perl

After Crypt::SSLeay or IO::Socket::SSL is installed, LWP will automatically work for URLs starting https://

Have vi detect perl mason .html files

In ~/.vim/ftdetect/mason.vim:

" Mason detection
au BufRead,BufNewFile *
\   if search('^\s*<%\(args\|once\|init\|perl\|shared\|attr\)>\s*$','n')
\|    set filetype=mason
\|  endif

Eclipse features

  • Syntax highlighting
  • Code folding
  • Go to source of library
  • Look up Javadoc/Perldoc
  • Automatic refactoring
  • File navigator