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

Dear monks,

I've been playing around with the Sub::Exporter module off CPAN. I like the way you can set up custom subroutines depending on the arguments passed to your module. My question is, how do you do this (pass arguments to build custom routines) using the -m flag in a one-liner?

Here is a simple example:

$ cat > Communicate.pm package Communicate; use 5.010_000; use strict; use warnings; sub build_greeter { my ( $class, $name, $arg ) = @_; return sub { my $person = shift // 'World'; my $comment = $arg->{greeting} // 'Hello'; say sprintf '%s, %s!', $comment, $person; } } use Sub::Exporter -setup => { exports => [ greet => \'build_greeter' ], }; 1; $ cat > communicate.pl #! perl use 5.010_000; use strict; use warnings; use lib '.'; use Communicate greet => { greeting => 'Well-done', -as => 'say_to', }; say_to('Nelo'); exit 0;

Now when I run communicate.pl I get the output I expect:

$ perl communicate.pl Well-done, Nelo!

Currently the following one-liner works:

$ perl -I. -e 'use Communicate greet => { greeting => q{Good job}, -as + => q{say_to} }; say_to(q{Nelo})' Good job, Nelo!

But I wondering if this could also work with the -m flag to get the same result and if so, how?

$ perl -I. -mCommunicate=greet<what would go in here?> -e 'say_to("Nelo")'

Thanks =)

Ps: This is just to satisfy my curiosity. I could very well use the one-liner as it stands above.


Smoothie, smoothie, hundre prosent naturlig!

Replies are listed 'Best First'.
Re: Building custom routines with Sub-Exporter using the "-m" flag in a perl one-liner
by jethro (Monsignor) on Aug 21, 2008 at 15:05 UTC

    Can't test it, don't have perl10 on this machine, so untested guess is:

    -mCommunicate=greet,"{greeting => 'Well-done',-as => 'say_to'}"

    UPDATE: Which is probably not the answer because of the split I guess. If that is the case, the commas in the string need to be substituted by \x... equivalents

      Thanks for that. I tried it out and got the following output:

      $ perl -IWorkspace/sub-exporter -mCommunicate=greet,"{ greeting => q{W +ell-done},-as => q{say_to} }" -e 'say_to(q{Nelo})' group "as => q{say_to} }" is not exported by the Communicate module at + -e line 0 BEGIN failed--compilation aborted. $

      When you said:

      ... the commas in the string need to be substituted by \x... equivalents

      I assume that has something to do with unicode ... but I don't know quite how I would use that in this situation I'm afraid =(


      Smoothie, smoothie, hundre prosent naturlig!

        "\x2c" in an interpolated ascii string is the same as ",". But my idea was a idea borne from fast talking, no thinking. Won't work, because it will not be interpolated once it reaches perl code.

        Actually I don't know a way to make it work, I tried a few things but no success. My guess is that while Sub::Exporter wants to get a hash reference, it only gets a string that would generate a hash reference if it were executed as perl. So instead of {-as =>something} it gets a string "{-as =>something}"