As a side note, you can play around with Devel::Peek to see how many
things are referencing a variable.
# perl -MDevel::Peek -e
'$foo="foo";$bar=\$foo;$zog=\$foo;Dump($foo);$bar="bar";Dump($foo);print
$foo;';
SV = PV(0x80f2424) at 0x810b3cc
REFCNT = 3
FLAGS = (POK,pPOK)
PV = 0x80f10a0 "foo"\0
CUR = 3
LEN = 4
SV = PV(0x80f2424) at 0x810b3cc
REFCNT = 2
FLAGS = (POK,pPOK)
PV = 0x80f10a0 "foo"\0
CUR = 3
LEN = 4
foo
There, you can see $foo has a REFCNT (ref count) of 3 ($foo, $bar and
$zog), then a REFCNT of 2 after the value of $bar is re-assigned. Of
course, the value in $foo is still 'foo';
# perl -MDevel::Peek -e
'$foo="foo";$bar=\$foo;$zog=\$foo;Dump($foo);$$bar="bar";Dump($foo);print
$foo;'; >
SV = PV(0x80f2424) at 0x810b3cc
REFCNT = 3
FLAGS = (POK,pPOK)
PV = 0x80f10a0 "foo"\0
CUR = 3
LEN = 4
SV = PV(0x80f2424) at 0x810b3cc
REFCNT = 3
FLAGS = (POK,pPOK)
PV = 0x80f10a0 "bar"\0
CUR = 3
LEN = 4
bar
Here, the REFCNT, as expected, stays the same. And the value in $foo is
changed.
If you are within code and want to make sure
you don't undef things which have refs to it, you can use:
# perl -MDevel::Peek=SvREFCNT -e \
'$foo="foo";$bar=\$foo;$zog=\$foo;print SvREFCNT($foo);';
3
Which will tell you how many refs there are. Of course, if it is 1,
then nothing else should be referencing $foo aside $foo.
This doesn't answer the question in any way, but thought some may be
interested in this side-note.
Cheers,
Kevin
On Thu, Aug 15, 2002 at 01:52:23PM -0400, Bob Showalter
([EMAIL PROTECTED]) said something similar to:
> > -Original Message-
> > From: Nikola Janceski [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, August 15, 2002 12:44 PM
> > To: Beginners (E-mail)
> > Subject: undef of nested data structures
> >
> >
> > I am wondering how undef works.
>
> perldoc -f undef
>
> >
> > I know that undef will undefine a variable if used like
> > undef($scalar);
>
> Yes.
>
> > I also know that it doesn't actually free up the memory but
> > tells Perl that
> > it's now available to be recycled for other data.
>
> Well, you probably should't worry about memory allocation details. Perl
> takes care of all those details behind the scenes.
>
> >
> > but what about nested data (ie. hashes of hashes, arrays of
> > arrays, etc.).
> > if $ref is a reference to a nested structure (let's say a
> > hash of hashes).
> > will -
> > undef $ref;
> > - undefine the enitre nested data structure if no other
> > variables contain
> > references to any part of it? (assuming no cyclical references)
>
> undef($ref) undefines the scalar $ref. Now, if whatever $ref was pointing to
> had no other references to it, then yes, that data would be freed. But this
> is a side effect of the undef(), not something special about undef() itself.
> The same thing would happen if you assigned "foo" to $ref, so that $ref no
> longer contained a reference to some data. Data is freed when the last
> reference to it is removed.
>
> You can think of undef like this:
>
>undef($scalar); is the same as: $scalar = undef;
>undef(@array); is the same as: @array = ();
>undef(%hash); is the same as: %hash = ();
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
[Writing CGI Applications with Perl - http://perlcgi-book.com]
Children are naive -- they trust everyone. School is bad enough, but, if you
put a child anywhere in the vicinity of a church, you're asking for trouble.
-- Frank Zappa
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]