in reply to Legitimate use for combined 'local' and 'our'

#!/usr/bin/perl our $V = 'non-local'; sub print_v { print "$V\n"; } { local our $V = 'local'; print_v; } print_v __END__ # output: local non-local

Since local affects variables in code you call, you have to call the sub from the scope in which you used <c>local our<c>.

Replies are listed 'Best First'.
Re^2: Legitimate use for combined 'local' and 'our'
by JavaFan (Canon) on Nov 15, 2010 at 11:02 UTC
    Not a good enough example as you can freely remove the our in local our, and it will still produce the same result (even under strict). Only if you place the first part in a different scope, and enable strict, you'll see a difference:
    #!/usr/bin/perl use strict; use warnings; { our $V = 'non-local'; sub print_v { print "$V\n"; } } { local our $V = 'local'; print_v; } print_v __END__
    If you now remove the second our, the program doesn't compile.