On Jan 28, [EMAIL PROTECTED] said: >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.
The fact that they're in the same file is why your test cases all fail. >$x = dosomething(); >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.) Right. >The documentation for perlmod implies there's an import method that's >part of exporter, so I tried something like this. Right. The 'import' method is a part of Exporter.pm, NOT of SomePackage. Thus, calling SomePackage::import() isn't going to work, since SomePackage doesn't HAVE an 'import' function. SomePackage->import('dosomething'); would have worked. It's the same as import SomePackage 'dosomething'; They're method-calling syntaces, not function calls. >import SomePackage (qw(dosomething)); >$x = dosomething(); >exit; > >package SomePackage; >use Exporter; >@ISA = qw(Exporter); >@EXPORT = qw(dosomething); >sub dosomething{ >return time; >} >(Not what I expected; why didn't it tell me main::import is undefined?) >Undefined subroutine &main::dosomething called at test_import.pl line 5. This is because you run into a problem caused by the the package being in the same file as your program. Some things in Perl are done at compile-time, and others are done at run-time. Functions are parsed at compile-time, which is why this code doesn't die because it can't find the 'foo' function: foo(); sub foo { print 100 } If subroutine definitions weren't parsed ahead of time, Perl wouldn't know you'd made a 'foo' function. In your code, simplified as follows, the problem is thus: Perl doesn't know to use Exporter's 'import' method, and so it uses SomePackage's 'import' method. However, as the documentation states, if a package doesn't HAVE an 'import' method, it silently fails (that is, no warning message is given, and no error is thrown). SomePackage->import('foo'); foo(); package SomePackage; require Exporter; @ISA = 'Exporter'; @EXPORT = 'foo'; sub foo { print "foo!\n" } You see, @ISA and @EXPORT aren't defined until Perl reaches them when EXECUTING your program. And since the first thing Perl executes is the 'import' call, those arrays are defined too late to help. One way to fix this is with a BEGIN block: SomePackage->import('foo'); foo(); BEGIN { package SomePackage; require Exporter; @ISA = 'Exporter'; @EXPORT = 'foo'; sub foo { print "foo!\n" } } The stuff in the BEGIN block is executed at compile-time. Therefore, when Perl executes SomePackage->import('foo'), it will know to look at Exporter's 'import' method, and things will work properly. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>