File Coverage

blib/lib/Regexp/RegGrp/Data.pm
Criterion Covered Total %
statement 45 45 100.0
branch 19 20 95.0
condition 16 18 88.8
subroutine 8 8 100.0
pod 0 1 0.0
total 88 92 95.6


line stmt bran cond sub pod time code
1             package Regexp::RegGrp::Data;
2              
3 4     4   18775 use 5.008009;
  4         12  
  4         153  
4 4     4   21 use warnings;
  4         5  
  4         118  
5 4     4   15 use strict;
  4         10  
  4         161  
6 4     4   20 use Carp;
  4         6  
  4         450  
7              
8             our @ACCESSORS = ( 'regexp', 'replacement', 'store', 'restore_pattern' );
9              
10             ##########################################################################################
11              
12             {
13 4     4   26 no strict 'refs';
  4         8  
  4         2003  
14              
15             foreach my $field ( @ACCESSORS ) {
16             next if defined *{'Regexp::RegGrp::Data::' . $field}{CODE};
17              
18             *{'Regexp::RegGrp::Data::' . $field} = sub {
19 428     428   5841 my $self = shift;
20              
21 428         1705 return $self->{'_' . $field};
22             };
23             }
24             }
25              
26             sub new {
27 68     68 0 12361 my ( $class, $in_ref ) = @_;
28 68         96 my $self = {};
29              
30 68         149 bless( $self, $class );
31              
32 68 100       144 unless ( $in_ref->{regexp} ) {
33 2         237 carp( 'Value for key "regexp" must be a scalar or a regexp object!' );
34 2         44 return;
35             }
36              
37 66         96 foreach my $accessor ( @ACCESSORS ) {
38 252 100 100     1528 if ( $accessor eq 'regexp' || $accessor eq 'restore_pattern' ) {
    50 66        
39 126 100 100     441 if (
40             ref( $in_ref->{$accessor} ) and
41             ref( $in_ref->{$accessor} ) ne 'Regexp'
42             ) {
43 4         383 carp( 'Value for key "' . $accessor . '" must be a scalar or a regexp object!' );
44 4         151 return;
45             }
46             }
47             elsif ( $accessor eq 'replacement' || $accessor eq 'store' ) {
48 126 100 100     347 if (
49             ref( $in_ref->{$accessor} ) and
50             ref( $in_ref->{$accessor} ) ne 'CODE'
51             ) {
52 4         421 carp( 'Value for key "' . $accessor . '" must be a scalar or a code reference!' );
53 4         796 return;
54             }
55             }
56             }
57              
58 58 100       127 if ( ref( $in_ref->{modifier} ) ) {
59 2         312 carp( 'Value for key "modifier" must be a scalar!' );
60 2         181 return;
61             }
62              
63 56         109 $self->{_regexp} = $in_ref->{regexp};
64             $self->{_replacement} = defined( $in_ref->{store} ) ? (
65             $in_ref->{restore_pattern} ? $in_ref->{replacement} : sub {
66 9     9   40 return sprintf( "\x01%d\x01", $_[0]->{store_index} );
67             }
68 56 100       150 ) : $in_ref->{replacement};
    100          
69 56         71 $self->{_store} = $in_ref->{store};
70              
71 56 100 100     212 if ( defined( $in_ref->{modifier} ) || ! ref( $in_ref->{regexp} ) ) {
72 25 100       51 my $modifier = defined( $in_ref->{modifier} ) ? $in_ref->{modifier} : 'sm';
73              
74 25         55 $self->{_regexp} =~ s/^\(\?[\^dlupimsx-]+:(.*)\)$/$1/si;
75 25         109 $self->{_regexp} = sprintf( '(?%s:%s)', $modifier, $self->{_regexp} );
76             }
77              
78 56   66     275 my $restore_pattern = $in_ref->{restore_pattern} || qr~\x01(\d+)\x01~;
79 56         214 $self->{_restore_pattern} = qr/$restore_pattern/;
80              
81 56         179 return $self;
82             }
83              
84             1;