LJ Archive

Listing 2. retrieve-birthday-alzabo.pl, an Alzabo implementation of the program in Listing 1.

#!/usr/bin/perl
use warnings;
use strict;
use Alzabo::Runtime::Schema;
# Set up some basic variables
my $schema_name = 'addressbook';
my $username = 'reuven';
my $password = '';
# What name should we look for?
my $look_for_name = $ARGV[0];
die "You didn't enter a name to look for "
    unless $look_for_name;
# Add % to the front and back, for an SQL regexp
$look_for_name = "%" . $look_for_name . "%";
# Load the schema from disk
my $schema =
    Alzabo::Runtime::Schema->load_from_file
    ( name => $schema_name );
$schema->set_user( $username );
$schema->set_password( $password );
$schema->connect;
# Get the table object on which we're going
# to operate
my $people = $schema->table("People");
# Retrieve all of the rows from our table matching
# our query
my $row_cursor = $people->rows_where
    (where => [[$people->column('first_name'),
                'LIKE', $look_for_name],
               'or',
               [$people->column('last_name'),
                'LIKE',
                $look_for_name]]);
my $rows_returned = 0;
# Iterate through each row using a cursor
while (my $row = $row_cursor->next_row)
{
    my $first_name = $row->select('first_name');
    my $last_name = $row->select('last_name');
    my $birthday = $row->select('birthday');
    print "$first_name $last_name
           (birthday: $birthday)\n";
    $rows_returned++;
}
# Indicate if there was a problem
if ($rows_returned == 0)
{
    print "No one in the addressbook matches
           that pattern.\n";
}
LJ Archive