dasnippa has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I am curious about why strict does not complain about the following:
#! /usr/local/bin/perl -w use strict; sub functioncall { $b = "foo"; print "$b\n"; } functioncall();
In case its a version issue... I'm running
v5.6.0 built for IA64.ARCHREV_0 (on HP-UX)

Replies are listed 'Best First'.
Re: strict's behavior within subroutines?
by toolic (Bishop) on Jun 15, 2011 at 18:44 UTC
    From the strict doc:
    Because of their special use by sort(), the variables $a and $b are exempted from this check.
    This applies inside or outside of subroutines.

    perlcritic can detect $b without my, however.

Re: strict's behavior within subroutines?
by MidLifeXis (Monsignor) on Jun 15, 2011 at 18:37 UTC

    It is highly suggested not to use $b or $a as normal variables, since they are special-cased for use in sort. Since you might use functioncall() as a special sort function, strict/warnings won't complain about them.

    Well, more or less.

    --MidLifeXis

Re: strict's behavior within subroutines?
by davido (Cardinal) on Jun 15, 2011 at 20:33 UTC

    For the record, strict doesn't complain about the other special variables either. $b is one of many. It just happens to have a name that is a little less effective in observing the Principle of Least Astonishment.

    If the sort variables were designed with that principle in mind, they would have more unusual names, since they're special or unusual themselves, lest they give the impression of not being special cases of scalar variables. Of course that would require breaking existing usages.


    Dave