On Wed, Jan 28, 2004 at 01:32:14PM -0500 [EMAIL PROTECTED] wrote:

> 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.
> 
> 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));

import() is a method and not a function. When you call it as a function
it no longer obeyes inheritance and thus cannot be found since
SomePackage inherits import() from Exporter.

> 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:
> --------
> 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?)

Because here you called it as a method. This is indirect method
invocation:

    import SomePackage qw(bla);

is the same as

    SomePackage->import(qw(bla));

Here perl was indeed able to call import(). However, the reason why it
still doesn't work as you expect is the order of execution. If you do

    package SomePackage;
    require Exporter;
    @ISA = qw(Exporter);
    @EXPORT = qw(dosomething);
    sub dosomething{
        return time;
    }

    package main;
    SomePackage->import( qw/dosomething/ );
    print dosomething();

things will work. This has to do with the fact that @EXPORT is still
empty when you do the import() and when you have package main before the
exporting package. That is because assignment happens at runtime.

In case you wonder why you got no error with

    package main;
    SomePackage->import(...);
    package SomePackage;
    use Exporter;
    @ISA = qw(Exporter);

even though @ISA was still empty by the time you called
SomePackage->import, this is because 'import' and 'unimport' are two
special cases. They can always be called as method even when they do not
exist nor are inherited. So in fact you can also do:

    package main;
    main->import( qw/bla/ );
    __END__

and wont get any error.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to