File Coverage

blib/lib/Acme/Addslashes.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 30 30 100.0


line stmt bran cond sub pod time code
1             package Acme::Addslashes;
2              
3 2     2   28313 use utf8;
  2         3  
  2         13  
4              
5             # ABSTRACT: Perl twist on the most useful PHP function ever - addslashes
6              
7             =encoding utf-8
8              
9             =head1 NAME
10              
11             Acme::Addslashes - Perl twist on the most useful PHP function ever - addslashes
12              
13             =head1 SYNOPSIS
14              
15             Do you have some text? Have you ever wanted to add some slashes to it? Well now you can!
16              
17             PHP has a totally awesome C function - L.
18              
19             PERL has long been lacking such a function, and at long last here it is. Of
20             course the PERL version is better. Here is a run down of what's better in PERL:
21              
22             =over
23              
24             =item 1 PHP's addslashes can only adds slashes before characters.
25              
26             Thanks to unicode, PERL's version doesn't have this limitation. We add slashes
27             I. Isn't that cool?
28              
29             =item 2 PHP's addslashes only adds slashes to some characters
30              
31             Why not add slashes to all characters? More slashes directly equals safer code.
32             That is scientific fact. There is no real evidence for it, but it is scientific fact.
33              
34             B Now with extra long slashes for even more protection! Thanks ~SKINGTON!
35              
36             =back
37              
38             =head1 USAGE
39              
40             use Acme::Addslashes qw(addslashes);
41              
42             my $unsafe_string = "Robert'); DROP TABLE Students;--";
43            
44             my $totally_safe_string = addslashes($unsafe_string);
45              
46             # $totally_safe_string now contains:
47             # R̸o̸b̸e̸r̸t̸'̸)̸;̸ ̸D̸R̸O̸P̸ ̸T̸A̸B̸L̸E̸ ̸S̸t̸u̸d̸e̸n̸t̸s̸;̸-̸-̸
48              
49             # If that's not enough slashes to be safe, I don't know what is
50              
51             =cut
52              
53 2     2   89 use v5.12;
  2         6  
  2         72  
54 2     2   30 use strict; # lolwut? strict??
  2         2  
  2         76  
55              
56 2     2   63112 use Encode qw(encode);
  2         55055  
  2         293  
57 2     2   20 use feature qw(unicode_strings);
  2         3  
  2         240  
58 2     2   1950 use parent "Exporter";
  2         600  
  2         11  
59              
60             our @EXPORT_OK = qw(addslashes);
61              
62             our $VERSION = '0.1.3';
63              
64             =head1 FUNCTIONS
65              
66             =head2 addslashes
67              
68             my $totally_safe_string = addslashes("Robert'); DROP TABLE Students;--");
69              
70             The only function exported by this module. Will literally add slashes to anything.
71              
72             Letters, numbers, punctuation, whitespace, unicode symbols.
73             You name it, this function can add a slash to it.
74              
75             Will return you a C encoded string containing your original string, but with
76             enough slashes added to make Freddy Krueger jealous.
77              
78             =cut
79              
80             # The addslashes function. It is documented above. -- JAITKEN
81             sub addslashes {
82             # Get the arguments passed to the function using the shift command -- JAITKEN
83 4     4 1 1672 my $unsafe_string = shift;
84              
85             # Split the string into letters - just like explode in PHP. Or maybe str_split
86             # I can't remember which one is which -- JAITKEN
87 4         29 my @unsafe_array = split('', $unsafe_string);
88            
89             # Add slashes to every character thanks to unicode.
90             # This is complex magic -- JAITKEN
91             # I think these slashes could be longer -- SKINGTON
92             # You forgot the last slash -- JAITKEN
93 4         18 my $safe_string = join("\N{U+0338}", @unsafe_array) . "\N{U+0338}";
94              
95             # Return the safe string using the return function of PERL -- JAITKEN
96 4         16 return encode("utf8", $safe_string);
97             }
98              
99             # The end of the module. -- JAITKEN
100             1;
101              
102              
103             =head1 AUTHOR
104              
105             James Aitken
106              
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             This software is copyright (c) 2012 by James Aitken.
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut