File Coverage

blib/lib/PPIx/Regexp/Support.pm
Criterion Covered Total %
statement 29 32 90.6
branch 11 18 61.1
condition n/a
subroutine 9 9 100.0
pod 4 4 100.0
total 53 63 84.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             PPIx::Regexp::Support - Basis for the PPIx::Regexp support classes
4              
5             =head1 SYNOPSIS
6              
7             use PPIx::Regexp::Dumper;
8             PPIx::Regexp::Dumper->new( 'qr{foo}smx' )
9             ->print();
10              
11             =head1 INHERITANCE
12              
13             C<PPIx::Regexp::Support> is not descended from any other class.
14              
15             C<PPIx::Regexp::Support> is the parent of
16             L<PPIx::Regexp::Dumper|PPIx::Regexp::Dumper>,
17             L<PPIx::Regexp::Lexer|PPIx::Regexp::Lexer> and
18             L<PPIx::Regexp::Tokenizer|PPIx::Regexp::Tokenizer>.
19              
20             =head1 DESCRIPTION
21              
22             This abstract class provides methods for the C<PPIx::Regexp> support
23             classes.
24              
25             =head1 METHODS
26              
27             This class provides the following public methods. Methods not documented
28             here are private, and unsupported in the sense that the author reserves
29             the right to change or remove them without notice.
30              
31             =cut
32              
33             package PPIx::Regexp::Support;
34              
35 9     9   53 use strict;
  9         14  
  9         226  
36 9     9   29 use warnings;
  9         9  
  9         368  
37              
38 9     9   32 use PPIx::Regexp::Constant qw{ @CARP_NOT };
  9         11  
  9         655  
39 9     9   37 use PPIx::Regexp::Util qw{ __instance };
  9         11  
  9         2961  
40              
41             our $VERSION = '0.091';
42              
43             =head2 close_bracket
44              
45             This method takes as its argument a character. If this character is an
46             open bracket the corresponding close bracket is returned. Otherwise
47             C<undef> is returned. Only the ASCII bracket characters are considered
48             brackets: (), {}, [], and <>.
49              
50             =cut
51              
52             {
53             my %bracket = (
54             '(' => ')',
55             '{' => '}',
56             '<' => '>',
57             '[' => ']',
58             );
59              
60             sub close_bracket {
61 705     705 1 1255 my ( undef, $char ) = @_; # Invocant unused
62 705 50       1223 defined $char or return;
63 705 100       1521 __instance( $char, 'PPIx::Regexp::Element' )
64             and $char = $char->content();
65 705         2796 return $bracket{$char};
66             }
67              
68             }
69              
70             =head2 decode
71              
72             This method wraps the Encode::decode subroutine. If the object specifies
73             no encoding or encode_available() returns false, this method simply
74             returns its input string.
75              
76             =cut
77              
78             sub decode {
79 740     740 1 1376 my ( $self, $data ) = @_;
80 740 100       2657 defined $self->{encoding} or return $data;
81 1 50       4 encode_available() or return $data;
82 1         11 return Encode::decode( $self->{encoding}, $data );
83             }
84              
85             =head2 encode
86              
87             This method wraps the Encode::encode subroutine. If the object specifies
88             no encoding or encode_available() returns false, this method simply
89             returns its input string.
90              
91             =cut
92              
93             sub encode {
94 51     51 1 62 my ( $self, $data ) = @_;
95 51 50       126 defined $self->{encoding} or return $data;
96 0 0       0 encode_available() or return $data;
97 0         0 return Encode::encode( $self->{encoding}, $data );
98             }
99              
100             =head2 encode_available
101              
102             This method returns true if the Encode module is available, and false
103             otherwise. If it returns true, the Encode module has actually been
104             loaded.
105              
106             =cut
107              
108             {
109              
110             my $encode_available;
111              
112             sub encode_available {
113 1 50   1 1 3 defined $encode_available and return $encode_available;
114 1 50       3 return ( $encode_available = eval {
115 1         7 require Encode;
116 1         5 1;
117             } ? 1 : 0
118             );
119             }
120              
121             }
122              
123             # This method is to be used only by the PPIx-Regexp package. It returns
124             # the first of its arguments which is defined. It will go away when
125             # (or if!) these modules get 'use 5.010;' at the top.
126              
127             sub __defined_or {
128 742     742   2693 my ( undef, @args ) = @_; # Invocant unused
129 742         1452 foreach my $arg ( @args ) {
130 2226 100       13170 defined $arg and return $arg;
131             }
132 0           return;
133             }
134              
135             1;
136              
137             __END__
138              
139             =head1 SUPPORT
140              
141             Support is by the author. Please file bug reports at
142             L<https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
143             L<https://github.com/trwyant/perl-PPIx-Regexp/issues>, or in
144             electronic mail to the author.
145              
146             =head1 AUTHOR
147              
148             Thomas R. Wyant, III F<wyant at cpan dot org>
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             Copyright (C) 2009-2023, 2025 by Thomas R. Wyant, III
153              
154             This program is free software; you can redistribute it and/or modify it
155             under the same terms as Perl 5.10.0. For more details, see the full text
156             of the licenses in the directory LICENSES.
157              
158             This program is distributed in the hope that it will be useful, but
159             without any warranty; without even the implied warranty of
160             merchantability or fitness for a particular purpose.
161              
162             =cut
163              
164             # ex: set textwidth=72 :