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?

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

    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

      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';

      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.

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?
[Corion]: ... an aggregator site that also listed (ideally) all of these links once. And maybe also have an exclude list to blacklist some of the most spammy links that the site may have never mentioned at all
[marto]: I gave up reading HN at all, and feel much better for it :P
[Corion]: Now, how to best automate that Google search ... :-)
[Corion]: marto: Yeah, I'm also short of not reading it at all. I already stopped reading it in the morning because it gave me a foul mood.
[Corion]: The good posts on HN are the non-computing posts. Most of the other stuff is maybe relevant to you if you are 20 and live in Silicon Valley...
[Corion]: Two attributes that don't describe me.
[Corion]: Maybe I should also write a curator for HN or simply not read it anymore, like you do.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-07-24 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (354 votes). Check out past polls.