closure bug?

Michael Stillwell mjs at beebo.org
Fri Nov 8 00:13:15 CST 2002


Am I doing something wrong here or is there a bug in Perl's closure 
(and/or scoping) mechanism?

In the outer section, $data->{name} exists; in the inner section it 
doesn't.

#!/usr/bin/env perl

use strict;
use warnings;

{
     my $data = { name => "Michael" } ;

     sub foo {
         if (exists $data->{name}) {
             print "OUTER: EXISTS\n";
         }
         else {
             print "OUTER: DOES NOT EXIST\n";
         }

#       $_ = $data;

         my $fn = sub {
             if (exists $data->{name}) {
                 print "INNER: EXISTS\n";
             }
             else {
                 print "INNER: DOES NOT EXIST\n";
             }
         };

         $fn->();
     }
}

foo();

__END__

If the commented out line is uncommented, $date->{name} exists in the 
inner section too.  (Tested on 5.6, 5.6.1, 5.8.)




--M.

* * *
http://beebo.org




More information about the Melbourne-pm mailing list