Config-IOD-Reader
view release on metacpan or search on metacpan
lib/Config/IOD/Expr.pm view on Meta::CPAN
package Config::IOD::Expr;
use 5.010;
use strict;
use warnings;
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2022-05-02'; # DATE
our $DIST = 'Config-IOD-Reader'; # DIST
our $VERSION = '0.345'; # VERSION
my $EXPR_RE = qr{
(?&ANSWER)
(?(DEFINE)
(?<ANSWER> (?&ADD))
(?<ADD> (?&MULT) | (?&MULT) (?: \s* ([+.-]) \s* (?&MULT) )+)
(?<MULT> (?&UNARY) | (?&UNARY) (?: \s* ([*/x%]) \s* (?&UNARY))+)
(?<UNARY> (?&POWER) | [!~+-] (?&POWER))
(?<POWER> (?&TERM) | (?&TERM) (?: \s* \*\* \s* (?&TERM))+)
(?<TERM>
(?&NUM)
| (?&STR_SINGLE)
| (?&STR_DOUBLE)
| undef
| (?&VAR)
| (?&FUNC)
| \( \s* ((?&ANSWER)) \s* \)
)
(?<FUNC> val \s* \( (?&TERM) \))
(?<NUM>
(
-?
(?: 0 | [1-9][0-9]* )
(?: \. [0-9]+ )?
(?: [eE] [-+]? [0-9]+ )?
)
)
(?<VAR> \$[A-Za-z_][A-Za-z0-9_]{0,63})
(?<STR_SINGLE>
(
'
(?:
[^\\']+
|
\\ ['\\]
|
\\
)*
'
)
)
(?<STR_DOUBLE>
(
"
(?:
[^\\"]+
|
\\ ["'\\\$tnrfbae]
# octal, hex, wide hex
)*
"
)
)
) # DEFINE
}msx;
sub _parse_expr {
my $str = shift;
return [400, 'Not a valid expr'] unless $str =~ m{\A$EXPR_RE\z}o;
my $res = eval "package Config::IOD::Expr::_Compiled; no strict; no warnings; $str"; ## no critic: BuiltinFunctions::ProhibitStringyEval
return [500, "Died when evaluating expr: $@"] if $@;
[200, "OK", $res];
}
1;
# ABSTRACT: Parse expression
__END__
=pod
=encoding UTF-8
=head1 NAME
Config::IOD::Expr - Parse expression
=head1 VERSION
This document describes version 0.345 of Config::IOD::Expr (from Perl distribution Config-IOD-Reader), released on 2022-05-02.
=head1 SYNOPSIS
( run in 1.434 second using v1.01-cache-2.11-cpan-5735350b133 )