import trick

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;
[at] Foo::ISA = qw(Exporter);
[at] Foo::EXPORT = qw(nap);

sub import {
Foo->export_to_level(1, [at] _);
}
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.
newtan [ Mi, 02 Januar 2008 23:29 ] [ ID #1898399 ]

Re: import trick

On 1=D4=C23=C8=D5, =C9=CF=CE=E76=CA=B129=B7=D6, Coolio <new... [at] gmail.com> 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;
> [at] Foo::ISA =3D qw(Exporter);
> [at] Foo::EXPORT =3D qw(nap);
>
> sub import {
> Foo->export_to_level(1, [at] _);
> }
> 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;
[at] ISA =3D qw(Exporter);
[at] EXPORT_OK =3D qw ($b);

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

" This will export the symbols one level =A1=AFabove=A1=AF the current=

package -
ie: to the program or module that used package A. "
murk [ Do, 03 Januar 2008 01:10 ] [ ID #1899157 ]

Re: import trick

On Jan 2, 2:29 pm, Coolio <new... [at] gmail.com> 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;
> [at] Foo::ISA = qw(Exporter);
> [at] Foo::EXPORT = qw(nap);
>
> sub import {
> Foo->export_to_level(1, [at] _);
> }
> 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
attn.steven.kuo [ Do, 03 Januar 2008 01:46 ] [ ID #1899159 ]
Perl » comp.lang.perl.misc » import trick

Vorheriges Thema: FAQ 7.1 Can I get a BNF/yacc/RE for the Perl language?
Nächstes Thema: "Use of initialized value in scalar assignment"