import trick

C

Coolio

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.
 
M

murk

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.

I think this might help:


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

sub import
{
$A::b = 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. "
 
A

attn.steven.kuo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top