On Wed, Sep 03, 2008 at 10:56:00PM +0300, Shmuel Fomberg wrote: > Hi All. > > I have this module on CPAN, Data::ParseBinary. > now I want to add a library of pre-prepared format. For example, a > parser/builder for BMP files. > The question is, how do I call it? > some options that I though about: > have module named Data::ParseBinary::lib::GraphicsBMP that export > function named GetGraphicBMP, or a variable $GraphicBMP. > maybe I shoud skip the "lib" level, and mass all the files under the > same directory, creating a Data::ParseBinary::GraphicsBMP module. > another possibility is to make a Data::ParseBinary::lib module, that > export function GraphicBMP(). > And final option is to make the main module to export a function, say > ParseLibrary, and let users do: > $parser = ParseLibrary("Graphic-BMP"); > or something like that. or maybe: > $parser = ParseLibrary->Graphic->BMP; > > What do you say? What would you have done? > Shmuel. > I would use an abstraction pattern and do something like:
package Data::ParseBinary; sub new { my($class, $package, $init) = @_; my($concrete_package) = "Data::ParseLibrary::${package}"; eval qq{ use $concrete_package; }; die "Couldn't use $concrete_package, $@" if $@; my($self) = $concrete_package->new($init); return $self; } package Data::ParseBinary::Graphic::BMP; sub new { my($class, $self) = @_; $self = {} unless $self; bless $self, $class; return $self; } # GetGraphicBMP is redundant, because it is in the GraphicsBMP lib. sub get { my($self, $file) = @_; .... return $bmp; } package main; # The main part of the program, right? $parser = ParseLibrary->new("Graphic::BMP"); $bmp = $parser->get($bmp_file); # Get some other kind of file. $jpeg = ParseLibrary->new("Graphic::JPEG")->get($jpeg_file); ... Austin