Aion-Format
view release on metacpan or search on metacpan
lib/Aion/Format.pm view on Meta::CPAN
# np Ñ Ð¿ÑедÑÑÑановленнÑми опÑиÑми
sub np($;%) {
my ($arg, %properties) = @_;
%properties = %{_extends_ddp_properties(\%properties)};
DDP::np $arg, %properties
}
#@category ÐовÑÑки
# ÐовÑÑка Ð´Ð»Ñ STDERR
sub trapperr(&) {
my $sub = shift;
local *STDERR;
open STDERR, '>:utf8', \my $f; my $guard = Guard::guard { close STDERR };
$sub->();
undef $guard;
utf8::decode($f) unless utf8::is_utf8($f);
$f
}
# ÐовÑÑка Ð´Ð»Ñ STDOUT
sub trappout(&) {
my $sub = shift;
local *STDOUT;
open STDOUT, '>:utf8', \my $f; my $guard = Guard::guard { close STDOUT };
$sub->();
undef $guard;
utf8::decode($f) unless utf8::is_utf8($f);
$f
}
#@category ЦвеÑ
# ÐолоÑизиÑÑÐµÑ ÑекÑÑ escape-поÑледоваÑелÑноÑÑÑми: coloring("#{BOLD RED}ya#{}100!#RESET"), а заÑем - заменÑÐµÑ ÑоÑÐ¼Ð°Ñ sprintf-ом
sub coloring(@) {
my $s = shift;
$s =~ s!#\{(?<x>[\w \t]*)\}|#(?<x>\w+)!
my $x = $+{x};
$x = "RESET" if $x ~~ [qw/r R/];
Term::ANSIColor::color($x)
!nge;
sprintf $s, @_
}
# ÐеÑаÑÐ°ÐµÑ Ð² STDOUT вÑвод coloring
sub printcolor(@) {
print coloring @_
}
# ÐеÑаÑÐ°ÐµÑ Ð² STDERR вÑвод coloring
sub warncolor(@) {
print STDERR coloring @_
}
# ÐÐ»Ñ ÐºÑона: ÐиÑÐµÑ Ð² STDOUT
sub accesslog(@) {
print "[", POSIX::strftime("%F %T", localtime), "] ", coloring @_;
}
# ÐÐ»Ñ ÐºÑона: ÐиÑÐµÑ Ð² STDIN
sub errorlog(@) {
print STDERR "[", POSIX::strftime("%F %T", localtime), "] ", coloring @_;
}
#@category ÐÑеобÑазованиÑ
# ÐÑÐ¾Ð²Ð¾Ð´Ð¸Ñ ÑооÑвеÑÑÑвиÑ
#
# replace "...", qr/.../ => sub {...}, ...
#
sub matches($@) { goto &replace }
sub replace($@) {
my $s = shift;
my $i = 0;
my $re = join "\n| ", map { $i++ % 2 == 0? "(?<I$i> $_ )": () } @_;
my $arg = \@_;
my $fn = sub {
for my $k (keys %+) {
return $arg->[$k]->() if do { $k =~ /^I(\d+)\z/ and $k = $1 }
}
};
$s =~ s/$re/$fn->()/gex;
$s
}
#@category ТÑанÑлиÑеÑаÑиÑ
# ТÑанÑлиÑеÑÑиÑÑÐµÑ ÑÑÑÑкий ÑекÑÑ (x, w, q)
our %TRANS = qw/
а a и i п p Ñ c Ñ eh
б b й y Ñ r Ñ ch Ñ ju
в v к k Ñ s Ñ sh Ñ ja
г g л l Ñ t Ñ sch
д d м m Ñ u Ñ qh
е e н n Ñ f Ñ y
Ñ jo о o Ñ
kh Ñ q
ж zh
з z
/;
sub transliterate($) {
my ($s) = @_;
$s =~ s/[а-ÑÑ]/lc($&) eq $&? $TRANS{$&}: ucfirst $TRANS{lc $&}/gier;
}
# ТÑанÑлиÑеÑÑиÑÑÐµÑ ÑекÑÑ, оÑÑавлÑÑ ÑолÑко лаÑинÑкие бÑÐºÐ²Ñ Ð¸ ÑиÑе
sub trans($) {
my ($s) = @_;
$s = transliterate $s;
$s =~ s{[-\s_]+}{-}g;
$s =~ s![^a-z-]!!gi;
$s =~ s!^-*(.*?)-*\z!$1!;
lc $s
}
#@category СÑÑоки
# ÐÑеобÑазÑÐµÑ Ð² ÑÑÑÐ¾ÐºÑ perl
sub to_str(;$) {
lib/Aion/Format.pm view on Meta::CPAN
10 => "Ð´Ð»Ñ Ð¿ÑоÑеÑÑионалов",
0 => "Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²",
);
sub flesch_index_human($) {
my ($flesch_index) = @_;
$FLESCH_INDEX_NAMES{int($flesch_index / 10) * 10} // "неÑвÑзнÑй ÑÑÑÑкий ÑекÑÑ"
}
1;
__END__
=encoding utf-8
=head1 NAME
Aion::Format - a Perl extension for formatting numbers, coloring output, etc.
=head1 VERSION
0.1.2
=head1 SYNOPSIS
use Aion::Format;
trappout { print "123\n" } # => 123\n
coloring "#red ⬠#r\n" # => \e[31m ⬠\e[0m\n
trappout { printcolor "#red ⬠#r\n" } # => \e[31m ⬠\e[0m\n
=head1 DESCRIPTION
Utilities for formatting numbers, coloring output, etc.
=head1 SUBROUTINES
=head2 coloring ($format, @params)
Colorizes text using escape sequences and then replaces the format with C<sprintf>. The color names are used from the C<Term::ANSIColor> module. For B<RESET> use C<#r> or C<#R>.
coloring "#{BOLD RED}###r %i", 6 # => \e[1;31m##\e[0m 6
=head2 printcolor ($format, @params)
Like C<coloring>, but prints the formatted string to standard output.
=head2 warncolor ($format, @params)
Like C<coloring>, but prints the formatted string to C<STDERR>.
trapperr { warncolor "#{green}ACCESS#r %i\n", 6 } # => \e[32mACCESS\e[0m 6\n
=head2 accesslog ($format, @params)
Writes to STDOUT using the C<coloring> function for formatting and adds a date-time prefix.
trappout { accesslog "#{green}ACCESS#r %i\n", 6 } # ~> \[\d{4}-\d{2}-\d{2} \d\d:\d\d:\d\d\] \e\[32mACCESS\e\[0m 6\n
=head2 errorlog ($format, @params)
Writes to B<STDERR> using the C<coloring> function for formatting and adds a date-time prefix.
trapperr { errorlog "#{red}ERROR#r %i\n", 6 } # ~> \[\d{4}-\d{2}-\d{2} \d\d:\d\d:\d\d\] \e\[31mERROR\e\[0m 6\n
=head2 p ($target; %properties)
C<p> from Data::Printer with preset settings.
Instead of the inconvenient first parameter, a simple scalar is used.
The optional C<%properties> parameter allows you to override settings.
trapperr { p +{cat => 123} } # ~> cat.+123
=head2 np ($target; %properties)
C<np> from Data::Printer with preset settings.
Instead of the inconvenient first parameter, a simple scalar is used.
The optional C<%properties> parameter allows you to override settings.
np +{cat => 123} # ~> cat.+123
=head2 flesch_index_human ($flesch_index)
Converts the Flesch index to a Russian label using step 10.
flesch_index_human -10 # => неÑвÑзнÑй ÑÑÑÑкий ÑекÑÑ
flesch_index_human -3 # => Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²
flesch_index_human 0 # => Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²
flesch_index_human 1 # => Ð´Ð»Ñ Ð°ÐºÐ°Ð´ÐµÐ¼Ð¸ÐºÐ¾Ð²
flesch_index_human 15 # => Ð´Ð»Ñ Ð¿ÑоÑеÑÑионалов
flesch_index_human 99 # => Ð´Ð»Ñ 11 Ð»ÐµÑ (ÑÑÐ¾Ð²ÐµÐ½Ñ 5-го клаÑÑа)
flesch_index_human 100 # => Ð´Ð»Ñ Ð¼Ð»Ð°Ð´ÑеклаÑÑников
flesch_index_human 110 # => неÑвÑзнÑй ÑÑÑÑкий ÑекÑÑ
=head2 from_radix ($string, $radix)
Parses a natural number in the specified number system. The default is the 64-digit system.
The symbols used for numbers are 0â9, AâZ, aâz, _, and â. These characters are used before and for the 64 character system. For numbers after the 64-digit system, B<CP1251> encoding characters are used.
from_radix "A-C" # -> 45004
from_radix "A-C", 64 # -> 45004
from_radix "A-C", 255 # -> 666327
eval { from_radix "A-C", 256 }; $@ # ~> The number system 256 is too large. Use NS before 256
=head2 to_radix ($number, $radix)
Converts a natural number to a given number system. The default is the 64-digit system.
to_radix 10_000 # => 2SG
to_radix 10_000, 64 # => 2SG
to_radix 10_000, 255 # => dt
eval { to_radix 0, 256 }; $@ # ~> The number system 256 is too large. Use NS before 256
=head2 kb_size ($number)
Adds numeric digits and adds a unit of measurement.
kb_size 102 # => 102b
kb_size 1024 # => 1k
lib/Aion/Format.pm view on Meta::CPAN
=item * L<Roman::Unicode> uses the numbers â (5000), â (1000) and so on.
=item * L<Acme::Roman> adds support for Roman numerals in Perl code (C<< I + II -E<gt> III >>), but only uses the C<+>, C<-> and C<*> operators.
=item * L<Date::Roman> is an object-oriented Perl extension for handling Roman-style dates but with Arabic numerals (id 3,702).
=item * L<DateTime::Format::Roman> - Roman date formatter, but with Arabic numerals (5 Kal Jun 2003).
=back
=head2 round ($number, $decimal)
Rounds a number to the specified decimal place.
round 1.234567, 2 # -> 1.23
round 1.235567, 2 # -> 1.24
=head2 sinterval ($interval)
Creates human-readable spacing.
The width of the result is 12 characters.
sinterval 6666.6666 # => 01:51:06.667
sinterval 6.6666 # => 00:00:06.667
sinterval .333 # => 0.33300000 s
sinterval .000_33 # => 0.3300000 ms
sinterval .000_000_33 # => 0.330000 mks
=head2 sround ($number, $digits)
Leaves C<$digits> digits after the last zero (the 0 itself is ignored).
By default C<$digits> is 2.
sround 10.11 # -> 10
sround 12.11 # -> 12
sround 100.11 # -> 100
sround 133.11 # -> 133
sround 0.00012 # -> 0.00012
sround 1.2345 # -> 1.2
sround 1.2345, 3 # -> 1.23
=head2 trans ($s)
Transliterates Russian text, leaving only Latin letters and dashes.
trans "ÐÐ¸Ñ Ð²Ð¾ вÑÑм ÐиÑе!" # => mir-vo-vsjom-mire
=head2 transliterate ($s)
Transliterates Russian text.
transliterate "ÐÐ¸Ñ Ð²Ð¾ вÑÑм ÐиÑе!" # => Mir vo vsjom Mire!
=head2 trapperr (&block)
Trap for B<STDERR>.
If there is an error in the block, C<STDOUT> is restored, but the output in the block is lost.
trapperr { print STDERR "Stars: â¨" } # => Stars: â¨
See also C<IO::Capture::Stderr>.
=head2 trappout (&block)
Trap for B<STDOUT>.
If there is an error in the block, C<STDOUT> is restored, but the output in the block is lost.
trappout { print "Stars: â¨" } # => Stars: â¨
trappout { print "Stars: â¨"; die "error" } # @=> error
See also C<IO::Capture::Stdout>.
=head2 TiB ()
The constant is equal to one tebibyte.
TiB # -> 2**40
=head2 GiB ()
The constant is equal to one gibibyte.
GiB # -> 2**30
=head2 MiB ()
The constant is equal to one mebibyte.
MiB # -> 2**20
=head2 KiB ()
The constant is equal to one kibibyte.
KiB # -> 2**10
=head2 xxL ()
Maximum length of LongText mysql and mariadb data.
L - large.
xxL # -> 4*GiB-1
=head2 xxM ()
Maximum length of MediumText mysql and mariadb data.
M - medium.
xxM # -> 16*MiB-1
=head2 xxR ()
Maximum text length of mysql and mariadb data.
R - regularity.
xxR # -> 64*KiB-1
=head2 xxS ()
Maximum length of TinyText mysql and mariadb data.
S - small.
xxS # -> 255
=head2 to_str (;$scalar)
Convert to Perl string without interpolation.
to_str "a'\n" # => 'a\\'\n'
( run in 0.685 second using v1.01-cache-2.11-cpan-59e3e3084b8 )