[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using OO with mod_perl correctly to avoid leaks


I've been poring over the Perl Best Practices book as of late, lovely work, wish
I would have read it when it first came out....

Anyway, I'm curious about a way I'm using variables, here is my example:

package Habitat::Definitions;
use Class::Std::Utils;
{
use strict;
use base qw( Habitat );

my %definitions_of;

#
# Initialize definitions
#
sub new {

my( $class ) = @_;
my $self = bless anon_scalar(), $class;

my $defs = {};

$defs->{server_user} = $ENV{HOME} ? "perl" : "boysie";

# setup external ips
$defs->{servers} = { dynamic => '192.168.0.1', static => '192.168.0.2' };

# default redirect
$defs->{real_habitat} = "http://www.habitatlife.com/habitat/";

$definitions_of{ ident $self } = $defs;
# not returning $self, but $definitions hash reference instead
return $definitions_of{ident $self};
}

sub DESTROY {
my ( $self ) = @_;

delete $definitions_of{ident $self};
}
}
1;

...then later....

my $defs = Habitat::Definitions->new();

...and later still...

my $usr = $defs->{server_user};

$defs->{root_directory} = "/Users/$usr/";


Is this bad usage?

Am I creating the same situation using inside-out classes seek to avoid,
i.e. accessing object internals directly via manipulating $definitions_of{ident Habitat::Definitions}?

Is this kind of structure still prone to leak memory?

What are the ramifications of not returning the $self blessed reference?


Thanks for any help,
Boysenberry Payne




Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.