DrGTO has asked for the wisdom of the Perl Monks concerning the following question:
this will produce one MySQL query per object initialisation. So many queries for a longer user list ... I am wondering, if there is a way to use the class constructur with a list of ids, running only one query in the new() constructur and then return several user objects at once? Something like:my @users; foreach my $id (@userIds) { push (@users,User->new($id)); }
Where array @users() contain several user objects afterwards. My class constructor looks likemy @users=User->new($id1,$id2,$id3,$id4);
All vague solutions I can think of, mean a huge reconstruction of code. E.g. different constructors for single / multiple instances nested within the class.... Any help, comments are appreciated. Thanks!!sub new { my $proto = shift(@_); my $userId=shift(@_); my $class = ref($proto) || $proto; my $self = { "user_id"=> undef, "user_name" => undef }; bless ($self, $class); my $href = GETSQL("SELECT user_name FROM users WHERE user_id = $us +erId"); @{$self}{keys %{$href}} = values %{$href}; return $self;
|
|---|