develooper Front page | perl.perl5.porters | Postings from February 2003

Re: [perl #21053] Lexical scoping bug concerning eval

Thread Previous
From:
Dave Mitchell
Date:
February 17, 2003 05:44
Subject:
Re: [perl #21053] Lexical scoping bug concerning eval
Message ID:
20030217134029.H13561@fdgroup.com
On Sat, Feb 15, 2003 at 03:13:43AM -0000, marcello@hummer.stanford.edu (via RT) wrote:
> Hi, I am reporting a lexical scoping bug of a nasty nature.
> I am running Perl v5.6.1 on a redhat 7.3 i386-Linux platform.
> Synopsis:
> Given a variable in package B, used from package A, package A can't access it
> in via an eval statement.
> 
> Example:
> Here is the short and sweet (or not so sweet)! version:
> __FILE__BEGINS__
> #!/usr/bin/perl
> package B;
> my $answer = 42;
> 1;
> package A;
> sub fn{
> 	print "answer=" . eval ('$B::answer') . "\n";
> 	print "answer=" . eval ('package B; $answer') . "\n";
>  }
> 1;
> package main;
> A::fn();
> 1;
> __FILE__ENDS__
> this prints:
> answer=
> answer=42

Thanks for the report; however, this is not a bug, as you have confused
lexical variables with package (global) variables.

lexical variables are declared with 'my', and are valid only for the
current lexical scope, ie until the next block end, sub end or file end,
whichever comes first - and they are not visible from other files.
They do not live in any package.

Global variables are accessible from all files and all parts of a program;
and are normally specified as package_name::variable_name; however, as
a convenience, you can drop the package_name:: prefix is that happens to
be the current package.



-- 
To collect all the latest movies, simply place an unprotected ftp server
on the Internet, and wait for the disk to fill....

Thread Previous


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