I still didn't manage to do it with Perl6::Export::Attrs (i'll try more - help appreciated - details at the end of this message).
But finally i managed to override the import() methods appropriately for the cases when Export is used. Simply overriding import() didn't work, as i lost the benefit of Export. I had to use the export_to_level() method (defined by Export) in the overriden import().
Here is the simple sample code (all files start with use strict and warnings).
package Mod2;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(subr); # Exported by default
sub import {
print "import() was called with arguments : @_\n";
my $my_arg = pop @_;
# i have to pop(),
# because export_to_level() doesn't want to see $my_arg
print "my arg: $my_arg\n";
Mod2->export_to_level(1, @_);
}
sub subr { return "subr() was called\n"; }
1;
And the script to run it :
use Mod2 'argument';
print subr();
Which displays :
import() was called with arguments : Mod2 argument
my arg: argument
subr() was called
It works !
Now with Perl6::Export::Attrs, i tried this (with the same script, using Mod3 instead of Mod2; i also tried to not put any argument after use Mod3;) :
package Mod3;
use Perl6::Export::Attrs;
sub subr :Export(:DEFAULT) { return "subr() was called\n"; }
IMPORT{ print "inside the IMPORT block\n"; }
1;
and obtained this output:
inside the IMPORT block
Can't call method "IMPORT" without a package or object reference at Mo
+d3.pm line 11.
Compilation failed in require at Script3.pl line 5.
BEGIN failed--compilation aborted at Script3.pl line 5.
Does anyone know how to use this IMPORT block properly?
|