Skip to content

Commit

Permalink
unescape results from PPI::Token::QuoteLike::Words::literal
Browse files Browse the repository at this point in the history
fixes #124
fixes #127
  • Loading branch information
moregan authored and wchristian committed Nov 21, 2014
1 parent b034573 commit fb8f80e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
23 changes: 15 additions & 8 deletions lib/PPI/Token/QuoteLike/Words.pm
Expand Up @@ -42,20 +42,27 @@ BEGIN {
=head2 literal
Returns the words contained. Note that this method does not check the
Returns the words contained as a list. Note that this method does not check the
context that the token is in; it always returns the list and not merely
the last element if the token is in scalar context.
=cut

sub literal {
my $self = shift;
my $section = $self->{sections}->[0];
return split ' ', substr(
$self->{content},
$section->{position},
$section->{size},
);
my ( $self ) = @_;

my $content = $self->_section_content(0);
return if !defined $content;

# Undo backslash escaping of '\', the left delimiter,
# and the right delimiter. The right delimiter will
# only exist with paired delimiters: qw() qw[] qw<> qw{}.
my ( $left, $right ) = ( $self->_delimiters, '', '' );
$content =~ s/\\([\Q$left$right\\\E])/$1/g;

my @words = split ' ', $content;

return @words;
}

1;
Expand Down
27 changes: 26 additions & 1 deletion t/ppi_token_quotelike_words.t
Expand Up @@ -3,7 +3,7 @@
# Unit testing for PPI::Token::QuoteLike::Words

use t::lib::PPI::Test::pragmas;
use Test::More tests => 1381;
use Test::More tests => 1941;
use Test::Deep;

use PPI;
Expand Down Expand Up @@ -52,14 +52,39 @@ LITERAL: {

my $bs = '\\'; # a single backslash character

# escaped opening and closing
permute_test ["$bs)"], '(', ')', [')'];
permute_test ["$bs("], '(', ')', ['('];
permute_test ["$bs}"], '{', '}', ['}'];
permute_test ["${bs}{"], '{', '}', ['{'];
permute_test ["$bs]"], '[', ']', [']'];
permute_test ["${bs}["], '[', ']', ['['];
permute_test ["$bs<"], '<', '>', ['<'];
permute_test ["$bs>"], '<', '>', ['>'];
permute_test ["$bs/"], '/', '/', ['/'];
permute_test ["$bs'"], "'", "'", ["'"];
permute_test [$bs.'"'], '"', '"', ['"'];

# alphanum delims have to be separated from qw
assemble_and_run " ", ['a', "${bs}1"], '1', " ", " ", '1', ['a', '1'];
assemble_and_run " ", ["${bs}a"], 'a', " ", " ", 'a', ['a'];
assemble_and_run "\n", ["${bs}a"], 'a', "\n", "\n", 'a', ['a'];

# '#' delims cannot be separated from qw
assemble_and_run '', ['a'], '#', '', ' ', '#', ['a'];
assemble_and_run '', ['a'], '#', ' ', ' ', '#', ['a'];
assemble_and_run '', ["$bs#"], '#', '', ' ', '#', ['#'];
assemble_and_run '', ["$bs#"], '#', ' ', ' ', '#', ['#'];
assemble_and_run '', ["$bs#"], '#', "\n", "\n", '#', ['#'];

# a single backslash represents itself
assemble_and_run '', [$bs], '(', ' ', ' ', ')', [$bs];
assemble_and_run '', [$bs], '(', "\n", ' ', ')', [$bs];

# a double backslash represents itself
assemble_and_run '', ["$bs$bs"], '(', ' ', ' ', ')', [$bs];
assemble_and_run '', ["$bs$bs"], '(', "\n", ' ', ')', [$bs];

# even backslash can be a delimiter, in when it is, backslashes
# can't be embedded or escaped.
assemble_and_run '', [], $bs, ' ', ' ', $bs, [];
Expand Down

0 comments on commit fb8f80e

Please sign in to comment.