in reply to Get the $main value when use local

local and my are just completely different things. local $something in the main package creates a temporary replacement for the global $main::something. my $something creates a lexical variable with the same name as $something - but $main::something always refers to the global, since lexicals are independent of package.

I would recommend you just use lexicals. The cases where you want to use local() are very limited (generally, local is used for temporarily overriding special globals like $/, $_ etc).

If you really need to use globals, just do this:

$a='main'; if (1) { my $olda = $a local $a = 'sub'; print "$a\n"; print "$olda\n"; } print "$a\n";