SolarBeam/Util.pm

filter: Statementstmt 100% 24 / 24 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 3 / 3 totaltotal 100% 34 / 34 CC 2 SCAR 2.9
CC
2
Cov
100%
escape
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« SolarBeam.pm ↑ Summary
1 package SolarBeam::Util;
2 3 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our @EXPORT_OK = qw(escape escape_chars unescape_chars);
7  
8 3 my $all = quotemeta('+-&|!(){}[]^~:\\"*?');
9 3 my $chars = quotemeta('+-&|!(){}[]^"~*?:\\');
10 3 my $wilds = quotemeta('+-&|!(){}[]^~:\\');
11  
12 13 ▶sub escape {
Subroutine escape: called CC=2 SCAR=6.9
Pod documented
13 13 my $s = shift;
14 13 my $chars;
15  
16 13 ▶ if (ref $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s) { } 4 9 13
17 4 $s = $$s;
18 4 $s =~ s{([$all])}{\\$1}g;
19 }
20 else {
21 9 $s =~ s{([$wilds])}{\\$1}g;
22 }
23  
24 13 return $s;
25 }
26  
27 1 ▶sub escape_chars {
Subroutine escape_chars: called CC=1 SCAR=0.0
Pod documented
28 1 my $s = shift;
29 1 $s =~ s{([$chars])}{\\$1}g;
30 1 $s;
31 }
32  
33 1 ▶sub unescape_chars {
Subroutine unescape_chars: called CC=1 SCAR=0.0
Pod documented
34 1 my $s = shift;
35 1 $s =~ s{\\([$chars])}{$1}g;
36 1 $s;
37 }
38  
39 3 1;
40  
41 =encoding utf8
42  
43 =head1 NAME
44  
45 SolarBeam::Util - Utility functions for SolarBeam
46  
47 =head1 SYNOPSIS
48  
49 use SolarBeam::Util qw(escape escape_chars unescape_chars);
50 say escape_chars "foo?*";
51  
52 =head1 DESCRIPTION
53  
54 L<SolarBeam::Util> contains utility functions for L<SolarBeam>.
55  
56 =head1 FUNCTIONS
57  
58 =head2 escape
59  
60 $str = escape $str;
61  
62 =head2 escape_chars
63  
64 $str = escape_chars $str;
65  
66 The following values must be escaped in a search value:
67  
68 + - & | ! ( ) { } [ ] ^ " ~ * ? : \
69  
70 B<NB:> Values sent to L<SolarBeam::Query/new> are automatically escaped for you.
71  
72 =head2 unescape_chars
73  
74 $str = unescape_chars $str;
75  
76 Unescapes values escaped in L</escape_chars>.
77  
78 =head1 SEE ALSO
79  
80 L<SolarBeam>.
81  
82 =cut
« SolarBeam.pm ↑ Summary