Perl: Life as a one-liner
December 29th, 2006 LukeI 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.
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.
I’ve recently been doing a lot of work with databases, and been more than a mite jealous of PHP’s
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
#!/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.
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:
I figured out that there was a really cool way to do this, using a
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
Recently, I had to retrieve a large number of attributes from a MySQL table. When doing this, I use the
$sth = $dbh->prepare("SELECT id, name FROM users");
my %user;
$sth->execute();
$sth->bind_columns(\@user{qw(id name)});
while($sth->fetch) {
...
}
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.
You are currently browsing the girasquid.com weblog archives for December, 2006.