File: | lib/Yukki/TextUtil.pm |
Coverage: | 88.1% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Yukki::TextUtil; | ||||||
2 | |||||||
3 | 4 4 | 29 8 | use v5.24; | ||||
4 | 4 4 4 | 10 5 16 | use utf8; | ||||
5 | |||||||
6 | 4 4 4 | 1113 22747 46 | use Encode (); | ||||
7 | 4 4 4 | 3377 70212 79 | use IO::Prompter (); | ||||
8 | 4 4 4 | 21 5 132 | use Path::Tiny; | ||||
9 | 4 4 4 | 13 6 30 | use YAML (); | ||||
10 | |||||||
11 | 4 4 4 | 13 5 24 | use namespace::clean; | ||||
12 | |||||||
13 | # ABSTRACT: Utilities to help make everything happy UTF-8 | ||||||
14 | |||||||
15 - 19 | =head1 DESCRIPTION Yukki aims at fully supporting UTF-8 in everything it does. Please report any bugs you find. This library exports tools used internally to help make sure that input is decoded from UTF-8 on the way in and encoded into UTF-8 on the way out. =cut | ||||||
20 | |||||||
21 | 4 | 1479 | use Sub::Exporter -setup => { | ||||
22 | exports => [ qw( | ||||||
23 | dump_file | ||||||
24 | load_file | ||||||
25 | prompt | ||||||
26 | ) ], | ||||||
27 | 4 4 | 1847 39 | }; | ||||
28 | |||||||
29 - 37 | =head1 SUBROUTINES =head2 dump_file dump_file($file, $data); This is pretty much identical in purpose to L<YAML/DumpFile>, but encodes to UTF-8 on the way out. =cut | ||||||
38 | |||||||
39 | sub dump_file { | ||||||
40 | 0 | 1 | 0 | my ($file, $data) = @_; | |||
41 | 0 | 0 | path($file)->spew_utf8(YAML::Dump($data)); | ||||
42 | } | ||||||
43 | |||||||
44 - 50 | =head2 load_file $data = load_file($file); This is similar to L<YAML/LoadFile>, but decodes from UTF-8 while reading input. =cut | ||||||
51 | |||||||
52 | sub load_file { | ||||||
53 | 4 | 1 | 4157 | my ($file) = @_; | |||
54 | 4 | 14 | YAML::Load(path($file)->slurp_utf8); | ||||
55 | } | ||||||
56 | |||||||
57 - 63 | =head2 prompt $value = prompt(...); This is similar to L<IO::Prompter/prompt>, but decodes UTF-8 in the input. =cut | ||||||
64 | |||||||
65 | sub prompt { | ||||||
66 | 0 | 1 | Encode::decode('UTF-8', IO::Prompter::prompt(@_)); | ||||
67 | } | ||||||
68 | |||||||
69 | 1; |