App-SahUtils
view release on metacpan or search on metacpan
script/coerce-with-sah view on Meta::CPAN
},
linenum => {
summary => 'When showing source code, add line numbers',
schema=>['bool', is=>1],
cmdline_aliases => {l=>{}},
tags => ['category:output'],
},
},
examples => [
{
src => q([[prog]] date --coerce-to DateTime --data-as-perl '"2016-05-22"'),
src_plang => 'bash',
},
{
summary => 'Coerce multiple data',
src => q([[prog]] date --coerce-to 'float(epoch)' --multiple-data-as-perl '["2016-05-15", "1463328281"]'),
src_plang => 'bash',
},
{
summary => 'Add one or more coerce rules',
src => q([[prog]] duration --coerce-rule From_str::human --coerce-rule From_str::iso8601 --data-as-perl '"P1Y2M"'),
src_plang => 'bash',
},
{
summary => 'Show source code',
src => q([[prog]] duration --coerce-to 'float(secs)' -c),
src_plang => 'bash',
},
{
summary => 'Show source code, with line number',
src => q([[prog]] duration --coerce-to 'DateTime::Duration' -c -l),
src_plang => 'bash',
},
{
summary => 'Show source code (JavaScript)',
src => q([[prog]] date -C js -c),
src_plang => 'bash',
},
],
};
sub coerce_with_sah {
script/coerce-with-sah view on Meta::CPAN
=head2 /home/u1/.config/coerce-with-sah.conf
=head2 /home/u1/coerce-with-sah.conf
=head2 /etc/coerce-with-sah.conf
=head1 EXAMPLES
=head2 Example #1
% coerce-with-sah date --coerce-to DateTime --data-as-perl '"2016-05-22"'
do {
my $a = bless({
formatter => undef,
local_c => {
day => 22,
day_of_quarter => 52,
day_of_week => 7,
day_of_year => 143,
hour => 0,
minute => 0,
script/coerce-with-sah view on Meta::CPAN
quarter_stand_alone_narrow => 'fix',
quarter_stand_alone_wide => 'fix',
script => undef,
territory => "United States",
time_format_full => "h:mm:ss\x{202F}a zzzz",
time_format_long => "h:mm:ss\x{202F}a z",
time_format_medium => "h:mm:ss\x{202F}a",
time_format_short => "h:mm\x{202F}a",
variant => undef,
version => 44,
}, "DateTime::Locale::FromData"),
offset_modifier => 0,
rd_nanosecs => 0,
tz => bless({
is_olson => 1,
max_year => 2033,
name => "Asia/Jakarta",
spans => [
["-Inf", 58904383968, "-Inf", 58904409600, 25632, 0, "LMT"],
[
58904383968,
script/coerce-with-sah view on Meta::CPAN
61514870400,
61946267400,
61514897400,
61946294400,
27000,
0,
"+0730",
],
[61946267400, "Inf", 61946292600, "Inf", 25200, 0, "WIB"],
],
}, "DateTime::TimeZone::Asia::Jakarta"),
utc_rd_days => 736105,
utc_rd_secs => 61200,
utc_year => 2017,
}, "DateTime");
$a->{locale}{locale_data}{am_pm_abbreviated} = $a->{locale}{am_pm_abbreviated};
$a->{locale}{locale_data}{available_formats} = $a->{locale}{available_formats};
$a->{locale}{locale_data}{day_format_abbreviated} = $a->{locale}{day_format_abbreviated};
$a->{locale}{locale_data}{day_format_narrow} = $a->{locale}{day_format_narrow};
$a->{locale}{locale_data}{day_format_wide} = $a->{locale}{day_format_wide};
$a->{locale}{locale_data}{day_stand_alone_abbreviated} = $a->{locale}{day_stand_alone_abbreviated};
$a->{locale}{locale_data}{day_stand_alone_narrow} = $a->{locale}{day_stand_alone_narrow};
$a->{locale}{locale_data}{day_stand_alone_wide} = $a->{locale}{day_stand_alone_wide};
$a->{locale}{locale_data}{era_abbreviated} = $a->{locale}{era_abbreviated};
$a->{locale}{locale_data}{era_narrow} = $a->{locale}{era_narrow};
script/coerce-with-sah view on Meta::CPAN
=head2 Show source code
% coerce-with-sah duration --coerce-to 'float(secs)' -c
require Scalar::Util;
require Time::Duration::Parse::AsHash;
sub {
my $data = shift;
unless (defined $data) {
return undef;
}
do { if ($data =~ /\A([0-9]{1,2}):([0-9]{1,2})(?::([0-9]{1,2})(\.[0-9]{1,9})?)?\z/) { my $res = do { if ($1 > 23) { ["Invalid hour '$1', must be between 0-23"] } elsif ($2 > 59) { ["Invalid minute '$2', must be between 0-59"] } elsif (defined $3...
}
=head2 Show source code, with line number
% coerce-with-sah duration --coerce-to 'DateTime::Duration' -c -l
1|require DateTime::Duration;
2|require Scalar::Util;
3|require Time::Duration::Parse::AsHash;
4|sub {
5| my $data = shift;
6| unless (defined $data) {
7| return undef;
8| }
9| do { if ($data =~ /\A([0-9]{1,2}):([0-9]{1,2})(?::([0-9]{1,2})(\.[0-9]{1,9})?)?\z/) { my $res = do { if ($1 > 23) { ["Invalid hour '$1', must be between 0-23"] } elsif ($2 > 59) { ["Invalid minute '$2', must be between 0-59"] } elsif (defin...
10|}
=head2 Show source code (JavaScript)
% coerce-with-sah date -C js -c
function (data) {
if (data === undefined || data === null) {
return null;
}
return ((typeof(data)=='number' && data >= 100000000 && data <= 2147483648) ? ((new Date(data * 1000))) : (function() { if ((data instanceof Date)) { var _tmp1 = isNaN(data) ? ['Invalid date', data] : [null, data]; if (_tmp1[0]) { return null } ...
script/validate-with-sah view on Meta::CPAN
src => q([[prog]] '["int","min",1,"max",10]' -c -l),
src_plang => 'bash',
summary => 'Show validator Perl source code only, with line number',
},
{
src => q([[prog]] '["date"]' -c --no-coerce),
src_plang => 'bash',
summary => 'Show validator Perl source code, with no coercion code (the validator will only accept epochs)',
},
{
src => q([[prog]] '["date",{"x.perl.coerce_to":"DateTime"}]' -c --no-coerce),
src_plang => 'bash',
summary => 'Show validator Perl source code, with no coercion code (the validator will only accept DateTime objects)',
},
{
src => q([[prog]] '["int","min",1,"max",10]' -c -C js),
src_plang => 'bash',
summary => 'Show validator JavaScript code',
},
{
src => q([[prog]] '["int","min",1,"max",10]' -C js -c -l),
src_plang => 'bash',
summary => 'Show validator JS source code only, with line number',
script/validate-with-sah view on Meta::CPAN
(!defined($data) ? 1 :
(# check type 'date'
((!ref($data) && $data =~ /\A[0-9]+\z/) ? 1 : (($err_data //= "Not of type date"),0))));
($err_data //= "");
return($err_data);
}}
=head2 Show validator Perl source code, with no coercion code (the validator will only accept DateTime objects)
% validate-with-sah '["date",{"x.perl.coerce_to":"DateTime"}]' -c --no-coerce
do {
no warnings ('void');
require Scalar::Util;
sub {
my ($data) = @_;
my $err_data;
my $_sahv_res =
# skip if undef
(!defined($data) ? 1 :
(# check type 'date'
((Scalar::Util::blessed($data) && $data->isa('DateTime')) ? 1 : (($err_data //= "Not of type date"),0))));
($err_data //= "");
return($err_data);
}}
=head2 Show validator JavaScript code
% validate-with-sah '["int","min",1,"max",10]' -c -C js
function(data) {
( run in 0.261 second using v1.01-cache-2.11-cpan-05444aca049 )