Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: How to pass an argument to a Module?

by mascip (Pilgrim)
on Sep 09, 2012 at 16:15 UTC ( #992599=note: print w/ replies, xml ) Need Help??


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

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?


Comment on Re^2: How to pass an argument to a Module?
Select or Download Code
Re^3: How to pass an argument to a Module?
by Anonymous Monk on Sep 09, 2012 at 17:00 UTC

    Does anyone know how to use this IMPORT block properly?

    Presumably the author. Seeing how the module never tests this IMPORT block business (Perl6-Export-Attrs-0.0.3/t/00.load.t), I would complain to the author.

    You can probably get away with writing  sub IMPORT {} , cause that works for BEGIN/END/... also

      IMPORT{ print "inside the IMPORT block, with arguments @_\n"; }
      works but does the same thing as when i override import() : my subroutines are not exported to the symbol table.
      I'll write to the author.

      The author's response (i tried it and it works, with sub IMPORT) :

      This is not a bug in the module.
      The problem is that you specified:

      sub subr :Export(:DEFAULT) { return "subr() was called\n"; }
      :DEFAULT exports are only exported when the module is use'd with no arguments at all.
      But then you call:
      use Mod3 'argument';
      with an argument, so the :DEFAULT exports don't happen.

      You could fix this by specifying:

      sub subr :Export(:MANDATORY) { return "subr() was called\n"; }
      (assuming you always want subr() exported).
      If not, then you need to specifically request subr(), like this:
      use Mod3 '&subr', 'argument';
      or request all the defaults, like this:
      use Mod3 ':DEFAULT', 'argument';

Re^3: How to pass an argument to a Module?
by Anonymous Monk on Sep 09, 2012 at 17:03 UTC
    $ perl -MO=Deparse,-p -e " IMPORT { warn(666); } " do { warn(666) }->IMPORT; -e syntax OK $ perl -MO=Deparse,-p -e " sub IMPORT { warn(666); } " sub IMPORT { warn(666); } -e syntax OK

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992599]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-12-20 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls