Acme-Addslashes

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


## addslashes

    my $totally_safe_string = addslashes("Robert'); DROP TABLE Students;--");

The only function exported by this module. Will literally add slashes to anything.

Letters, numbers, punctuation, whitespace, unicode symbols.
You name it, this function can add a slash to it.

Will return you a `utf8` encoded string containing your original string, but with
enough slashes added to make Freddy Krueger jealous.

# AUTHOR

James Aitken <jaitken@cpan.org>



# COPYRIGHT AND LICENSE

lib/Acme/Addslashes.pm  view on Meta::CPAN

package Acme::Addslashes;

use utf8;

# ABSTRACT: Perl twist on the most useful PHP function ever - addslashes

=encoding utf-8

=head1 NAME

Acme::Addslashes - Perl twist on the most useful PHP function ever - addslashes

=head1 SYNOPSIS

lib/Acme/Addslashes.pm  view on Meta::CPAN


=head2 addslashes

    my $totally_safe_string = addslashes("Robert'); DROP TABLE Students;--");

The only function exported by this module. Will literally add slashes to anything.

Letters, numbers, punctuation, whitespace, unicode symbols.
You name it, this function can add a slash to it.

Will return you a C<utf8> encoded string containing your original string, but with
enough slashes added to make Freddy Krueger jealous.

=cut

# The addslashes function. It is documented above. -- JAITKEN
sub addslashes {
    # Get the arguments passed to the function using the shift command -- JAITKEN
    my $unsafe_string = shift;

    # Split the string into letters - just like explode in PHP. Or maybe str_split
    # I can't remember which one is which -- JAITKEN
    my @unsafe_array = split('', $unsafe_string);
    
    # Add slashes to every character thanks to unicode.
    # This is complex magic -- JAITKEN
    # I think these slashes could be longer -- SKINGTON
    # You forgot the last slash -- JAITKEN
    my $safe_string = join("\N{U+0338}", @unsafe_array) . "\N{U+0338}";

    # Return the safe string using the return function of PERL -- JAITKEN
    return encode("utf8", $safe_string);
}

# The end of the module. -- JAITKEN
1;


=head1 AUTHOR

James Aitken <jaitken@cpan.org>

t/001_basics.t  view on Meta::CPAN

use Test::More tests => 1;

use v5.12;
use strict;
use warnings;
use utf8;
use feature qw(unicode_strings);

use_ok 'Acme::Addslashes';

t/002_features.t  view on Meta::CPAN

use Test::More tests => 4;

use v5.12;
use strict;
use warnings;
use utf8;
use feature qw(unicode_strings);

use Acme::Addslashes qw(addslashes);
use Encode qw(decode);

ok(decode("utf8", addslashes q{Moose}) eq q{M̸o̸o̸s̸e̸}, 'Latin text slashed up');

ok(decode("utf8", addslashes qq{☃}) eq qq{☃̸}, 'Slashed up snowman');

ok(decode("utf8", addslashes qq{\x{1F4A9}}) eq qq{\x{1F4A9}\x{338}}, 'Slashed up Pile of Poo');

ok(decode("utf8", addslashes q{Robert'); DROP TABLE Students;--}) eq q{R̸o̸b̸e̸r̸t̸'̸)̸;̸ ̸D̸R̸O̸P̸ ̸T̸A̸B̸L̸E̸ ̸S̸t̸u̸d̸e̸n̸t̸s̸;̸-̸-̸}, 'Slashed up little Bobby Tables');



( run in 0.484 second using v1.01-cache-2.11-cpan-49f99fa48dc )