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

Hi, I just noticed an odd behavior in this Tk script, where a variable $b is used without being declared with "my $b", and strict dosn't issue a warning. Can someone tell me why?
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Photo; use Tk::JPEG; # substitute any jpg file here, and it should be # scaled down by a factor of 4 and put in the # button my $file = "zen16.jpg"; my $w = new MainWindow; my $image1 = $w->Photo( -file => "$file", -format => 'jpeg' ); my $image = $w->Photo(); $image->copy( $image1, -subsample => 4 ); $b = $w->Button( -image => $image )->pack(); $b->configure( -image => $image ); MainLoop;

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

2006-07-05 Retitled by broquaint, as per Monastery guidelines
Original title: 'Why dosn't strict complain here'

Replies are listed 'Best First'.
Re: Why doesn't strict complain here
by davorg (Chancellor) on Jul 05, 2006 at 12:45 UTC

    $a and $b are special variables (because of their use in sort routines). They are therefore not subject to strict checks.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Why doesn't strict complain here?
by derby (Abbot) on Jul 05, 2006 at 12:47 UTC

    $a and $b are special vars (you really shouldn't use them). From perlvar:

    Special package variables when using sort(), see "sort" in perlfunc. Because of this specialness $a and $b don't need to be declared (using use vars, or our()) even when using the "strict 'vars'" pragma. Don't lexicalize them with "my $a" or "my $b" if you want to be able to use them in the sort() comparison block or function.

    -derby
Re: Why doesn't strict complain here?
by zentara (Cardinal) on Jul 05, 2006 at 12:51 UTC
    Doh !!!! Thanks..... I need to post a dumb question once in awhile to keep up my reputation. :-)

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Collision with "$a" and "$b" built-ins is one of the hazards of using single letter variables, along with
      • making it difficult to search for the variable
      • others maintaining code
      I used to think "$i" was just fine as an index variable, but my collegues, after grumbling in private for several years, convinced me that I could aford to type "$idx". (although it wasn't easy to give up the habit.)
        Yeah, it's a bad habit from Engineering school where everything is x,y,z,w,t,i,j,k.

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