File Coverage

blib/lib/Data/Perl/Role/String.pm
Criterion Covered Total %
statement 40 40 100.0
branch 6 6 100.0
condition n/a
subroutine 13 13 100.0
pod 11 11 100.0
total 70 70 100.0


line stmt bran cond sub pod time code
1             package Data::Perl::Role::String;
2             $Data::Perl::Role::String::VERSION = '0.002009';
3             # ABSTRACT: Wrapping class for Perl scalar strings.
4              
5 9     9   6221 use strictures 1;
  9         55  
  9         221  
6              
7 9     9   600 use Role::Tiny;
  9         21  
  9         91  
8              
9 9     9 1 46 sub new { bless \(my $s = $_[1]), $_[0] }
10              
11 1     1 1 263 sub inc { ++${$_[0]} }
  1         4  
12              
13 1     1 1 256 sub append { ${$_[0]} = ${$_[0]} . $_[1] }
  1         3  
  1         2  
14              
15 1     1 1 258 sub prepend { ${$_[0]} = $_[1] . ${$_[0]} }
  1         3  
  1         4  
16              
17             sub replace {
18 2 100   2 1 843 if (ref($_[2]) eq 'CODE') {
19 1         2 ${$_[0]} =~ s/$_[1]/$_[2]->()/e;
  1         22  
  1         5  
20             }
21             else {
22 1         45 ${$_[0]} =~ s/$_[1]/$_[2]/;
  1         9  
23             }
24 2         241 ${$_[0]};
  2         8  
25             }
26              
27             sub match {
28 1     1 1 8 ${$_[0]} =~ /$_[1]/;
  1         8  
29             }
30              
31 1     1 1 2 sub chop { CORE::chop ${$_[0]} }
  1         4  
32              
33 1     1 1 9 sub chomp { CORE::chomp ${$_[0]} }
  1         6  
34              
35 1     1 1 2 sub clear { ${$_[0]} = '' }
  1         3  
36              
37 1     1 1 5 sub length { CORE::length ${$_[0]} }
  1         4  
38              
39             sub substr {
40 7 100   7 1 24 if (@_ >= 4) {
    100          
41 1         1 substr ${$_[0]}, $_[1], $_[2], $_[3];
  1         7  
42             }
43             elsif (@_ == 3) {
44 4         3 substr ${$_[0]}, $_[1], $_[2];
  4         17  
45             }
46             else {
47 2         3 substr ${$_[0]}, $_[1];
  2         8  
48             }
49             }
50              
51             1;
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             Data::Perl::Role::String - Wrapping class for Perl scalar strings.
60              
61             =head1 VERSION
62              
63             version 0.002009
64              
65             =head1 SYNOPSIS
66              
67             use Data::Perl qw/string/;
68              
69             my $string = string("foo\n");
70              
71             $string->chomp; # returns 1, $string == "foo"
72              
73             =head1 DESCRIPTION
74              
75             This class provides a wrapper and methods for interacting with scalar strings.
76              
77             =head1 PROVIDED METHODS
78              
79             =over 4
80              
81             =item * B
82              
83             Constructs a new Data::Perl::String object, optionally initialized to $value if
84             passed in, and returns it.
85              
86             =item * B
87              
88             Increments the value stored in this slot using the magical string autoincrement
89             operator. Note that Perl doesn't provide analogous behavior in C<-->, so
90             C is not available. This method returns the new value.
91              
92             This method does not accept any arguments.
93              
94             =item * B
95              
96             Appends to the string, like C<.=>, and returns the new value.
97              
98             This method requires a single argument.
99              
100             =item * B
101              
102             Prepends to the string and returns the new value.
103              
104             This method requires a single argument.
105              
106             =item * B
107              
108             Performs a regexp substitution (L). There is no way to provide the
109             C flag, but code references will be accepted for the replacement, causing
110             the regex to be modified with a single C. C can be applied using the
111             C operator. This method returns the new value.
112              
113             This method requires two arguments.
114              
115             =item * B
116              
117             Runs the regex against the string and returns the matching value(s).
118              
119             This method requires a single argument.
120              
121             =item * B
122              
123             Just like L. This method returns the chopped character.
124              
125             This method does not accept any arguments.
126              
127             =item * B
128              
129             Just like L. This method returns the number of characters
130             removed.
131              
132             This method does not accept any arguments.
133              
134             =item * B
135              
136             Sets the string to the empty string (not the value passed to C).
137              
138             This method does not have a defined return value.
139              
140             This method does not accept any arguments.
141              
142             =item * B
143              
144             Just like L, returns the length of the string.
145              
146             =item * B
147              
148             This acts just like L. When called as a writer, it returns
149             the substring that was replaced, just like the Perl builtin.
150              
151             This method requires at least one argument, and accepts no more than three.
152              
153             =back
154              
155             =head1 SEE ALSO
156              
157             =over 4
158              
159             =item * L
160              
161             =item * L
162              
163             =back
164              
165             =head1 AUTHOR
166              
167             Matthew Phillips
168              
169             =head1 COPYRIGHT AND LICENSE
170              
171             This software is copyright (c) 2014 by Matthew Phillips .
172              
173             This is free software; you can redistribute it and/or modify it under
174             the same terms as the Perl 5 programming language system itself.
175              
176             =cut
177              
178             __END__