[Israel.pm] Garbage Collection Question

Mikhael Goikhman migo at homemail.com
Sat May 8 16:43:55 PDT 2004

On 08 May 2004 20:40:26 +0300, Yuval Yaari wrote:
> Shlomo Yona said:
> >> Will Perl automagically get rid of $file after the last line I used it
> >> in (in case reference count == 0, of course), or would it be better to
> >> set it to undef?
> >
> > Depending on the scope of $file.
> > If $file is still in scope, then Perl's garbage collector will not touch
> > it. That is because there is no count 0 for that chunk of
> > memory.
> >
> > Setting the scalar to under fill, however, be useful in some
> > implementations of Perl, as it will set the reference count
> > to 0.
> I wonder if I can use Devel::Peek to check for the refcount of $file.
> On one hand, $file is not needed anymore, but on the other hand, $file is
> "used" by Devel::Peek.

You think $file is not needed anymore, but there is no way for perl to
predict this. A program may decide to use the current $file value at any
moment until a new assigment to $file or end of scope.

Consider this eval code for example: eval 'print $' . 'file';

And if $file is not "my", but has a symbol table entry, then $main::file
may be used even without eval, from any place in the program.


perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'

More information about the Perl mailing list