- local
Saved variable: local
Slurp mode used local $/ = undef;
examples/advanced-perl/local.pl
#!/usr/bin/perl use strict; use warnings; our $global = 100; our $my = 200; our $local = 300; print_variables(); # $global 100 # $my 200 # $local 300 set(); # $global 1 # $my 2 # $local 3 # inside print_variables(): # $global 1 # $my 200 # $local 3 print_variables(); # $global 1 # $my 200 # $local 300 sub set { $global = 1; my $my = 2; local $local = 3; print "$global\n"; print "$my\n"; print "$local\n"; print "---\n"; print_variables(); } sub print_variables { print "$global\n"; print "$my\n"; print "$local\n"; print "---\n"; }
100 200 300 --- 1 2 3 --- 1 200 3 --- 1 200 300 ---
examples/advanced-perl/dumper_local.pl
use strict; use warnings; use Data::Dumper; my %data = ( foo => 1, bar => 1, moo => 1, ); _dump(); _local_sorted(); _dump(); _sorted(); _dump(); sub _local_sorted { local $Data::Dumper::Sortkeys = 1; _dump(); } sub _sorted { $Data::Dumper::Sortkeys = 1; _dump(); } sub _dump { print Dumper \%data; }
$VAR1 = { 'bar' => 1, 'moo' => 1, 'foo' => 1 }; $VAR1 = { 'bar' => 1, 'foo' => 1, 'moo' => 1 }; $VAR1 = { 'bar' => 1, 'moo' => 1, 'foo' => 1 }; $VAR1 = { 'bar' => 1, 'foo' => 1, 'moo' => 1 }; $VAR1 = { 'bar' => 1, 'foo' => 1, 'moo' => 1 };