File Coverage

blib/lib/Data/Sah/Filter/perl/Str/remove_comment.pm
Criterion Covered Total %
statement 8 16 50.0
branch 0 4 0.0
condition 0 4 0.0
subroutine 3 5 60.0
pod 0 2 0.0
total 11 31 35.4


line stmt bran cond sub pod time code
1             package Data::Sah::Filter::perl::Str::remove_comment;
2              
3 1     1   420271 use 5.010001;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         24  
5 1     1   4 use warnings;
  1         1  
  1         359  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2024-07-17'; # DATE
9             our $DIST = 'Data-Sah-Filter'; # DIST
10             our $VERSION = '0.025'; # VERSION
11              
12             sub meta {
13             +{
14 0     0 0   v => 1,
15             summary => 'Remove comment',
16             args => {
17             style => {
18             schema => ['str*', in=>['shell', 'cpp']],
19             default => 'shell',
20             },
21             },
22             examples => [
23             {value=>"foo"},
24             {value=>"foo # comment", filtered_value=>"foo"},
25             {value=>"foo # comment", filter_args=>{style=>"cpp"}},
26             {value=>"foo // comment", filter_args=>{style=>"cpp"}, filtered_value=>"foo"},
27             ],
28             };
29             }
30              
31             sub filter {
32 0     0 0   my %fargs = @_;
33              
34 0           my $dt = $fargs{data_term};
35 0   0       my $gen_args = $fargs{args} // {};
36 0   0       $gen_args->{style} //= 'shell';
37              
38 0           my $res = {};
39             $res->{expr_filter} = join(
40             "",
41             "do { ", (
42             "my \$tmp = $dt; ",
43             ($gen_args->{style} eq 'shell' ? "\$tmp =~ s/\\s*#.*//g; " :
44 0 0         $gen_args->{style} eq 'cpp' ? "\$tmp =~ s!\\s*//.*!!g; " :
    0          
45             die "Unknown style '$gen_args->{style}'"),
46             "\$tmp ",
47             ), "}",
48             );
49              
50 0           $res;
51             }
52              
53             1;
54             # ABSTRACT: Remove comment
55              
56             __END__