Created
May 20, 2016 01:24
-
-
Save xtetsuji/f10aa4a6e3d76428a0840ac608b52061 to your computer and use it in GitHub Desktop.
Perl truly global variable and tacit global variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# $_ is truly global variable | |
# $a is especially global varialble (for sort and some function). is not trully? | |
use strict; | |
use warnings; | |
use v5.10; | |
# initialize strictly | |
undef $_; | |
undef $a; | |
undef $main::_; | |
undef $main::a; | |
undef $Foo::_; | |
undef $Foo::a; | |
package Foo { | |
sub set_a { | |
$a = shift; | |
} | |
sub set_underline { | |
$_ = shift; | |
} | |
} | |
say "a is strictly undefined." if !grep { !defined } ($a, $main::a, $Foo::a); | |
say "_ is strictly undefined." if !grep { !defined } ($_, $main::_, $Foo::_); | |
say "set \$a at Foo"; | |
Foo::set_a("foo"); | |
say "a, main::a, Foo::a = " . ( join ",", map { $_ // "(undef)" } $a, $main::a, $Foo::a ) . ""; | |
say "set \$_ at Foo"; | |
Foo::set_underline("foo"); | |
say "_, main::_, Foo::_ = " . ( join ",", map { $_ // "(undef)" } $_, $main::_, $Foo::_ ) . ""; | |
__END__ | |
output: | |
$ perl set_special_globals.pl | |
set $a at Foo | |
a, main::a, Foo::a = (undef),(undef),foo | |
set $_ at Foo | |
_, main::_, Foo::_ = foo,foo,(undef) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment