in reply to Storing object references internally
Think about what you're doing - if this makes sense, no problem. I have code like this:
This allows for objects that are expensive to create (e.g., reading data from database or XML data store, etc.) to be created only once, and retrieved each time thereafter. In my scenario, the data store cannot change between the time I start my program and the end, so this is safe. If your data could change, and that could be important, then you may want to avoid caching the objects so that it gets recreated each time, which means reloading the data from the data store.package Foo; use strict; use warnings; #... my %cache; sub fetch { my $class = shift; my $instance = shift; return $cache{$instance} if $cache{$instance}; my $self = {}; bless ($self, $class); $cache{$instance} = $self; $self->{name} = $instance; # more initialisation. $self; }
|
|---|