Mailing List Archive


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

[tlug] perl scoping problem




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=
$
--------------------------------------





Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links