in reply to what happens when lexical variables go out of scope?
For code like this:
$ftp goes out of scope at #1, but there's still a reference being held (in $connection->{ftp}!), so the Net::FTP only gets destroyed at #2.{ my $connection; { my $ftp = Net::FTP->new('some.host'); $connection->{ftp} = $ftp; } #1 # Do stuff with $connection->{ftp}... } #2
Think of a "reference" as an arrow pointing to a thingy. It goes from an lvalue (something that's assignable, like a scalar variable or a slot in an array or in a hash) to a thingy. The thingy knows how many arrows point at it; it goes away when the last arrow goes away.
|
|---|