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!!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.