Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: How to pass an argument to a Module?

by Corion (Pope)
on Sep 09, 2012 at 12:44 UTC ( #992584=note: print w/replies, xml ) Need Help??

in reply to How to pass an argument to a Module?

See use (or perlmod). Both describe what use actually is.

In short, if you implement a subroutine import in your module, it will get called at use time.

Replies are listed 'Best First'.
Re^2: How to pass an argument to a Module?
by mascip (Pilgrim) on Sep 09, 2012 at 16:15 UTC

    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 line 11. Compilation failed in require at line 5. BEGIN failed--compilation aborted at line 5.
    Does anyone know how to use this IMPORT block properly?

      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.

      $ 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
Re^2: How to pass an argument to a Module?
by mascip (Pilgrim) on Sep 09, 2012 at 13:03 UTC

    Thank you.
    I understand that i have to override import(). But as i use Perl6::Export::Attrs, i'm reading in the doc that i have to use an IMPORT block instead. I'm going to try this.

    Another idea for "cheating" : having a CLASS() method which would contain my parameter, and which i would override by monkeypatching.
    Not very clean though, i'll try the IMPORT thing.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992584]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-04-24 01:19 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (433 votes). Check out past polls.