Text.pm

filter: Statementstmt 36.8% 21 / 57 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 46.6% 7 / 15 Podpod n/a totaltotal 30.4% 28 / 92 CC 10 SCAR 22.2
CC
10
Cov
30%
bulletize
30.0
wrap
6.0
unwrap
6.0
CRAP 9.2
↑ Summary lorem.pl »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package autobox::Text;
4  
5 # ABSTRACT: autoboxed methonds for text
6  
7 3 ▶use base qw/autobox/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
10 3 my $class = shift;
11 # $class->SUPER::import(DEFAULT => 'autobox::Core::', @_);
12 3 $class->SUPER::import(
13 ARRAY => 'autobox::Text::Subs',
14 SCALAR => 'autobox::Text::Subs',
15 );
16 }
17  
18 package autobox::Text::Subs;
19  
20 3 ▶no warnings qw/redefine/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Encode qw(find_encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub wrap {
Subroutine wrap: not called CC=2 SCAR=17.9
26 0 my $text = shift;
27 0 ▶ $Text::Wrap::columns = shift || 80;
Condition0%0/2
shift() || 80
l!l
00
MC/DC0%0/1
shift() || 80
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
28 0 return Text::Wrap::wrap('', '', $text);
29 }
30  
31 0 ▶sub unwrap {
Subroutine unwrap: not called CC=2 SCAR=17.9
32 0 my @text = split /\n\n/, shift;
33 0 s/\n/ /g for (@text);
34 0 return (join "\n\n", @text) =~ s/^\s*|\s*$//rg;
35 }
36  
37 0 ▶sub bulletize {
Subroutine bulletize: not called CC=5 SCAR=34.0
38 0 my @text = split /\n/, shift;
39 0 ▶ my $bullet = shift || '-';
Condition0%0/2
shift() || '-'
l!l
00
MC/DC0%0/1
shift() || '-'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
40  
41 0 ▶ if ($bullet =~ /\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bullet =~ /\d/) { } 0 0 0
42 0 ▶ my $fmt = shift || "%i. %s";
Condition0%0/2
shift() || '%i. %s'
l!l
00
MC/DC0%0/1
shift() || '%i. %s'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
43 0 return (join "\n", map { sprintf $fmt, $bullet++, $_ } @text) =~ s/^\s*|\s*$//rg
44 } else {
45 0 ▶ my $fmt = shift || "%s %s";
Condition0%0/2
shift() || '%s %s'
l!l
00
MC/DC0%0/1
shift() || '%s %s'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
46 0 return (join "\n", map { sprintf $fmt, $bullet, $_ } @text) =~ s/^\s*|\s*$//rg;
47 }
48 }
49  
50 0 ▶sub unbulletize {
Subroutine unbulletize: not called CC=2 SCAR=17.9
51 0 my @text = split /\n/, shift;
52 0 for (@text) {
53 0 s/^[\*\-\_]\s//;
54 0 s/\d+\.\s//;
55 }
56 0 return (join "\n", @text) =~ s/^\s*|\s*$//rg;
57 }
58  
59 0 ▶sub trim {
Subroutine trim: not called CC=1 SCAR=6.9
60 0 my $str = shift;
61 0 $str =~ s/^\s+//;
62 0 $str =~ s/\s+$//;
63 0 return $str;
64 }
65  
66 0 ▶sub tidy {
Subroutine tidy: not called CC=1 SCAR=6.9
67 0 my $str = shift;
68 0 $str =~ s/^\s+//;
69 0 $str =~ s/\s+$//;
70 0 $str =~ s/[\t ]+/ /g;
71 0 $str =~ s/\n{3,}/\n\n/g;
72 0 return $str;
73 }
74  
75 0 ▶sub encode {
Subroutine encode: not called CC=2 SCAR=17.9
76 0 my $str = shift;
77 0 ▶ my $encoding = shift || "UTF-8";
Condition0%0/2
shift() || 'UTF-8'
l!l
00
MC/DC0%0/1
shift() || 'UTF-8'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
78 0 return Encode::decode $encoding, $str;
79 }
80  
81 0 ▶sub decode {
Subroutine decode: not called CC=2 SCAR=17.9
82 0 my $str = shift;
83 0 ▶ my $encoding = shift || "UTF-8";
Condition0%0/2
shift() || 'UTF-8'
l!l
00
MC/DC0%0/1
shift() || 'UTF-8'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
84 0 return Encode::decode $encoding, $str;
85 }
86  
87 # markdown
88 # lorem
89 # truncate String::Truncate
90  
91 3 1;
92  
93 =head1 NAME
94  
95 autobox::Text - turns baubles into trinkets
96  
97 =head1 SYNOPSIS
98  
99 use autobox::Text;
100  
101 my $text = "Some text";
102 my $wrapped = $text->wrap(60);
103 my $bullets = $text->bulletize('*');
104  
105 =head1 DESCRIPTION
106  
107 This module extends scalars and arrays with a set of
108 text-processing methods using L<autobox>.
109  
110 =head1 METHODS
111  
112 =head2 SCALAR->wrap([$columns])
113  
114 Wraps a long string into multiple lines. Defaults to 80 columns if
115 no column width is provided. Uses L<Text::Wrap>.
116  
117 =head2 SCALAR->unwrap
118  
119 Removes wrapping by joining each paragraph into a single line,
120 preserving paragraph breaks.
121  
122 =head2 SCALAR->bulletize([$bullet, $format])
123  
124 Adds bullet characters or numeric markers to each line. By default,
125 uses a hyphen C<->. If C<$bullet> contains digits, numeric bulleting
126 is applied using a format string (default: C<"%i. %s">). For non-numeric
127 bullets, format string defaults to C<"%s %s">.
128  
129 =head2 SCALAR->unbulletize
130  
131 Removes leading bullet characters (C<*>, C<->, C<_>) or numeric markers
132 from each line of a bulletized list. Does not impact numbered lists
133  
134 =head2 SCALAR->trim
135  
136 Removes leading and trailing whitespace from a string.
137  
138 =head2 SCALAR->tidy
139  
140 Performs multiple cleanup operations:
141  
142 =over
143  
144 =item * Strips leading and trailing whitespace.
145  
146 =item * Collapses multiple tabs and spaces into one
147  
148 =item * Collapses three or more consecutive newlines into two.
149  
150 =back
151  
152 =head1 SEE ALSO
153  
154 L<autobox>, L<Text::Wrap>, L<String::Truncate>
155  
156 =head1 AUTHOR
157  
158 Simone Cesano
159  
160 =head1 COPYRIGHT AND LICENSE
161  
162 This software is copyright (c) 2025 by Simone Cesano.
163  
164 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
↑ Summary lorem.pl »