File Coverage

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


line stmt bran cond sub pod time code
1             package SolarBeam::Util;
2 3     3   23957 use Mojo::Base -strict;
  3         4  
  3         19  
3              
4 3     3   310 use Exporter 'import';
  3         4  
  3         796  
5              
6             our @EXPORT_OK = qw(escape escape_chars unescape_chars);
7              
8             my $all = quotemeta('+-&|!(){}[]^~:\\"*?');
9             my $chars = quotemeta('+-&|!(){}[]^"~*?:\\');
10             my $wilds = quotemeta('+-&|!(){}[]^~:\\');
11              
12             sub escape {
13 13     13 1 598 my $s = shift;
14 13         27 my $chars;
15              
16 13 100       35 if (ref $s) {
17 4         6 $s = $$s;
18 4         83 $s =~ s{([$all])}{\\$1}g;
19             }
20             else {
21 9         112 $s =~ s{([$wilds])}{\\$1}g;
22             }
23              
24 13         86 return $s;
25             }
26              
27             sub escape_chars {
28 1     1 1 3 my $s = shift;
29 1         56 $s =~ s{([$chars])}{\\$1}g;
30 1         9 $s;
31             }
32              
33             sub unescape_chars {
34 1     1 1 4 my $s = shift;
35 1         66 $s =~ s{\\([$chars])}{$1}g;
36 1         5 $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