Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created May 20, 2016 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/f10aa4a6e3d76428a0840ac608b52061 to your computer and use it in GitHub Desktop.
Save xtetsuji/f10aa4a6e3d76428a0840ac608b52061 to your computer and use it in GitHub Desktop.
Perl truly global variable and tacit global variable.
#!/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