develooper Front page | perl.perl5.porters | Postings from December 2000

[ID 19991029.006] Incorrect binding of lexicals

From:
Stephen P. Potter
Date:
December 13, 2000 12:19
Subject:
[ID 19991029.006] Incorrect binding of lexicals
Message ID:
200012132021.PAA06212@spotter.yi.org
This seems to still be an issue @8101.

-spp


Ala Qumsieh <aqumsieh@matrox.com> reports in c.l.p.misc that the
 following program gives incorrect output:

 *********************
 #!/usr/local/bin/perl -w

 use strict;

 package X;

 sub new {
         my $class = shift;

         my $obj = {
                 X   => 1,
                 Y   => 2,
                 EQN => '$x * $y * $t',
         };
         return bless $obj, $class;
 }

 {
         my ($x, $y);
         sub calculate {
                 my $obj = shift;

                 unless (defined $x) {           # <----  AAA
                         $x = $obj->{X};         # <----  BBB
                         $y = $obj->{Y};
                 }

                 my $t = shift;

 #               print "$x $y\n";

                 my $z = eval $obj->{EQN};
                 return $z;
         }
 }

 package main;

 my $x = new X;
 my $z = $x->calculate(3);

 print "Z is $z.\n";
 ***********************

 Now, running the above program generates the following:

 Use of uninitialized value at (eval 1) line 1.
 Use of uninitialized value at (eval 1) line 1.
 Z is 0.

 Bug shows in both 5.005_03 and 5.0065_62.

 Running under the debugger, we see at points AAA and BBB that $x and $y
 refer to different variables:

 X::calculate(-:21):                     unless (defined $x) {
   DB<1> x \$x, \$y
 0  SCALAR(0x1aba98)
    -> undef
 1  SCALAR(0x1cbc50)
    -> undef
   DB<2> s
 X::calculate(-:22):                             $x = $obj->{X};
   DB<2> x \$x, \$y
 0  SCALAR(0xdd678)
    -> undef
 1  SCALAR(0xdd690)
    -> undef
   DB<3> 

 So the binding of lexicals has got confused in some way.


 Mike Guy



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About