| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 9 | ▶ | use PPIx::Regexp::Constant qw{ @CARP_NOT }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 9 | ▶ | use PPIx::Regexp::Util qw{ __instance }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 9 | our $VERSION = '0.092'; | |||||||||
| 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 | 9 | my %bracket = ( | |||||||||
| 54 | '(' => ')', | ||||||||||
| 55 | '{' => '}', | ||||||||||
| 56 | '<' => '>', | ||||||||||
| 57 | '[' => ']', | ||||||||||
| 58 | ); | ||||||||||
| 59 | |||||||||||
| 60 | 705 | ▶ | sub close_bracket { | ||||||||
|
Subroutine
close_bracket: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 61 | 705 | my ( undef, $char ) = @_; # Invocant unused | |||||||||
| 62 | 705 | ▶ | defined $char or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 705 | ▶ | __instance( $char, 'PPIx::Regexp::Element' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | and $char = $char->content(); | ||||||||||
| 65 | 705 | 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 | 9 | ▶ | sub decode { | ||||||||
|
Subroutine
decode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 79 | 740 | my ( $self, $data ) = @_; | |||||||||
| 80 | 740 | ▶ | defined $self->{encoding} or return $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 1 | ▶ | encode_available() or return $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 1 | 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 | 51 | ▶ | sub encode { | ||||||||
|
Subroutine
encode: called CC=3 SCAR=16.5
Pod
documented
| |||||||||||
| 94 | 51 | my ( $self, $data ) = @_; | |||||||||
| 95 | 51 | ▶ | defined $self->{encoding} or return $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | ▶ | encode_available() or return $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 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 | 9 | my $encode_available; | |||||||||
| 111 | |||||||||||
| 112 | 1 | ▶ | sub encode_available { | ||||||||
|
Subroutine
encode_available: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||
| 113 | 1 | ▶ | defined $encode_available and return $encode_available; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 1 | ▶ | return ( $encode_available = eval { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 1 | require Encode; | |||||||||
| 116 | 1 | 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 | 9 | ▶ | sub __defined_or { | ||||||||
|
Subroutine
__defined_or: called CC=3 SCAR=11.1
| |||||||||||
| 128 | 742 | my ( undef, @args ) = @_; # Invocant unused | |||||||||
| 129 | 742 | foreach my $arg ( @args ) { | |||||||||
| 130 | 2226 | ▶ | defined $arg and return $arg; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | } | ||||||||||
| 132 | 0 | return; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 9 | 1; | |||||||||
| 136 | |||||||||||
| 137 | __END__ | ||||||||||