Amass

Perl: Life as a one-liner

December 29th, 2006 Luke

I found this on perlmonks.org, and thought it was pretty clever:

$state{tired}?sleep(40):eat($food);

It was in the signature of corenth at the time.

Cool Perl Snippet: Global Config Files

December 22nd, 2006 Luke

I’ve recently been doing a lot of work with databases, and been more than a mite jealous of PHP’s include() syntax and how it often gets used to store things like database configuration values into a single file. However, I’ve managed to find a way to do it with Perl. Here’s how:

First off, you have your file that will be USING the config file. Here’s what it should look like:

#!/usr/bin/perl -w use strict; # look Ma! it works under strict! my %config; require './config.pl'; print "Config Value: $config{foo}\n";

We declare a %config hash just before require‘ing the configuration file, in order to keep strict from throwing an error. Here’s what your config file should look like:

#!/usr/bin/perl -w our %config = ( foo => 'bar', baz => 'bat' );

The output from our main file will be:

Config Value: bar

And there you go. Global and easy to use config files in perl. Take that, PHP.

Perl/DBI Snippet: Randomization

December 21st, 2006 Luke

Here’s another snippet from some of my work on my latest project. I needed to randomly choose a certain ID out of a table, based on certain attributes. The method that I used follows this order:

  • Count number of ID’s we can use
  • Store all valid ID’s into an array
  • Randomly choose an element inside the array, and use it’s value as the ID

I figured out that there was a really cool way to do this, using a for loop, and this is how I did it:

my $validIDs = $dbh->do("SELECT id FROM table WHERE valid = 1"); my $randomID = int(rand($validIDs)); $sth = $dbh->prepare("SELECT id FROM table WHERE valid = 1"); $sth->execute(); my $IDnumber; $sth->bind_columns(\$IDnumber); my @IDs; for(my $i = 0; $sth->fetch; $i++) { $IDs[$i] = $IDnumber; }

At this point, we can refer to the random ID value using the syntax $IDs[$randomID]. Neat.

Perl/DBI Snippet: retrieve a hash of column values

December 21st, 2006 Luke

Recently, I had to retrieve a large number of attributes from a MySQL table. When doing this, I use the prepare(),execute(),fetch() method of database interaction. Now, this is all well and good, but I don’t want to be creating five new scalars for each time I need to fetch that much data. I find it much easier to store it all inside a hash, and then just refer to bits of the hash by their column names. Here’s the solution I found, by using forced context:

$sth = $dbh->prepare("SELECT id, name FROM users"); my %user; $sth->execute(); $sth->bind_columns(\@user{qw(id name)}); while($sth->fetch) { ... }

Parakeet: Ready to go!

December 11th, 2006 Luke <

Well, Parakeet is finally finished. It took me about a month and a half, but it’s ready to go. I’ve transferred it from my testing sandbox to it’s own directory, so it can now be found at girasquid.com/parakeet, and it all works. There are minor display bugs(PNG transparency on the register page under IE6+, CSS centering isn’t working on some pages), but the application itself works.

In a way, I’m almost sad to see it finished. It gave me something to focus on, and something to think about during classes that were otherwise pretty boring.

I spent Saturday morning trying to figure out what to work on, now that Parakeet has reached the point it has. Sure, there are still some features I want to add. Shared Budgets would be cool, along with allowing users to drag and drop expenses/budgets to reorder them. They’re things that I’ll be working on, but I know they won’t take me very long to implement. I guess it’s on to finding another project.