import trick

import trick

am 02.01.2008 23:29:21 von newtan

hi,

in a file 'nap.pl', i have some code like this:

#!/usr/bin/perl -w
use strict;

Foo->import(nap);
nap();

{
package Foo;
use Exporter;
@Foo::ISA = qw(Exporter);
@Foo::EXPORT = qw(nap);

sub import {
Foo->export_to_level(1, @_);
}
sub nap {
select undef, undef, undef, 0.25;
print "Hi\n";
}
}

now if i run 'nap.pl', i'll get error "Can't locate object method
"export_to_level" via package "Foo"". how can i import subroutine
'nap' into 'main' from Foo? for simplicity, i removed package
variables from the example; thus, i'd like to keep the outer curly
brackets to maintain scope for these package variables. thanks.

Re: import trick

am 03.01.2008 01:10:49 von murk

On 1ÔÂ3ÈÕ, ÉÏÎç6ʱ29·Ö, Coolio wr=
ote:
> hi,
>
> in a file 'nap.pl', i have some code like this:
>
> #!/usr/bin/perl -w
> use strict;
>
> Foo->import(nap);
> nap();
>
> {
> package Foo;
> use Exporter;
> @Foo::ISA =3D qw(Exporter);
> @Foo::EXPORT =3D qw(nap);
>
> sub import {
> Foo->export_to_level(1, @_);
> }
> sub nap {
> select undef, undef, undef, 0.25;
> print "Hi\n";
> }
>
> }
>
> now if i run 'nap.pl', i'll get error "Can't locate object method
> "export_to_level" via package "Foo"". how can i import subroutine
> 'nap' into 'main' from Foo? for simplicity, i removed package
> variables from the example; thus, i'd like to keep the outer curly
> brackets to maintain scope for these package variables. thanks.

I think this might help:


package A;
@ISA =3D qw(Exporter);
@EXPORT_OK =3D qw ($b);

sub import
{
$A::b =3D 1;
A->export_to_level(1, @_);
}

" This will export the symbols one level ¡¯above¡¯ the current=

package -
ie: to the program or module that used package A. "

Re: import trick

am 03.01.2008 01:46:32 von attn.steven.kuo

On Jan 2, 2:29 pm, Coolio wrote:
> hi,
>
> in a file 'nap.pl', i have some code like this:
>
> #!/usr/bin/perl -w
> use strict;
>
> Foo->import(nap);
> nap();
>
> {
> package Foo;
> use Exporter;
> @Foo::ISA = qw(Exporter);
> @Foo::EXPORT = qw(nap);
>
> sub import {
> Foo->export_to_level(1, @_);
> }
> sub nap {
> select undef, undef, undef, 0.25;
> print "Hi\n";
> }
>
> }
>
> now if i run 'nap.pl', i'll get error "Can't locate object method
> "export_to_level" via package "Foo"". how can i import subroutine
> 'nap' into 'main' from Foo? for simplicity, i removed package
> variables from the example; thus, i'd like to keep the outer curly
> brackets to maintain scope for these package variables. thanks.


You can either (1) Move the two lines:

Foo->import('nap'); # note the argument is a string (not a bareword)
nap();

*after* the section in curly braces that defines the Foo package.

Or, (2) convert the section in curly braces to a BEGIN block.

In your current code, package Foo is incompletely defined when
Foo->import(...) is executed.

--
Hope this helps,
Steven