in reply to Re: How to pass an argument to a Module?
in thread How to pass an argument to a Module?
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).
And the script to run it :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;
Which displays :use Mod2 'argument'; print subr();
It works !import() was called with arguments : Mod2 argument my arg: argument subr() was called
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;) :
and obtained this output:package Mod3; use Perl6::Export::Attrs; sub subr :Export(:DEFAULT) { return "subr() was called\n"; } IMPORT{ print "inside the IMPORT block\n"; } 1;
Does anyone know how to use this IMPORT block properly?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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to pass an argument to a Module?
by Anonymous Monk on Sep 09, 2012 at 17:00 UTC | |
by mascip (Pilgrim) on Sep 09, 2012 at 18:25 UTC | |
by mascip (Pilgrim) on Sep 09, 2012 at 17:05 UTC | |
|
Re^3: How to pass an argument to a Module?
by Anonymous Monk on Sep 09, 2012 at 17:03 UTC |