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

HI, Pls find the below code
#!/usr/bin/perl use strict "vars"; my $x = 10; my $y = 20; $a = $x * $y; print $a;
This is not throwing the error. Also ,$b = $x *$y accepted. When i use $c = $x * $y and so on,its not throwing the error.Kindly let me know the reason y perl is not throwing error for $a= $x * $y and $b= $x * $y

Replies are listed 'Best First'.
Re: use strict Question?
by moritz (Cardinal) on Dec 07, 2009 at 12:26 UTC

    As SirClive already mentioned, $a and $b are globals (so that sort can work easily), and thus not subject to strict vars.

    This works in practice, but leaves the bitter taste of design smell in your mouth.

    In Perl 6 there is instead a very simple way to declare formal parameters to a block, you can write

    @array.sort: { 2 * $^a <=> $^b }

    Here the twigil ^ declares the variables as formal parameters to the block. So by writing two extra characters in the typical sort block you get rid of this smelly exception.

    Since these parameters are filled in lexicographic order of their names, @array.sort: { $^b <=> $^a } sorts in reverse, just as it would do (with slightly different syntax) in Perl 5.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: use strict Question?
by zentara (Cardinal) on Dec 07, 2009 at 12:40 UTC
    As SirClive already mentioned, $a and $b are globals

    ..... i was just wondering..... since almost every new Perl programmer gets bit by the $a and $b thing.. at least one time..... is Perl 6 going to remedy this?.... like couldn't their at least be a warning when you try to define my $a or my $b ?


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      Please start your own thread :)
        ..... i only contribute to the eternal thread ..... ;-)

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku
Re: use strict Question?
by ww (Archbishop) on Dec 07, 2009 at 12:07 UTC
    Duh. See OP's clarification and SirClive's answer, below.

    Perhaps because there's no error to throw?

    Line 6 and the operations in your narrative assign to $a, $b, $c the result of the simple multiplication; did you expect something different?

      I think the original poster is referring to the fact that they didn't need my $a or my $b but they did need my $c my $d , etc etc The reason for this is that $a and $b are global variables which are used by the sort function. See perldoc -f sort for more --- start quote ---

      If the subroutine's prototype is "($$)", the elements to be compared are passed by reference in @_, as for a normal subroutine. This is slower than unprototyped subroutines, where the elements to be compared are passed into the subroutine as the package global variables $a and $b (see example below). Note that in the latter case, it is usually counter-productive to declare $a and $b as lexicals.

      -- end quote ---
        Thanks SirClive!
      $c = $x * $y;throws error. Global symbol "$c" requires explicit package name But ,when i use $a = $x * $y perl doesn't throwing the error. 1.I used "use strict" in the program. 1. I declared two variables $x and $y as my. 2. But i didnt declared $a or $b as my..I expected Global symbol "$a" requires explicit package name for $a and $b.But it didnt. 3. When i use $c ,it throw error. I am curious to know why while $a and $b perl not throwing the error!
        $a and $b are special package variables used by sort(), so they don't have to be declared even if you are using strict. $c isn't a special variable; hence it must be declared, and that's why it throws an error. See perlvar for more info.