File Coverage

blib/lib/SolarBeam/Util.pm
Criterion Covered Total %
statement 13 19 68.4
branch 2 2 100.0
condition n/a
subroutine 3 5 60.0
pod 3 3 100.0
total 21 29 72.4


line stmt bran cond sub pod time code
1             package SolarBeam::Util;
2 2     2   9 use Mojo::Base -strict;
  2         4  
  2         16  
3              
4 2     2   251 use Exporter 'import';
  2         4  
  2         588  
5              
6             our @EXPORT_OK = qw(escape escape_chars unescape_chars);
7              
8             my $escape_all = quotemeta('+-&|!(){}[]^~:\\"*?');
9             my $escape_chars = quotemeta('+-&|!(){}[]^"~*?:\\');
10             my $escape_wilds = quotemeta('+-&|!(){}[]^~:\\');
11              
12             sub escape {
13 11     11 1 618 my $s = shift;
14 11         10 my $chars;
15              
16 11 100       25 if (ref $s) {
17 3         5 $s = $$s;
18 3         49 $s =~ s{([$escape_all])}{\\$1}g;
19             }
20             else {
21 8         55 $s =~ s{([$escape_wilds])}{\\$1}g;
22             }
23              
24 11         47 return $s;
25             }
26              
27             sub escape_chars {
28 0     0 1   my $s = shift;
29 0           $s =~ s{([$escape_chars])}{\\$1}g;
30 0           $s;
31             }
32              
33             sub unescape_chars {
34 0     0 1   my $s = shift;
35 0           $s =~ s{\\([$escape_chars])}{$1}g;
36 0           $s;
37             }
38              
39             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 contains utility functions for L.
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 Values sent to L are automatically escaped for you.
71              
72             =head2 unescape_chars
73              
74             $str = unescape_chars $str;
75              
76             Unescapes values escaped in L.
77              
78             =head1 SEE ALSO
79              
80             L.
81              
82             =cut