in reply to initializing objects from values from a db

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?
Save the recurring data in an (anonymous) variable and store a reference to that variable in your object. But don't forget to dereference the reference if you need to access the data!

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

  • Comment on Re: initializing objects from values from a db

Replies are listed 'Best First'.
Re^2: initializing objects from values from a db
by punkish (Priest) on Dec 05, 2009 at 20:29 UTC
    Q1. What is an "anonymous" variable?

    Q2. So, I would query the db and create this anonymous variable in my script outside Obj.pm module, before the object is created, then pass a reference to it when creating the object? Then, I would keep track of when a new object would require a new version of this information (after 5000 objects with the current info in "cache" have been created and used), and then refresh this anonymous variable from new info from the db? Is that what you are suggesting?

    Update: Following up on my Q2 above, I can only think of how to do this as stated above. However, I would prefer to tuck everything inside my Obj.pm, so that the anonymous variable is created inside the class, and perhaps a class method keeps track of how it is used and allocated. Wouldn't that be cleaner and more obj-oriented, so to say?

    --

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