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

Also, a major part of needing to predeclare your variables is to avoid typos. Having default-lexical scope avoids a lot of the nasty action-at-a-distance that default-global scope produces, but it still can't detect the typo between $outfile and $out_file for you, which I consider the major point of using strict.

Replies are listed 'Best First'.
Re^3: my $scope as the default for variables
by Anonymous Monk on Jun 19, 2012 at 08:36 UTC

    :) 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 :)

      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