Hi there monks,
this is my first post, so please forgive any foolish behaviour.
I am not a perl newbie but I am trying out some OOP stylish programming.
I have a user class, which is loaded with values from a database.
So, whenever I create a new user object I have one corresponding MySQL query for the user data.
Now, I have the situation, that I would like to get all users in the system.
Using a loop
my @users;
foreach my $id (@userIds) {
push (@users,User->new($id));
}
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=User->new($id1,$id2,$id3,$id4);
Where array @users() contain several user objects afterwards.
My class constructor looks like
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;
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!!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.