File Coverage

blib/lib/DateTime/Locale/Util.pm
Criterion Covered Total %
statement 24 30 80.0
branch 10 14 71.4
condition 3 6 50.0
subroutine 6 6 100.0
pod 0 1 0.0
total 43 57 75.4


line stmt bran cond sub pod time code
1             package DateTime::Locale::Util;
2              
3 16     16   88 use strict;
  16         35  
  16         395  
4 16     16   67 use warnings;
  16         29  
  16         517  
5 16     16   80 use namespace::autoclean 0.19 -except => ['import'];
  16         364  
  16         127  
6              
7 16     16   1063 use Exporter qw( import );
  16         32  
  16         4994  
8              
9             our $VERSION = '1.38';
10              
11             our @EXPORT_OK = 'parse_locale_code';
12              
13             # It might be better to redo this as something that looks up each piece in a
14             # catalog of codes. In particular, attempting to distinguish variants from
15             # scripts is basically impossible without hard-coding (see 'tarask' below) or
16             # by looking the codes up in a catalog.
17             sub parse_locale_code {
18 11     11 0 40 my @pieces = split /-/, $_[0];
19              
20 11 50       31 return unless @pieces;
21              
22 11         43 my %codes = ( language => lc shift @pieces );
23 11 100       53 if ( @pieces == 1 ) {
    50          
    100          
24 6 100 66     31 if ( length $pieces[0] == 2 || $pieces[0] =~ /^\d\d\d$/ ) {
    50          
25 5         17 $codes{territory} = uc shift @pieces;
26             }
27              
28             # The "be-Tarask" locale appears to be the only locale with a variant
29             # and no territory or script.
30             elsif ( lc $pieces[0] eq 'tarask' ) {
31 0         0 $codes{variant} = uc shift @pieces;
32             }
33             else {
34 1         4 $codes{script} = _tc( shift @pieces );
35             }
36             }
37             elsif ( @pieces == 3 ) {
38 0         0 $codes{script} = _tc( shift @pieces );
39 0         0 $codes{territory} = uc shift @pieces;
40 0         0 $codes{variant} = uc shift @pieces;
41             }
42             elsif ( @pieces == 2 ) {
43              
44             # I don't think it's possible to have a script + variant without also
45             # having a territory.
46 2 50 33     10 if ( length $pieces[1] == 2 || $pieces[1] =~ /^\d\d\d$/ ) {
47 2         8 $codes{script} = _tc( shift @pieces );
48 2         8 $codes{territory} = uc shift @pieces;
49             }
50             else {
51 0         0 $codes{territory} = uc shift @pieces;
52 0         0 $codes{variant} = uc shift @pieces;
53             }
54             }
55              
56 11         67 return %codes;
57             }
58              
59             sub _tc {
60 3     3   14 return ucfirst lc $_[0];
61             }
62              
63             1;
64              
65             # ABSTRACT: Utility code for DateTime::Locale
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             DateTime::Locale::Util - Utility code for DateTime::Locale
76              
77             =head1 VERSION
78              
79             version 1.38
80              
81             =head1 DESCRIPTION
82              
83             There are no user-facing parts in this module.
84              
85             =head1 SUPPORT
86              
87             Bugs may be submitted at L<https://github.com/houseabsolute/DateTime-Locale/issues>.
88              
89             There is a mailing list available for users of this distribution,
90             L<mailto:datetime@perl.org>.
91              
92             =head1 SOURCE
93              
94             The source code repository for DateTime-Locale can be found at L<https://github.com/houseabsolute/DateTime-Locale>.
95              
96             =head1 AUTHOR
97              
98             Dave Rolsky <autarch@urth.org>
99              
100             =head1 COPYRIGHT AND LICENSE
101              
102             This software is copyright (c) 2003 - 2023 by Dave Rolsky.
103              
104             This is free software; you can redistribute it and/or modify it under
105             the same terms as the Perl 5 programming language system itself.
106              
107             The full text of the license can be found in the
108             F<LICENSE> file included with this distribution.
109              
110             =cut