On Wed, 12 Jan 2005 12:24:00 +0200, Octavian Rasnita <[EMAIL PROTECTED]> wrote:

> The program:
> 
> use lib ".";
> use strict;
> use Parse;
> 
> my $f = Parse->new;
> 
> opendir(DIR, ".");
> my @files = readdir DIR;
> closedir DIR;
> 
> foreach my $file(@files) {
> next unless $file =~ /\.xls$/i;
> 
> $f->parse($file);
> }


> sub DESTROY {
> my $self = shift;
> undef $self;
> 
> #I don't know if this sub helps, or if it is correctly written
> }
> 

Teddy,

There are a couple of things going on here.  The last one is the
destructor.  Perl should Garbage collect automatically.  The only time
you need to give it a push, is if you end up creating references to
references.  the Garabge collector does actually get trid of things,
it just lowers their refcount by one.  Now that youwe've seen you
code, this probably isn't your problem, and messing with $self is
dangerous if you don't need to do it.

You're real issue here is scoping.  Once you call new, that object
stays in scope until it passes out of scope.  You call new at the
beginning of the program, and then go into a forecah loop, and keep
adding refferences to Workbook->Parse() objects, and then garbage
collection isn't done until the end.  This is what allows you to only
open the database connection once, but it causes things to pile up in
memory.

If you don't mind incurring the extra overhead on the database server,  move

$f = Parse->new ;

Inside the foreach loop.  This will ensure that that the Parse object
is destroyed at the end of each iteration:

foreach my $file(@files) {
     next unless $file =~ /\.xls$/i;
     $f = Parse->new ; 
     $f->parse($file);
}

If you want to keep the database connection open,  you need to make
sure that the reference counts of $f and $book fall to zero each time
sub Parse exits.  Because they're references, they don't go out of
scope when the subroutine exits, so they and the  Workbook->Parse
objects keep piling up.  when you're debugging, you can use
Devel::Peek to keep track of the references and make sure the counts
go down.  Also take a look at Chapter 11 and particularly recipe 11.5
in the _Perl Cookbook_.

HTH,

--jay

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to