For example, the following code continually allocates memory blocks at the same two locations. This is not related to my since we're forcing my to continually reallocate SVs by keeping the refcount greater than 1. There is no reuse of allocated memory at all.What refcount is kept greater than 1? After the print, the result of f() is no longer reference to. Hence the ref to $x goes out of scope, and since nothing is refering to $x, that one goes out of scope as well. Here's a slight variation that shows this:perl -le"sub f{ \my $x } print f() for 1..10;"
OTOH, if you do keep the refcount above 0, no address is reused:sub DESTROY {say "DESTROYED"} sub f{ bless \my $x } say f() for 1..10; __END__ main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED main=SCALAR(0x9721530) DESTROYED main=SCALAR(0x9704c30) DESTROYED
my @a; sub f{ push @a, \my $x; $a[-1] } say f() for 1..10; __END__ SCALAR(0x9c34108) SCALAR(0x9c33c1c) SCALAR(0x9c41f8c) SCALAR(0x9c33f50) SCALAR(0x9c341a8) SCALAR(0x9c34180) SCALAR(0x9c33f00) SCALAR(0x9c50534) SCALAR(0x9c5050c) SCALAR(0x9c504e4)
In reply to Re^5: Reference of constants and literals
by JavaFan
in thread Reference of constants and literals
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |