I almost got the ideas with all your replies, thanks.
On 7/20/06, D. Bolliger <[EMAIL PROTECTED]> wrote:
Hi Ken
Ken Perl am Mittwoch, 19. Juli 2006 12:04:
> ok, let me explain what I mean.
Better done by inline/bottom posting, if you already got a bottom answer :-)
> $account = new Account;
> then I can get the currency of the two countries,
> $account->currency_us;
> $account->currency_fr;
>
> after I freeze the code, a new country jumps out, suppose i need
> support Iraq, but now I can't use this,
> $account->currency_iraq;
> If I want to support this method, I have to modify the constructor
> again to add the new country, but what I am looking for is the
> solution that doesn't need to modify the constructor again.
If I don't misunderstand you, you are looking for a lookup class to find out
the currency of countries, and the (country, currency) pairs are defined in a
database.
You have two "variables" here:
1) the number of (country, currency) pairs
2) the information needed to access the underlying database table
sub new {
my $class=shift;
my $dbh=shift; # of DBI class
my ($tablename, @fieldnames)[EMAIL PROTECTED] # if this flexibility needed
my %pairs;
# initialize %pairs (country, currency) from database
return bless \%pairs, $class;
}
sub currency {
my ($self, $country)[EMAIL PROTECTED];
return exists $self->{$country}
? $self->{$country}
: undef;
}
The usage of this class is not easy because of the many arguments to new().
But it's enough flexible as to not need changes.
For easier usage, you could derive a class that encapsulates most or all
arguments to new() of the base class (by hardcoding it, by using a
configfile).
But I'm a bit in doubt as to whether there is not a simpler solution...
Maybe this gives you an idea?
And sorry for my bad english :-)
Dani
[history]
> On 7/19/06, Rob Dixon <[EMAIL PROTECTED]> wrote:
> > Ken Perl wrote:
> > > I find a difficulty when writing the constructor which may has dynamic
> > > and non fixed number of properties, say we want to construct a new
> > > class Account,
> > >
> > > package Account;
> > >
> > > sub new {
> > > my $class = shift;
> > > my $self = { currency_us=>undef,
> > > currency_fr =>undef,
> > > };
> > > bless $self, $class;
> > > return $self;
> > > }
> > >
> > > If we want to create new object with more other contries' currency and
> > > don't want to modify the code, for example, get more other contries'
> > > name from database, how to write my constructor method and it can live
> > > with the dynamic database?
> >
> > I'm unclear what you want. I don't see how you an do anything without
> > modifying the code. Do you mean you want to subclass Account?
> >
> > Suppose you have a list of currencies in @currencies (I can't explain how
> > to get it there as I don't know where it's coming from) you can add these
> > to your object with:
> >
> > my @currencies = qw/ currency_uk currency_gr currency_sp /;
> >
> > @[EMAIL PROTECTED] = ();
> >
> > Does this help at all?
> >
> > Rob
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
> --
> perl -e 'print unpack(u,"62V5N\"FME;G\!E<FQ`9VUA:6PN8V]M\"[EMAIL PROTECTED]
> ")'
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
--
perl -e 'print unpack(u,"62V5N\"FME;G\!E<FQ`9VUA:6PN8V]M\"[EMAIL PROTECTED]
")'
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>