Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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!!

In reply to OOP: How to construct multiple instances of a class at once by DrGTO

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (6)
    As of 2018-06-18 04:04 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (107 votes). Check out past polls.