File: interpolated

package info (click to toggle)
libppix-regexp-perl 0.090-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,524 kB
  • sloc: perl: 8,022; makefile: 8
file content (130 lines) | stat: -rwxr-xr-x 2,863 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/local/bin/perl

use 5.006;

use strict;
use warnings;

use File::Find;
use File::Spec;
use Getopt::Long;
use PPI;
use PPIx::Regexp;

my %opt;

GetOptions( \%opt, qw{ once! } ) or die;

find( \&interpolated, @ARGV ? @ARGV : ( File::Spec->curdir() ) );

sub interpolated {
    -d $_ and return prune();
    is_perl( $_ ) or return;
    my $named;
    my $doc = PPI::Document->new( $_ );
    foreach my $class ( qw{ PPI::Token::Regexp::Match
	PPI::Token::Regexp::Substitute
	PPI::Token::QuoteLike::Regexp
	} ) {
	foreach my $elem ( @{ $doc->find( $class ) || [] } ) {
	    my $re = PPIx::Regexp->new( $elem );
	    $re->regular_expression()->find_first(
		'PPIx::Regexp::Token::Interpolation' )
		or next;
	    $opt{once} and $re->modifier()->asserts( 'o' ) and next;
	    $named++ or print $File::Find::name, "\n";
	    print '    ', $re->content(), "\n";
	}
    }
}

sub is_perl {
    my ( $fn ) = @_;
    -T $fn or return;
    $fn =~ m/ [.] pm \z /smx  and return 1;
    $fn =~ m/ [.] pl \z /smxi and return 1;
    open ( my $fh, '<', $fn ) or return;
    local $_ = <$fh>;
    close $fh;
    defined $_ or return;
    m/ \A [#] ! .*? perl /smx and return 1;
    m/ \A [#] ! /smx and return;
    $fn =~ m/ [.] t  \z /smx  and return 1;
    return;
}

{

    my %pruned;
    
    BEGIN {
	%pruned = map { $_ => 1 } qw{ blib };
    }

    sub prune {
	'.' eq $_ and return;
	$pruned{$_}
	    or m/ \A [.] /smx
	    or return;
	$File::Find::prune = 1;
	return;
    }
}


__END__

=head1 TITLE

interpolated - Find all interpolated regular expressions

=head1 SYNOPSIS

 interpolated
 interpolated lib
 interpolated -once

=head1 OPTIONS

=over

=item -once

If this option is asserted, only interpolated regular expressions
B<without> the C</o> qualifier are found.

=back

=head1 DETAILS

This script searches files or directories for interpolated regular
expressions. These are considered to be matches or C<qr{ }> expressions
containing interpolations, or substitutions containing interpolations in
their regular expressions. Interpolation in the replacement portion of
the substitution are not considered.

The names of any files containing such regular expressions are listed,
along with the statements themselves.

If C<-once> is asserted, only regular expressions without the C</o>
modifier are listed.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010-2023, 2025 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.

=cut

# ex: set textwidth=72 :