Skip to content

Instantly share code, notes, and snippets.

@stevej
Created September 15, 2011 00:44
Show Gist options
  • Save stevej/1218243 to your computer and use it in GitHub Desktop.
Save stevej/1218243 to your computer and use it in GitHub Desktop.
Just a reminder that Perl had both dynamic and static scoping
$x = 0;
sub f { return $x; }
sub g { my $x = 1; return f(); }
print g()."\n";
# the power of scoping compels you, change global $y!
$y = 0;
sub f1 { return $y; }
sub g1 { local $y = 1; return f1(); }
print g1()."\n";
# $ perl ./scoping.pl
# 0
# 1
@afeinberg
Copy link

You can even do this:


sub a {
  our $foo = "foo";
}

sub f {
  our $foo;
  print $foo . "\n";
}

a();
f();
# prints "foo"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment