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

Re: do() sees lexicals if called from sub

Ronald J Kimball
March 6, 2000 14:12
Re: do() sees lexicals if called from sub
Message ID:
On Mon, Mar 06, 2000 at 01:51:17PM -0800, Peter Scott wrote:
> Something I posted on clpmod recently was further refined by David Dyck and 
> appears to be a bug.  Observed in 5.005_03 on Solaris and 5.5.650 on Linux; 
> also observed by Malcolm Dew-Jones on 5.005_03 on Win32.
> Quoth perlfunc:
> 	code evaluated with do FILENAME cannot see lexicals
> 	in the enclosing scope;
> It appears that it *can* see lexicals in the enclosing scope if it is 
> called via a subroutine.  (I discovered this when I typed in the 
> pseudo-definition of require as a sub from perlfunc.)
> % cat foo
> my $x = "main";
> do 'bar';
> print "\$x set in $x\n";
> blech();
> print "\$x set in $x\n";
> sub blech {
>    $x = "blech";
>    do 'bar';
> }
> % cat bar
> $x = "bar";
> print "In bar\n";
> % perl -w -Mstrict foo
> In bar
> $x set in main
> In bar
> $x set in bar

It appears that the first do creates a package variable named $x, and that
after the second do the package variable has taken over the lexical

Stepping through the debugger, x $x and X x return different values up
until the second do 'bar';.

Ronald Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About