package PriceType; use base qw(Class::Singleton); sub id { croak "this should return the price_type_id" }; my $dbh = DBI->connect(...); my $price_type = $dbh->prepare('select price_type_id, price_type_label from price_type'); $price_type->execute; $price_type->bind_columns(my \($id, $label) ); while (my $type = $price_type->fetch) { $label =~ = s/[^a-z]//gs; my $method = join('::', __PACKAGE__, $label, "id"); my $id = $id; { no strict 'refs'; *{$method} = sub { $id } }; }; #### sub get_price { my ($self, $price_type) = @_; croak "need PriceType" unless UNIVERSAL::isa($price_type, 'PriceType'); my $sth = $self->sql_find_price; $sth->execute($self->id, $price_type->id); my ($price) = $sth->fetchrow_array; return $price; } #### my $cost = $product->cost; my $list = $product->list_price; my $sale = $product->sale_price; #### my $cost = $product->price(PriceType::Cost->instance); my $list = $product->price(PriceType::ListPrice->instance); my $sale = $product->price(PriceType::SalePrice->instance); #### my $cost = PriceType::Cost->instance->get_price($product); my $list = PriceType::ListPrice->instance->get_price($product); my $sale = PriceType::SalePrice->instance->get_price($product);