use strict; use warnings; use 5.010; my $val = 10; sub f { say "in global f, \$val is: $val"; } f(); #Demonstrates that the global definition of f is overwritten #by the local definition of f (below) at compile time. As a #result, you won't see output from global f. When the local f #executes instead, the global $val above is hidden by a local #$val in the definition of f (below). say '=' x 20; sub g { my $val = shift; say "in g, \$val is: $val"; #This definition of f closes over $x in previous line. sub f { #line 40 say "in local f, \$val is: $val"; } f(); } g('hello'); say '=' x 20; g('goodbye'); say '=' x 20; --output:-- Variable "$val" will not stay shared at 4perl.pl line 41. Subroutine f redefined at 4perl.pl line 40. Use of uninitialized value $val in concatenation (.) or string at 4perl.pl line 41. in local f, $val is: ==================== in g, $val is: hello in local f, $val is: hello ==================== in g, $val is: goodbye in local f, $val is: hello ====================