> Hi, this question has to do with importing names from one package into another. In my case, both packages reside in the same file, and I simply want to import all the package-global symbols from the one package into the other. Can anyone say how to do this? Here's a bunch of tries that didn't work. >
This doesn't make a lot of sense to me, if you are going to export them all why bother? The big reason for the separate packages is to avoid namespace pollution, which if you export everything you have just counteracted. At least put them in a separate file, and use it? But to each their own.... > test_import.pl > -------------- > package main; > > print "Hi world\n"; > $x = dosomething(); > print "did something and got $x\n"; > exit; > > package SomePackage; > sub dosomething{ > return time; > } > > Obviously this isn't going to work because symbol dosomething is not defined in package main. (In this trivial example I could just say $x = SomePackage::dosomething(), but I have dozens of functions and I would prefer to simply import them into the namespace of main. Also, apart from this specific instance, I want to understand what I can do in general.) > > The documentation for perlmod implies there's an import method that's part of exporter, so I tried something like this. > > test_import.pl > -------------- > package main; > SomePackage::import(qw(dosomething)); > print "Hi world\n"; > $x = dosomething(); > print "did something and got $x\n"; > exit; > > package SomePackage; > use Exporter; > @ISA = qw(Exporter); > @EXPORT = qw(dosomething); > sub dosomething{ > return time; > } > Close, but import is a method not a plain function, so you need to call it on the class, SomePackage->import(); > outcome: > -------- > Undefined subroutine &SomePackage::import called at test_import.pl line 2. > > Another variation: > > test_import.pl > -------------- > package main; > import SomePackage (qw(dosomething)); > print "Hi world\n"; > $x = dosomething(); > print "did something and got $x\n"; > exit; > > package SomePackage; > use Exporter; > @ISA = qw(Exporter); > @EXPORT = qw(dosomething); > sub dosomething{ > return time; > } > > outcome: > -------- > (Not what I expected; why didn't it tell me main::import is undefined?) > Hi world > Undefined subroutine &main::dosomething called at test_import.pl line 5. > Because the syntax you causes Perl to realize it is supposed to be a method, now the problem is that SomePackage hasn't done any exporting yet, because it hasn't been seen yet. > Another variation: > > test_import.pl > -------------- > package main; > use SomePackage; > print "Hi world\n"; > $x = dosomething(); > print "did something and got $x\n"; > exit; > > package SomePackage; > use Exporter; > @ISA = qw(Exporter); > @EXPORT = qw(dosomething); > sub dosomething{ > return time; > } > > outcome: > -------- > This obviously won't work because 'use' expects a module name, not a package name. Same issue with 'require'. > > Can't locate SomePackage.pm in @INC (@INC contains: d:/app/xampp/perl/lib D:/app/xampp/perl/lib D:/app/xampp/perl/site/lib .) at test_import.pl line 3. > BEGIN failed--compilation aborted at test_import.pl line 3. > Which sort of tells you that you ought to be moving in that direction, but that's up to you... > That's it. Any ideas? > Go with your method call syntax above (or the one provided below) and either move the definition of SomePackage to the top of the script, or wrap the definition in a BEGIN {} compile time block.... so, #!/usr/local/bin/perl use strict; # Always! use warnings; # usually! Some::Package->import(); print "Hi world\n"; my $x = dosomething(); print "did something and got $x\n"; exit; BEGIN { package Some::Package; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(dosomething); sub dosomething { return time; } } HTH, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>