in reply to Re^5: modular file scoping
in thread modular file scoping

Personally, I think it's easiest to think about it in terms of references

In fact, in the following you can see how Perl uses the same reference counting mechanism as for hard references. Hopefully this will now put your mind at ease :-)

#!/usr/bin/env perl use warnings; use strict; # Debugging aids use Devel::Refcount 'refcount'; use Sub::Delete 'delete_sub'; { # This will act like a normal hash except it will print # "DESTROY" when it is destroyed (garbage collected). package DebugHash; BEGIN { require Tie::Hash; our @ISA = ('Tie::StdHash'); } sub DESTROY { print "DESTROY\n" } } { # Lexical scope for "my %hash". my %hash; # +1 reference to %hash (0+1=1) BEGIN { tie %hash, 'DebugHash' } # tie to debugging aid # Note: We'll use $hash{c} as a counter for debug output. # Apparently BEGIN{} blocks cause +1 reference to %hash (?) # Note: refcount()-1 since the argument \%hash is +1 reference. BEGIN { print ++$hash{c},": ",refcount(\%hash)-1,"\n" } # "1: 2" sub foo { # +1 reference to %hash (compile time) $hash{x}++; print ++$hash{c},": ",refcount(\%hash)-1,"\n"; } BEGIN { print ++$hash{c},": ",refcount(\%hash)-1,"\n" } # "2: 3" sub bar { # +1 reference to %hash (compile time) $hash{y}++; my $hashref = \%hash; # +1 reference to %hash (runtime) print ++$hash{c},": ",refcount(\%hash)-1,"\n"; } BEGIN { print ++$hash{c},": ",refcount(\%hash)-1,"\n" } # "3: 4" # Remember "BEGIN" is compile time, the following is run time. # The three references to %hash are the lexical "my %hash", # as well as "sub foo" and "sub bar". print ++$hash{c},": ",refcount(\%hash)-1,"\n"; # "4: 3" } # Lexical scope of "my %hash" ends, -1 reference to %hash. # The following calls are in "eval" b/c otherwise the calls # would be another reference keeping &main::foo/bar alive. eval "foo()"; # "5: 2" eval "bar()"; # "6: 3" delete_sub 'bar'; # -1 reference to %hash eval "foo()"; # "7: 1" delete_sub 'foo'; # -1 reference to %hash (1-1=0) => "DESTROY" print "Done.\n";

Replies are listed 'Best First'.
Re^7: modular file scoping
by Pstack (Scribe) on Oct 15, 2017 at 21:07 UTC

    I get it!

    Perl uses a simple counting-stack mechanism for scope retention. Which explains, if mechanically inclined, your preference for "..so long as there is something that refers to the variable, Perl keeps it around..".

    My own memory faculty is far too degenerated to keep such a count and so opt instead to identify the "seed" (count-1) whose lifespan dictates over its dependents (ie FORCES accessibility/life). It wouldn't be the first time this largely 'hands off' approach has landed me in trouble and, if it is not too late, already have an immensely richer appreciation of the wider underlying schema.