File Coverage

blib/lib/Parse/VarName.pm
Criterion Covered Total %
statement 20 20 100.0
branch 4 6 66.6
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Parse::VarName;
2              
3 1     1   608 use 5.010;
  1         3  
  1         36  
4 1     1   5 use strict;
  1         1  
  1         21  
5 1     1   4 use warnings;
  1         1  
  1         17  
6              
7 1     1   894 use Exporter::Lite;
  1         694  
  1         5  
8             our @EXPORT_OK = qw(split_varname_words);
9              
10             our $VERSION = '0.01'; # VERSION
11              
12             our %SPEC;
13              
14             # cannot be put inside sub, warning "Variable %s will not stay shared"
15             my @res;
16              
17             $SPEC{split_varname_words} = {
18             v => 1.1,
19             summary => 'Split words found in variable name',
20             description => <<'_',
21              
22             Try to split words found in a variable name, e.g. mTime -> [m, Time], foo1Bar ->
23             [foo, 1, Bar], Foo::barBaz::Qux2 -> [Foo, bar, Baz, Qux, 2].
24              
25             _
26             args => {
27             varname => {
28             schema => 'str*',
29             req => 1,
30             pos => 1,
31             },
32             include_sep => {
33             summary => 'Whether to include non-alphanum separator in result',
34             description => <<'_',
35              
36             For example, under include_sep=true, Foo::barBaz::Qux2 -> [Foo, ::, bar, Baz,
37             ::, Qux, 2].
38              
39             _
40             schema => [bool => {default=>0}],
41             },
42             },
43             result_naked => 1,
44             };
45             sub split_varname_words {
46 20     20 1 8595 my %args = @_;
47 20 50       49 my $v = $args{varname} or return [400, "Please specify varname"];
48              
49             #no warnings;
50 20         29 @res = ();
51 20 50       84 $v =~ m!\A(?:
52             (
53             [A-Z][A-Z]+ |
54             [A-Z][a-z]+ |
55             [a-z]+ |
56             [0-9]+ |
57             [^A-Za-z0-9]+
58             )
59             (?{ push @res, $1 })
60             )+\z!sxg
61             or return [];
62 20 100       43 unless ($args{include_sep}) {
63 16         18 @res = grep {/[A-Za-z0-9]/} @res;
  37         112  
64             }
65              
66 20         58 \@res;
67             }
68              
69             1;
70             # ABSTRACT: Routines to parse variable name
71              
72              
73             __END__