Update: My mental block is cleared, and I have figured out most of my questions below, except for the part about initializing 'bar'
Apologies in advance if this turns out to be too basic a question, but it has got me all stymied.

This is how I want my design to be -- the object constructor gets a couple of input values, and creates a fully populated object. There are three things to consider:

  1. millions of objects are created, albeit not concurrently, during a program run
  2. some aspects of initialization of all the objects, all millions of them, are controlled by values in a set of init config settings
  3. a group of params in the objects are common to groups of objects, and since this group is a lot of values, they should be queried only once
Example pseudo code:
# In my long running program my $dbname = get_db_name_from_some_config_file(); my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname","",""); my $user_id = 1; my $cfg = get_user_specific_config_values_stored_in_db($user_id); for my $obj_id (0 .. 2 million) { my $obj = new Obj(dbh => $dbh, cfg => $cfg, obj_id => $obj_id); $obj->very_long_complicated_program(); } # In a nearby module Obj.pm sub new { my ($class, %args) = @_; my $dbh = $args{dbh} my $cfg = $args{cfg}; my $obj_id = $args{obj_id}; my $in = { dbh => $dbh, cfg => $cfg, foo => {}, bar => {}, }; my $self = bless $in, $class; # foo is a result of a different query for every obj_id $in->{foo} = $self->foo($obj_id); # The following part is the only part I am still confused about. # Should I use something like Memoize here? # # bar changes only once per, say, every 5000 objects, # and the resulting query from $self->bar is a very # large set (thousands of rows), so it makes sense to # query the db only when bar changes. So, for obj_id # 0 through 4999, bar remains the same, then for obj_id # 5000 through 9999 a new bar is returned, and so on $in->{bar} = $self->bar($obj_id); return $self; } sub dbh { my $class = shift; return $self->{dbh}; } sub cfg { my $class = shift; return $self->{cfg}; } sub foo { my ($class, $obj_id) = shift; my $cfg = $self->cfg; my $dbh = $self->dbh; my $foo = query_dbh_for_foo_execute($obj_id); $foo = change_parts_of_foo_based_on($cfg); return $foo; } sub bar { my ($class, $obj_id) = shift; my $dbh = $self->dbh; my $bar = query_dbh_for_bar_execute($obj_id); $bar = change_parts_of_bar_based_on($cfg); return $bar; }

How do I go about implementing the above?

Update2: Another, possibly better way to ask my question -- how do I create an object a part of which (its data) is shared with other objects without requiring making a copy of that data?

--

when small people start casting long shadows, it is time to go to bed

In reply to initializing objects from values from a db by punkish

Title:
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, 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:
    & &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.