Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] perl scoping problem



lexicals don't go in the symbol table. In layman's terms:
my $foo; and $main::foo aren't the same thing.  Get rid
of the my, and your code should work.

--Matt
On Wed, Dec 04, 2002 at 12:09:24AM -0800, Brett Robson wrote:
> 
> Hi all
> 
> For those that remember me, I've just moved to Tokyo from Nagoya. I've been threating to attend a nomikai for a while now.
> 
> 
> I've got a problem with Perl namespaces. I haven't used strict and packages before. I thought that variables in main are accessible from with packages. 
> 
> The following code doesn't do what I expect. I expect that $main::foo will have the value robbo.
> 
> T
> 
> 
> 
> $cat x.pl
> #!/usr/bin/perl -w
> use x1;
> 
> my $foo;
> my $bar = "brett";
> 
> 
> $foo = "robbo";
> 
> print "main\n";
> print "foo=".$foo;
> print "\n";
> 
> &x1::set1();
> 
> print "back in main\n";
> print "foo=", $main::foo;
> 
> print "\n";
> 
> --------------------------------------
> $ cat x1.pm
> package x1;
> use Exporter;
> 
> sub set1
> {
> print "set\n";
> print "foo=", $main::foo , "\n";
> print "foo=", $::foo , "\n";
> #print "foo=", $foo , "\n";
> print "leaving set\n";
> }
> 
> 1;
> 
> --------------------------------------
> 
> $ ./x.pl
> main
> foo=robbo
> set
> Use of uninitialized value in print at x1.pm line 7.
> foo=
> Use of uninitialized value in print at x1.pm line 8.
> foo=
> leaving set
> back in main
> Use of uninitialized value in print at ./x.pl line 19.
> foo=
> $
> --------------------------------------
> 
> 
> 
> 
> 
> **********************************************************
> TLUG server is hosted by Open Source Development Lab Japan
> http://www.osdl.jp/
> **********************************************************
> 
> ==========================================================
> To unsubscribe from this mailing list, 
> please see instructions at
> http://www.tlug.jp/list.html
> ==========================================================
> 
> 


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links