The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

NAME

Getopt::EX::Func - Function call interface

SYNOPSIS

use Getopt::EX::Func qw(parse_func);
my $func = parse_func(...);
$func->call;

DESCRIPTION

This module provides the way to create function call object used in Getopt::EX module set.

If your script has --begin option which tells the script to call specific function at the beginning of execution. You can do it like this:

use Getopt::EX::Func qw(parse_func);
GetOptions("begin:s" => $opt_begin);
my $func = parse_func($opt_begin);
$func->call;

Then script can be invoked like this:

% example -Mfoo --begin 'repeat(debug,msg=hello,count=2)'

In this example, function repeat should be declared in module foo or in start up rc file such as ~/.examplerc. Actual function call is done in this way:

repeat ( debug => 1, msg => 'hello', count => '2' );

As you can notice, arguments in the function call string is passed in name => value style. Parameter without value (debug in this example) is assigned value 1.

Function itself can be implemented like this:

our @EXPORT = qw(repeat);
sub repeat {
my %opt = @_;
print Dumper \%opt if $opt{debug};
for (1 .. $opt{count}) {
say $opt{msg};
}
}

It is also possible to declare the function in-line:

% example -Mfoo --begin 'sub{ say "wahoo!!" }'

Function say can be used because the function is executed under use v5.14 context.