File Coverage

blib/lib/String/PerlQuote.pm
Criterion Covered Total %
statement 23 26 88.4
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 33 36 91.6


line stmt bran cond sub pod time code
1             package String::PerlQuote;
2              
3             our $DATE = '2014-12-10'; # DATE
4             our $VERSION = '0.01'; # VERSION
5              
6 1     1   51897 use 5.010001;
  1         4  
  1         47  
7 1     1   7 use strict;
  1         2  
  1         46  
8 1     1   6 use warnings;
  1         1  
  1         42  
9              
10 1     1   5 use Exporter;
  1         2  
  1         724  
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             single_quote
14             double_quote
15             );
16              
17             # BEGIN COPY PASTE FROM Data::Dump
18             my %esc = (
19             "\a" => "\\a",
20             "\b" => "\\b",
21             "\t" => "\\t",
22             "\n" => "\\n",
23             "\f" => "\\f",
24             "\r" => "\\r",
25             "\e" => "\\e",
26             );
27              
28             # put a string value in double quotes
29             sub double_quote {
30 4     4 1 1103 local($_) = $_[0];
31             # If there are many '"' we might want to use qq() instead
32 4         25 s/([\\\"\@\$])/\\$1/g;
33 4 100       33 return qq("$_") unless /[^\040-\176]/; # fast exit
34              
35 1         9 s/([\a\b\t\n\f\r\e])/$esc{$1}/g;
36              
37             # no need for 3 digits in escape for these
38 1         3 s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
  0         0  
39              
40 1         3 s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
  0         0  
41 1         4 s/([^\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
  0         0  
42              
43 1         8 return qq("$_");
44             }
45             # END COPY PASTE FROM Data::Dump
46              
47             sub single_quote {
48 2     2 1 2403 local($_) = $_[0];
49 2         16 s/([\\'])/\\$1/g;
50 2         12 return qq('$_');
51             }
52             1;
53             # ABSTRACT: Quote a string like Perl does
54              
55             __END__