use strict; use warnings; use Cwd; my @filedefs = ( { extension => 'css', preaction => sub { $_[0]->{count} = 0 }, testaction => sub { $_[0]->{count}++ }, postaction => sub { print "$_[0]->{label} !! $_[0]->{count} Instance found\n" }, operations => [ { label => 'Harcoded font size value', test => sub { $_[0] =~ /font-size\s*(:|=)\s*[^%]+$/ }, }, { label => 'Forced Line height value', test => sub { $_[0] =~ /line-height/ }, }, { label => 'position absolute value', test => sub { $_[0] =~ /position\s*(:|=)\s*absolute/ }, }, { label => 'Forced font color', test => sub { $_[0] =~ /^\s*color\s*(:|=)/ }, }, ], }, { extension => 'opf', operations => [ { label => 'language', test => sub { $_[0] =~ /language/ }, testaction => sub { print "$_[1]\n" }, }, { label => 'layout or fixed get', test => sub { $_[0] =~ /(pre-paginated|\Qname="fixed-layout" content="true"\E)/ }, testaction => sub { $_[0]->{done} = 1 }, postaction => sub { print +( $_[0]->{done} ) ? 'Fixed layout tag is found' : 'Fixed layout tag is not found', "\n" }, }, ], } ); my $dirpath = getcwd; for my $filedef ( @filedefs ) { my @filepaths = glob( "$dirpath/*.$filedef->{extension}" ); print "\nno $filedef->{extension} files\n" unless @filepaths; for my $filepath ( @filepaths ) { print "\n>>> $filepath <<<\n"; do { $_->{done} = 0; $_->{preaction} // $filedef->{preaction} // sub { } }->( $_ ) for ( @{$filedef->{operations}} ); open my $filehandle, '<', $filepath or die "could not open '$filepath': $!"; while ( my $line = <$filehandle> ) { # note the below two lines could be handled more efficiently in a testaction, which # returns a true or false value to indicate if last etc, because its unnecessary to check # for every line. my @operations = grep { not $_->{done} } @{$filedef->{operations}}; last unless @operations; chomp $line; do { $_->{test} // sub { 0 } }->( $line ) and do { $_->{testaction} // $filedef->{testaction} // sub { } }->( $_, $line ) for ( @operations ) } close $filehandle; do { $_->{postaction} // $filedef->{postaction} // sub { } }->( $_ ) for ( @{$filedef->{operations}} ); } } END { print "\nPress enter to exit\n"; <>; }