in reply to Re^4: Multiple uses of (?{ code }) do not appear to be called
in thread Multiple uses of (?{ code }) do not appear to be called

local our @o;
is the same as
our @o;
local @o;

our @o; disables use strict for @o.
local @o; protects the existing value of @o (and initialized @o to an empty array).

Without the local, there's no scoping. That's bad! If the caller also uses @o, you just clobbered the caller's variable.

Replies are listed 'Best First'.
Re^6: Multiple uses of (?{ code }) do not appear to be called
by rhesa (Vicar) on Dec 29, 2006 at 19:58 UTC
    Right, gotcha. I was confused, since the perldocs say: "An "our" declares the listed variables to be valid globals within the enclosing block". It didn't occur to me at first that an inner scope would simply reuse an "our" in the enclosing scope. I now also see what diotalevi meant with stomping on my parent :)

      dio wasn't refering to enclosing scopes. Compare

      sub test { my ($x) = shift; return if !$x; our @o = $x; test($x-1); print("@o\n"); } test(3); # 1,1,1

      with

      sub test { my ($x) = shift; return if !$x; local our @o = $x; test($x-1); print("@o\n"); } test(3); # 1,2,3

      The same problem occurs without recursion too. Compare

      sub foo { my ($x) = shift; our @o = 1; bar(); print("@o\n"); } sub bar { my ($x) = shift; our @o = 2; print("@o\n"); } foo(); # 2,2

      with

      sub foo { my ($x) = shift; local our @o = 1; bar(); print("@o\n"); } sub bar { my ($x) = shift; local our @o = 2; print("@o\n"); } foo(); # 2,1