#!/usr/bin/perl -w use strict; package RecipeDb; sub new { my ($class, %args) = @_; my $self = { dbname => $args{dbname}, dbuser => $args{dbuser}, dbpasswd => $args{dbpasswd}, # more options if needed ... #workspace dbh => undef, }; bless $self, $class; $self->{dbh} = DBI->connect("DBI:mysql:$self->{dbname}","$self->{dbuser}","$self->{dbpasswd}", { PrintError => 1, RaiseError => 0, }) || die "Can't connect to $self->{datasource}: $DBI::errstr"; return $self; } sub DESTROY { $self->{dbh}->disconnect(); } # your code from saute.pl, slightly modified sub get_recipe_info { my ($self, $recipeID)=@_; # notice $self my %recipe; #get main recipe parts my $sth = $self{dbh}->prepare("SELECT name,descr,instruct, preptime,notes,source FROM recipes WHERE PriKey=$recipeID"); # notice $self{dbh} $sth->execute(); while (my @row = $sth->fetchrow_array) { $recipe{"recipeID"}=$recipeID; $recipe{"name"}=$row[0]; $recipe{"descr"}=$row[1]; $recipe{"instruct"}=$row[2]; $recipe{"preptime"}=$row[3]; $recipe{"notes"}=$row[4]; $recipe{"source"}=$row[5]; } $sth->finish(); #get ingredients my @ingredients_list; $sth = $self{dbh}->prepare("SELECT recipeIngredients.quantity,units.name, ingredients.name FROM recipeIngredients,ingredients,units WHERE (recipeIngredients.recipe=$recipeID) AND (ingredients.PriKey=recipeIngredients.ingredient) AND (units.PriKey=recipeIngredients.units)"); # notice $self{dbh} $sth->execute(); while (my @row = $sth->fetchrow_array) { push (@ingredients_list, [$row[0],$row[1],$row[2]]); } $sth->finish(); $recipe{"ingredients"}=\@ingredients_list; #get categories my @categories_list; $sth = $self{dbh}->prepare("SELECT categories.name FROM recipeCategories,categories WHERE (recipeCategories.recipe=$recipeID) AND (categories.PriKey=recipeCategories.category)"); # notice $self{dbh} $sth->execute(); while (my @row = $sth->fetchrow_array) { push (@categories_list, $row[0]); } $sth->finish(); $recipe{"categories"}=\@categories_list; return \%recipe; } # more methods here 1; # true enough