our
@EXPORT_OK
=
(
qw< bad_param_error unexpected_after_error named_after_optpos_error pos_after_named_error >
,
qw< mispositioned_slurpy_error multiple_slurpy_error named_slurpy_error >
,
qw< required_error named_param_error badval_error badtype_error >
,
);
sub
_regexify
{
my
(
$compile_time
,
$class
,
$obj
,
$method
,
$msg
,
%extra
);
$compile_time
= (
$_
[0] ||
''
) eq
'COMPILE_TIME'
;
if
(
$compile_time
)
{
(
undef
,
$msg
,
%extra
) =
@_
;
}
else
{
(
$obj
,
$method
,
$msg
,
%extra
) =
@_
;
$class
=
ref
$obj
||
$obj
||
'main'
;
}
my
$error
=
$compile_time
?
"$msg in declaration at "
:
"In call to ${class}::$method(), $msg at "
;
if
(
$extra
{LINE})
{
$extra
{FILE} ||= $0;
$error
.=
"$extra{FILE} line $extra{LINE}.\n"
;
}
if
(
$compile_time
)
{
$error
.=
"Compilation failed"
;
}
$error
=
quotemeta
$error
;
return
$extra
{LINE} && !
$compile_time
?
qr/\A$error\Z/
:
qr/\A$error/
;
}
sub
bad_param_error
{
my
(
$param
,
%extra
) =
@_
;
return
_regexify(
COMPILE_TIME
=>
"Could not understand parameter specification: $param"
,
%extra
);
}
sub
unexpected_after_error
{
my
(
$trailing
,
%extra
) =
@_
;
return
_regexify(
COMPILE_TIME
=>
"Unexpected extra code after parameter specification: '$trailing'"
,
%extra
);
}
sub
named_after_optpos_error
{
my
(
$named
,
$optpos
,
%extra
) =
@_
;
return
_regexify(
COMPILE_TIME
=>
"Named parameter '$named' mixed with optional positional '$optpos'"
,
%extra
);
}
sub
pos_after_named_error
{
my
(
$pos
,
$named
,
%extra
) =
@_
;
return
_regexify(
COMPILE_TIME
=>
"Positional parameter '$pos' after named param '$named'"
,
%extra
);
}
sub
mispositioned_slurpy_error
{
my
(
$param
,
%extra
) =
@_
;
return
_regexify(
COMPILE_TIME
=>
"Slurpy parameter '$param' must come at the end"
,
%extra
);
}
sub
multiple_slurpy_error
{
my
(
%extra
) =
@_
;
return
_regexify(
COMPILE_TIME
=>
"Signature can only have one slurpy parameter"
,
%extra
);
}
sub
named_slurpy_error
{
my
(
$param
,
%extra
) =
@_
;
return
_regexify(
COMPILE_TIME
=>
"Slurpy parameter '$param' cannot be named; use a reference instead"
,
%extra
);
}
sub
required_error
{
my
(
$obj
,
$varname
,
$method
,
%extra
) =
@_
;
return
_regexify(
$obj
,
$method
,
"missing required argument $varname"
,
%extra
);
}
sub
named_param_error
{
my
(
$obj
,
$varname
,
$method
,
%extra
) =
@_
;
return
_regexify(
$obj
,
$method
,
"does not take $varname as named argument(s)"
,
%extra
);
}
sub
badval_error
{
my
(
$obj
,
$varname
,
$type
,
$val
,
$method
,
%extra
) =
@_
;
$val
=
defined
$val
?
qq{"$val"}
:
'undef'
;
return
_regexify(
$obj
,
$method
,
"the '$varname' parameter ($val) is not of type $type"
,
%extra
);
}
sub
badtype_error
{
my
(
$obj
,
$type
,
$submsg
,
$method
,
%extra
) =
@_
;
return
_regexify(
$obj
,
$method
,
"the type $type is unrecognized ($submsg)"
,
%extra
);
}
1;