perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From william <yin...@gmail.com>
Subject Re: Initialize object permanently
Date Tue, 20 May 2008 11:13:22 GMT
On 5/20/08, Dodger <el.dodgero@gmail.com> wrote:
> 2008/5/19 Michael Peters <mpeters@plusthree.com>:
>
> > william wrote:
>  >
>  >> Then I would need to modify the QueryData module then,
>  >
>  > No don't do that.
>  >
>  >> by modifying
>  >> the standard module would it make my future maintenance more
>  >> complicated ?
>  >
>  > Absolutely.
>  >
>  >> Do you have any tips for me ?
>  >
>  > Wrap the object in your own package. Let's call it My::QueryData.
>  >
>  > package My::QueryData;
>  > use QueryData;
>  >
>  > my $query_data;
>  > sub create {
>  >  $query_data = QueryData->new(...);
>  > }
>  >
>  > sub get {
>  >  return $query_data;
>  > }
>  >
>
>
> For extra syntactic sugar, you could always just do it singlet style.
>
>  package My::QueryData;
>  use base QueryData;
>  our $singlet;
>
>  sub new {
>     return $singlet if $singlet;
>     return $singlet = QueryData->new(@_);
>  }
>
>
>  Of course, if you want to allow different ones for different
>  invocations (i.e. Pkg->new(foo => 1) and Pkg->new(foo => 2), you can
>  make $singlet a hashref keyed by those options, instead, and check for
>  the appropriate one
>

Thanks, those solutions are nice.

Mime
View raw message