NAME

Getopt::Module - handle -M and -m options like perl

SYNOPSIS

use Getopt::Long;
use Getopt::Module qw(GetModule);

my ($modules, $eval);

GetOptions(
    'M|module=s' => GetModule(\$modules),
    'm=s'        => GetModule(\$modules, no_import => 1),
    'e|eval=s'   => \$eval,
);

my $sub = eval "sub { $modules $eval }";
command -Mautobox::Core -MBar=baz,quux -e '$_->split(...)->map(...)->join(...)'

DESCRIPTION

This module provides a convenient way for command-line Perl scripts to handle -M and -m options in the same way as perl.

EXPORTS

None by default.

GetModule

Signature: (ArrayRef | CodeRef | HashRef | ScalarRef [, Hash | HashRef ]) -> (Str, Str) -> HashRef

my $sub = GetModule($target, %options);

Takes a target and an optional hash or hashref of options and returns a subroutine which takes an option name and a perl -M/-m-style option value and assigns the value's components (module name, import type and parameters) to the target in the following ways.

TARGETS

ScalarRef

evalable use/no statements are appended to the referenced scalar, separated by the "separator" option. If no separator is supplied, it defaults to a single space (" "), e.g.:

Command:

command -MFoo=bar -M-Baz=quux

Usage:

my $modules;

GetOptions(
    'M|module=s' => GetModule(\$modules),
);

Result ($modules):

"use Foo qw(bar); no Baz qw(quux);"

ArrayRef

The use/no statement is pushed onto the arrayref, e.g.:

Command:

command -MFoo=bar,baz -M-Quux

Usage:

my $modules = [];

GetOptions(
    'M|module=s' => GetModule($modules),
);

Result ($modules):

[ "use Foo qw(bar baz);", "no Quux;" ]

HashRef

Pushes the statement onto the arrayref pointed to by $hash->{ $module_name }, creating it if it doesn't exist, e.g.:

Command:

command -MFoo=bar -M-Foo=baz -MQuux

Usage:

my $modules = {};

GetOptions(
    'M|module=s' => GetModule($modules),
);

Result ($modules):

{
    Foo  => [ "use Foo qw(bar);", "no Foo qw(baz);" ],
    Quux => [ "use Quux;" ],
}

CodeRef

The coderef is passed 3 arguments:

  • name

    The name of the Getopt::Long option, e.g. M.

  • eval

    The option's value as a use or no statement, e.g.: "use Foo qw(bar baz);".

  • parsed

    A hashref containing the option's parsed components, e.g.:

    Command:

    command -MFoo=bar,baz

    Usage:

    sub callback { ... }
    
    GetOptions(
        'M|module=s' => GetModule(\&callback),
    );

    The following hashref would be passed as the third argument to the callback sub:

    {
        args      => 'bar,baz',              # the import/unimport args; undef if none are supplied
        eval      => 'use Foo qw(bar baz);', # the evalable statement representing the option's value
        method    => 'import',               # the method call represented by the statement: either "import" or "unimport"
        module    => 'Foo'                   # the module name
        name      => 'M',                    # the Getopt::Long option name
        statement => 'use',                  # the statement type: either "use" or "no"
        value     => 'Foo=bar,baz',          # The Getopt::Long option value
    }

OPTIONS

no_import

By default, if no import/unimport parameters are supplied, e.g.:

command -MFoo

the use/no statement omits the parameter list:

use Foo;

If no parameters are supplied and no_import is set to a true value, the resulting statement disables the import/unimport method call by passing an empty list, e.g.:

use Foo ();

This corresponds to perl's -m option.

separator

The separator used to separate statements assigned to the scalar-ref target. Default: a single space (" ").

VERSION

1.0.0

SEE ALSO

AUTHOR

chocolateboy <chocolate@cpan.org>

COPYRIGHT AND LICENSE

Copyright (c) 2014-2020 by chocolateboy.

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.