Util-Any
view release on metacpan or search on metacpan
lib/Util/Any.pm view on Meta::CPAN
package main;
use strict;
use lib qw(lib t/lib);
use SubExporterGenerator -test => [
min => {-as => "min_under_20", under => 20},
min => {-as => "min_under_5" , under => 5},
];
print min_under_20(100,25,30); # 20
print min_under_20(100,10,30); # 10
print min_under_20(100,25,30); # 5
print min_under_20(100,1,30); # 1
If you don't specify C<-as>, exported function as C<min>.
But, of course, the following doesn't work.
use SubExporterGenerator -test => [
min => {under => 20},
min => {under => 5},
];
Util::Any try to export duplicate function C<min>, one of both should fail.
=head4 GIVE DEFAULT ARGUMENTS TO CODE GENERATOR
You may want to give default arguments to all code generators in same kind.
For example, if you create shortcut to use Number::Format,
you may want to give common arguments with creating instance.
-number => [
[ 'Number::Format' => {
'round' => sub {
my($pkg, $class, $func, $args, $default_args) = @_;
my $n = 'Number::Format'->new(%$default_args);
sub { $n->round(@_); }
},
'number_format' => sub {
my($pkg, $class, $func, $args, $default_args) = @_;
my $n = 'Number::Format'->new(%$default_args, %$args);
sub { $n->format_number(@_); }
}
}
];
And write as the following:
use Util::Yours -number => [-args => {thousands_sep => "_", int_curr_symbol => '\'} ];
print number_format(100000); # 100_000
print number_price(100000); # \100_000
thousands_sep and int_curr_symbol are given to all of -number kind of function.
=head2 DO SOMETHING WITHOUT EXPORTING ANYTHING
-strict => [
[ 'strict' => {
'.' => sub {
strict->import();
warnings->import();
},
}
];
This definition works like as pragma.
use Util::Yours -strict;
function name '.' is special. This name is not exported and only execute the code in the definition.
=head2 ADD DEFAULT ARGUMENT FOR EXPORTING
Define the following method.
package You::Utils -Base;
# ....
sub _default_kinds { '-list', '-string' }
This means '-list' and '-string' arguments are given as default exporting arguments.
So, these are same.
use Your::Utils;
is equal to
use Your::Utils -list, -string;
If you want to disable default kinds.
use Your::Utils -list => [], -string;
=head2 ADD PLUGGABLE FEATURE FOR YOUR MODULE
Just add a flag -Pluggbale.
package Util::Yours;
use Util::Any -Base, -Pluggable;
And write plugin as the following:
package Util::Yours::Plugin::Net;
sub utils {
# This structure is as same as $Utils.
return {
# kind name and plugin name should be same.
-net => [
[
'Net::Amazon', '',
{
amazon => sub {
my ($pkg, $class, $func, $args) = @_;
my $amazon = Net::Amazon->new(token => $args->{token});
sub { $amazon }
},
}
]
]
};
( run in 2.098 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )