pwagyi has asked for the wisdom of the Perl Monks concerning the following question:

If I have an interface 'I', and possibly several implementations A, B,C that satisfies interface 'I'. How could I have a module M takes 'I' as parameter? Something similar to ML functor?

Replies are listed 'Best First'.
Re: module as parameter
by haukex (Archbishop) on Sep 21, 2017 at 06:58 UTC

    I'm not sure I fully understand your question, but if you are asking how to use different modules, then you'll find the answer in the replies in this thread, and there is also this thread which is a bit more recent and shows some additional methods to dynamically load a module.

    If you are talking about inheritance in OO, note that @ISA is basically an array of strings that can be manipulated as such.

    However, note that use happens at compile time and modifications to @ISA are global, so in case you want something more selective, it'd be better if you could explain some more, best would be with code. See SSCCE and How do I post a question effectively? Since Perl is very flexible in its OO mechanisms, it's very likely there is a way to do what you want.

Re: module as parameter
by ikegami (Patriarch) on Sep 21, 2017 at 04:21 UTC

    You'll need to describe in more detail what you want to do. For starters, "module" usually refers to a file or namespace, neither of which have parameters.

Re: module as parameter
by anonymized user 468275 (Curate) on Sep 21, 2017 at 12:21 UTC
    ML functor wouldn't help either, because parameter I is insufficient to determine which of A B C is indicated. So I will assume you mean that the implementation also needs to be indicated in the call. But we still need to know what are the criteria for choosing which implementation. Example of parameter driven calling however:
    package Action; use Moo; has interface => (is => 'ro'); has implementation => (is => 'ro'); our %action = (IA => sub {&intIimpA;}, IB => sub {&intIimpB;}, IC => sub {&intIimpC;}, ); sub doAction { my $self = shift; &{$action{$self->interface . $self->implementation}}; } # sub intIimpA{ # implementation code in here } sub intIimpB{ } sub intIimpC{ } 1;

    One world, one people

Re: module as parameter
by Anonymous Monk on Sep 21, 2017 at 03:57 UTC
    Perl doesn't do that, because it has a very loose type system. In the event that you need to pass a type as a parameter, you usually just pass the type name as a string.