わいえむねっと

Contents
Categories
Calendar
2009/05
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Monthly Archives
~2000/01
Recent Entries
RSS1.0
Templates
Information
Processed: 0.019 sec
Chashed: -
2009/05/10 Sun
Exporterメモ。

$ cat foo.pm
package foo;

our @ISA = qw(Exporter);
our @EXPORT = qw(foo bar);

sub foo{print "foo\n"}
sub bar{print "bar\n"}
1;

$ cat foo.pl
use strict;
use warnings;
use foo;

foo();
bar();

$ perl foo.pl
foo
bar



$ cat foo.pm
package foo;

our @ISA = qw(Exporter);
our @EXPORT = qw(foo bar);

my $foo;
sub import
{
    $foo = $_[1];
}

sub foo{print "$foo\n"}
sub bar{print "bar\n"}
1;

$ cat foo.pl
use strict;
use warnings;
use foo qw(foobar);

foo();
bar();

とかしてuseする際に値を渡すようにしようとすると、

$ perl foo.pl
Undefined subroutine &main::foo called at foo.pl line 5.

そんなsubroutineないと怒られる。


を参考に

$ cat foo.pm
package foo;

our @ISA = qw(Exporter);
our @EXPORT = qw(foo bar);

my $foo;
sub import
{
    $foo = $_[1];
    foo->export_to_level(1, @_);
}

sub foo{print "$foo\n"}
sub bar{print "bar\n"}
1;

とすると、

$ perl foo.pl
"foobar" is not exported by the foo module
Can't continue after import errors at foo.pm line 10
BEGIN failed--compilation aborted at foo.pl line 3.

そんなものexportされていないと怒られる。
export_to_levelは

MyPackage->export_to_level($where_to_export, $package, @what_to_export);

のようになっているので、@what_to_exporに余計なモノが混入するのが原因。

$ cat foo.pm
package foo;

our @ISA = qw(Exporter);
our @EXPORT = qw(foo bar);

my $foo;
sub import
{
    $foo = $_[1];
    foo->export_to_level(1, $_[0]);
}

sub foo{print "$foo\n"}
sub bar{print "bar\n"}
1;

とすれば、

$ perl foo.pl
foobar
bar

とりあえず通る。