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

I have two programs: program #1
#!/usr/bin/perl use strict; $a = "hi\n"; print $a;
program #2
#!/usr/bin/perl use strict; $i = 5; print "The value is $i.\n";
Why does program #2 give me an error (when I don't use "my"):
Global symbol "$i" requires explicit package name at ./test-strict.pl line 4.
Global symbol "$i" requires explicit package name at ./test-strict.pl line 5.
Execution of ./test-strict.pl aborted due to compilation errors.
yet #1 doesn't? I'm very well aware that using strict requires you to declare your variables with "my". Yet I didn't use the word "my" on #1 and it worked fine.

Replies are listed 'Best First'.
Re: Questions on strict
by davido (Cardinal) on Jan 03, 2007 at 05:49 UTC

    $a and $b are special variables used within sort's code block or named subroutine. Because of their special use, they are approximately exempt from strictures. Because of their special status and use, you are generally discouraged from using them in any other context other than sort code blocks and subs, in any non-trivial code.


    Dave

Re: Questions on strict
by perrin (Chancellor) on Jan 03, 2007 at 05:51 UTC
    Unfortunately, $a and $b are special global variables in perl. They are used in custom sort routines. See the perlfunc man page for more about sort() and how it uses them.
Re: Questions on strict
by perl_lover (Chaplain) on Jan 03, 2007 at 05:47 UTC
    Already discussed in $a and strict. Please do a quick search before posting a question
Re: Questions on strict
by zentara (Cardinal) on Jan 03, 2007 at 14:05 UTC
    wackattack... don't feel bad, almost everyone here has been burned by that one, just remember..... don't use $a or $b as variables, unless you are sorting.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum