NAME

Aion::Format - a Perl extension for formatting numbers, coloring output, etc.

VERSION

0.1.0

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

DESCRIPTION

Utilities for formatting numbers, coloring output, etc.

SUBROUTINES

coloring ($format, @params)

Colorizes text using escape sequences and then replaces the format with sprintf. The color names are used from the Term::ANSIColor module. For RESET use #r or #R.

coloring "#{BOLD RED}###r %i", 6 # => \e[1;31m##\e[0m 6

printcolor ($format, @params)

Like coloring, but prints the formatted string to standard output.

warncolor ($format, @params)

Like coloring, but prints the formatted string to STDERR.

trapperr { warncolor "#{green}ACCESS#r %i\n", 6 }  # => \e[32mACCESS\e[0m 6\n

accesslog ($format, @params)

Writes to STDOUT using the 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

errorlog ($format, @params)

Writes to STDERR using the 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

p ($target; %properties)

p from Data::Printer with preset settings.

Instead of the inconvenient first parameter, a simple scalar is used.

The optional %properties parameter allows you to override settings.

trapperr { p +{cat => 123} } # ~> cat.+123

np ($target; %properties)

np from Data::Printer with preset settings.

Instead of the inconvenient first parameter, a simple scalar is used.

The optional %properties parameter allows you to override settings.

np +{cat => 123} # ~> cat.+123

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   # => несвязный русский текст

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, 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

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

kb_size ($number)

Adds numeric digits and adds a unit of measurement.

kb_size 102             # => 102b
kb_size 1024            # => 1k
kb_size 1023            # => 1\x{a0}023b
kb_size 1024*1024       # => 1M
kb_size 1000_002_000_001_000    # => 931\x{a0}324G

replace ($subject, @rules)

Multiple text transformations in one pass.

my $s = replace "33*pi",
    qr/(?<num> \d+)/x   => sub { "($+{num})" },
    qr/\b pi \b/x       => sub { 3.14 },
    qr/(?<op> \*)/x     => sub { " $& " },
;

$s # => (33) * 3.14

matches ($subject, @rules)

Synonym for replace. DEPRECATED.

my $s = matches "33*pi",
    qr/(?<num> \d+)/x   => sub { "($+{num})" },
    qr/\b pi \b/x       => sub { 3.14 },
    qr/(?<op> \*)/x     => sub { " $& " },
;

$s # => (33) * 3.14

nous ($templates)

A simplified regular expression language for text recognition in HTML documents.

1. Removes all spaces at the beginning and end.
2. From the beginning of each line, 4 spaces or 0-3 spaces and a tab are removed.
3. Spaces at the end of a line and strings of spaces are replaced with \s*.
4. All variables in {{ var }} are replaced with .*?. Those. everything is recognized.
5. All variables in {{> var }} are replaced with [^<>]*?. Those. HTML tags are not recognized.
6. All variables in {{: var }} are replaced with [^\n]*. Those. must be on one line.
7. Expressions in double square brackets ([[ ... ]]) may not exist.
8. Double brackets ((( ... )) are used as parentheses.
9. || - or.
my $re = nous [
q{
	<body>
	<center>
	<h2><a href={{> author_link }}>{{: author_name }}</a><br>
	{{ title }}</h2>
},
q{
    <li><A HREF="{{ comments_link }}">((Comments: {{ comments }}, last from {{ last_comment_posted }}.||Added comment))</A>
	<li><a href="{{ author_link }}">{{ author_name }}</a>
	[[ (translate: {{ interpreter_name }})]]
	 (<u>{{ author_email }}</u>) 
	<li>Year: {{ posted }}
},
q{
	<li><B><font color=#393939>Annotation:</font></b><br><i>{{ annotation_html }}</i></ul>
	</ul></font>
	</td></tr>
},
q{
	<!----------- The work itself --------------->
	{{ html }}
	<!------------------------------------------->
},
];

my $s = q{
<body>
<center>
<h2><a href=/to/book/link>A. Alis</a><br>
Grivus campf</h2>

Any others...

<!----------- The work itself --------------->
This book text!
<!------------------------------------------->
};

$s =~ $re;
my $result = {%+};
$result # --> {author_link => "/to/book/link", author_name => "A. Alis", title => "Grivus campf"}

num ($number)

Adds separators between digits of a number.

num +0         # => 0
num -1000.3    # => -1 000.3

The default separator is a non-breaking space. Set the separator and decimal point the same way:

num [1000, "#"]         		# => 1#000
num [-1000.3003003, "_", ","]   # => -1_000,3003003

See also Number::Format.

rim ($number)

Converts positive integers to Roman numerals.

rim 0       # => N
rim 4       # => IV
rim 6       # => VI
rim 50      # => L
rim 49      # => XLIX
rim 505     # => DV

Roman numerals after 1000:

rim 49_000      # => XLIX M
rim 49_000_000  # => XLIX M M
rim 49_009_555  # => XLIX IX DLV

See also:

round ($number, $decimal)

Rounds a number to the specified decimal place.

round 1.234567, 2  # -> 1.23
round 1.235567, 2  # -> 1.24

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

sround ($number, $digits)

Leaves $digits digits after the last zero (the 0 itself is ignored).

By default $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

trans ($s)

Transliterates Russian text, leaving only Latin letters and dashes.

trans "Мир во всём Мире!"  # => mir-vo-vsjom-mire

transliterate ($s)

Transliterates Russian text.

transliterate "Мир во всём Мире!"  # => Mir vo vsjom Mire!

trapperr (&block)

Trap for STDERR.

trapperr { print STDERR "Stars: ✨" }  # => Stars: ✨

See also IO::Capture::Stderr.

trappout (&block)

Trap for STDOUT.

trappout { print "Stars: ✨" }  # => Stars: ✨

See also IO::Capture::Stdout.

TiB ()

The constant is equal to one tebibyte.

TiB  # -> 2**40

GiB ()

The constant is equal to one gibibyte.

GiB  # -> 2**30

MiB ()

The constant is equal to one mebibyte.

MiB  # -> 2**20

KiB ()

The constant is equal to one kibibyte.

KiB  # -> 2**10

xxL ()

Maximum length of LongText mysql and mariadb data. L - large.

xxL  # -> 4*GiB-1

xxM ()

Maximum length of MediumText mysql and mariadb data. M - medium.

xxM  # -> 16*MiB-1

xxR ()

Maximum text length of mysql and mariadb data. R - regularity.

xxR  # -> 64*KiB-1

xxS ()

Maximum length of TinyText mysql and mariadb data. S - small.

xxS  # -> 255

to_str (;$scalar)

Convert to Perl string without interpolation.

to_str "a'\n" # => 'a\\'\n'
[map to_str, "a'\n"] # --> ["'a\\'\n'"]

from_str (;$one_quote_str)

Conversion from Perl string without interpolation.

from_str "'a\\'\n'"  # => a'\n
[map from_str, "'a\\'\n'"]  # --> ["a'\n"]

SUBROUTINES/METHODS

AUTHOR

Yaroslav O. Kosmina mailto:dart@cpan.org

LICENSE

GPLv3

COPYRIGHT

Aion::Format is copyright © 2023 by Yaroslav O. Kosmina. Rusland. All rights reserved.