File Coverage

blib/lib/Syntax/Feature/Ql.pm
Criterion Covered Total %
statement 62 62 100.0
branch n/a
condition n/a
subroutine 17 17 100.0
pod n/a
total 79 79 100.0


line stmt bran cond sub pod time code
1 1     1   75886 use strictures 1;
  1         8  
  1         27  
2              
3             # ABSTRACT: Turns a quoted string into a single line
4              
5             package Syntax::Feature::Ql;
6             {
7             $Syntax::Feature::Ql::VERSION = '0.001000';
8             }
9             BEGIN {
10 1     1   106 $Syntax::Feature::Ql::AUTHORITY = 'cpan:PHAYLON';
11             }
12 1     1   721 use Devel::Declare 0.006007 ();
  1         5645  
  1         27  
13 1     1   7 use B::Hooks::EndOfScope 0.09;
  1         16  
  1         6  
14 1     1   86 use Sub::Install 0.925 qw( install_sub );
  1         16  
  1         9  
15              
16 1     1   953 use aliased 'Devel::Declare::Context::Simple', 'Context';
  1         742  
  1         6  
17              
18 1     1   3123 use syntax qw( simple/v2 );
  1         3  
  1         11  
19 1     1   46948 use namespace::clean;
  1         2  
  1         5  
20              
21             my @NewOps = qw( ql qql );
22             my %QuoteOp = (ql => 'q', qql => 'qq');
23              
24 1     1   415 method install ($class: %args) {
  1     1   4  
  1         5  
  1         1  
25 1         2 my $target = $args{into};
26 2         2 Devel::Declare->setup_for($target => {
27             map {
28 1         2 my $name = $_;
29             ($name => {
30             const => sub {
31 17     17   952 my $ctx = Context->new;
32 17         108 $ctx->init(@_);
33 17         139 return $class->_transform($name, $ctx);
34             },
35 2         15 });
36             } @NewOps,
37             });
38 1         26 for my $name (@NewOps) {
39 2         64 install_sub {
40             into => $target,
41             as => $name,
42             code => $class->_run_callback,
43             };
44             }
45             on_scope_end {
46 1     1   97 namespace::clean->clean_subroutines($target, @NewOps);
47 1         43 };
48 1         14 return 1;
49             }
50              
51 1     1   473 method _run_callback {
  2     2   2  
  2         3  
52             return sub ($) {
53 17     17   2412 my $string = shift;
54 17         111 $string =~ s{(?:^\s+|\s+$)}{}gsm;
55 17         142 return join ' ', split m{\s*\n\s*}, $string;
56 2         13 };
57             }
58              
59 1     1   423 method _transform ($class: $name, $ctx) {
  17     17   20  
  17         22  
  17         17  
60 17         47 $ctx->skip_declarator;
61 17         321 my $length = Devel::Declare::toke_scan_str($ctx->offset);
62 17         111 my $string = Devel::Declare::get_lex_stuff;
63 17         27 Devel::Declare::clear_lex_stuff;
64 17         39 my $linestr = $ctx->get_linestr;
65 17         99 my $quoted = substr $linestr, $ctx->offset, $length;
66 17         60 my $spaced = '';
67 17         44 $quoted =~ m{^(\s*)}sm;
68 17         29 $spaced = $1;
69 17         61 my $new = sprintf '(%s)', join '',
70             $QuoteOp{$name},
71             $spaced,
72             substr($quoted, length($spaced), 1),
73             $string,
74             substr($quoted, -1, 1);
75 17         43 substr($linestr, $ctx->offset, $length) = $new;
76 17         88 $ctx->set_linestr($linestr);
77 17         223 return 1;
78             }
79              
80             1;
81              
82              
83             __END__