our
@DEFAULT_EXPORTS
=
qw( throw_exception debug DEBUG_FLAG process_template )
;
sub
default_exports {
@DEFAULT_EXPORTS
}
sub
import
{
my
$class
=
shift
;
my
$target
=
caller
;
my
@exports
=
$class
->default_exports;
foreach
my
$exported_method
(
@exports
) {
my
$sub
=
sub
{
$class
->
$exported_method
(
$target
,
@_
) };
Moo::_Utils::_install_tracked(
$target
,
$exported_method
,
$sub
);
}
}
sub
throw_exception {
my
(
$class
,
$target
,
$class_name
,
@args
) =
@_
;
my
$namespace
=
"Valiant::Util::Exception::$class_name"
;
my
$exception
= Module::Runtime::use_module(
$namespace
)->new(
@args
);
die
$exception
->as_string;
}
sub
DEBUG_FLAG {
$ENV
{VALIANT_DEBUG} ? 1:0 }
sub
debug {
my
(
$class
,
$target
,
$target_level
,
@args
) =
@_
;
return
unless
exists
$ENV
{VALIANT_DEBUG};
my
(
$level
,
$package_pattern
) =
split
(
','
,
$ENV
{VALIANT_DEBUG});
if
(
$package_pattern
) {
return
unless
$target
eq (
$package_pattern
||
''
);
}
warn
"$target: @args\n"
if
$level
>=
$target_level
;
}
sub
process_template {
my
(
$class
,
$target
,
$template
,
%values
) =
@_
;
$template
=~ s/\{\{([^}]+)\}\}/
defined
(
$values
{$1}) ?
$values
{$1}:
''
/gex;
return
$template
;
}
1;