in reply to Re^2: my $scope as the default for variables
in thread Please help me print this hash.

:) I was going to list this as a reason, but I remembered warnings

$ perl -Mwarnings -e " $foo = 1; " Name "main::foo" used only once: possible typo at -e line 1. $ perl -Mwarnings -e " my $foo = 1; "

If warnings can detect this I see no reason a New pragma 'scope' to change Perl's default scoping couldn't do the same

Naturally that won't help if you're making the typo twice, but neither does strict/my :)

Replies are listed 'Best First'.
Re^4: my $scope as the default for variables
by Corion (Patriarch) on Jun 19, 2012 at 08:56 UTC

    Yes, but turning a single use of a lexical variable into a warning will raise warnings for code like:

    use Guard; sub foo { $guard = scope_guard { print "done\n" }; ... do stuff, without ever mentioning $guard again ... };

    This could maybe circumvented by only raising the warning when my is missing:

    use Guard; sub foo { # This will warn $guard = scope_guard { print "done\n" }; ... do stuff, without ever mentioning $guard again ... };

    Detecting this will be tricky, but it should catch most of the typos that would get a warning.

      Yes, but turning a single use of a lexical variable into a warning will raise warnings for code like: ... This could maybe circumvented by only raising the warning when my is missing

      Or it isn't a blessed Guard :) Yes, but this new scope pragma could also notice its a Guard singleton and not issue a warning -- sure it won't work for all singletons, but the popular ones could be listed, and the new ones could register themselves like you register warnings or even autodie::hints