http://www.perlmonks.org?node_id=165537

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

Hola Monks,
I've been pulling my hair out for quite a while(more time than I want to admit) trying to figure out why this:
use strict; $a++; print $a;
actually prints "1", when it *thought* it should give a compile-time error for not declaring $a. I felt as though I had witnessed the impossibe. For a while there I was questioning my entire existence, and my world was about to collapse since everything I've ever known to be real and true suddenly was not.

I still don't know what the problem is, but my fears were alleviated when I expanded my minimalist test case to this:
use strict; $aaa++; print $aaa;
...and this works like it should, dying a horrible, miserable death.

So whats the deal? Anyone know why single character variable names slip through the cracks under the use strict pragma?? Or am I not seeing something?

Thanks!

John

Replies are listed 'Best First'.
Re: use strict, strange behavior with single character variables
by perlplexer (Hermit) on May 09, 2002 at 22:13 UTC
Re: use strict, strange behavior with single character variables
by grep (Monsignor) on May 09, 2002 at 22:14 UTC
    $a and $b are special. They belong to sort (well really they belong to the main package) so they are predeclared.

    grep
    Unix - where you can throw the manual on the keyboard and get a command
Re: use strict, strange behavior with single character variables
by Zaxo (Archbishop) on May 09, 2002 at 22:16 UTC

    See perlvar. $a and $b are package globals sacred to sort. They are exempt from strict, but don't use them. They will cause strange behavior if sort is called.

    After Compline,
    Zaxo

Re: use strict, strange behavior with single character variables
by Rex(Wrecks) (Curate) on May 09, 2002 at 22:19 UTC
    $a is a special var used by sort.

    From the Camel:
    "$a PKGThis var is used by the sort function to hold the first of each pair of values to be compared ($b is used to hold the second). (blah...blah...blah) Because it is a global, it is exempt from use strict ; complaints."

    It is a special var and should not really be used outside the sort function.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: use strict, strange behavior with single character variables
by Anonymous Monk on May 09, 2002 at 22:23 UTC
    Oh wow! Cool. I see the light now. Thanks everyone.
    John
Re: use strict, strange behavior with single character variables
by particle (Vicar) on May 09, 2002 at 22:28 UTC
    uh...ditto to everyone above. ;)

    but seriously... this thread will now be the only place i point people with this question. i've added it to my personal nodelet, where it shall remain in perpetuity, at the ready.

    ~Particle *accelerates*