mohad has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

Need your help with a few modifications in an existing code. I got this code with the help of this forum and would need help in editing this a little bit. This code reads the .opf and .css files in the directory and throws the result if the conditions are met.

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} In +stance 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="f +ixed-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 '$f +ilepath': $!"; while ( my $line = <$filehandle> ) { # note the below two lines could be handled more efficient +ly in a testaction, which # returns a true or false value to indicate if last etc, b +ecause its unnecessary to check # for every line. my @operations = grep { not $_->{done} } @{$filedef->{oper +ations}}; last unless @operations; chomp $line; do { $_->{test} // sub { 0 } }->( $line ) and do { $_->{te +staction} // $filedef->{testaction} // sub { } }->( $_, $line ) for ( + @operations ) } close $filehandle; do { $_->{postaction} // $filedef->{postaction} // sub { } }-> +( $_ ) for ( @{$filedef->{operations}} ); } } END { print "\nPress enter to exit\n"; <>; }

I want to make the below modifications: 1. Currently, the css and opf file should be place in the directory for the search to happen. If these files are inside the subdirectory then it wont detect these files. I want to modify in such a way that the program can search for files inside the sub directoriesas well. 2. It should read".html" files and should look out for the usage of "svg" tags in it. If found it should dispay that "svg tag has been found in xyz.html" 3. It should read opf for the text "cover". if its found then display cover is found.

Replies are listed 'Best First'.
Re: Searching for specific text
by Discipulus (Canon) on Oct 20, 2015 at 08:27 UTC
      Thanks for the suggestion. can you please create an example in the above code upon which I'll be able to modify it as per my need? I'm just learning perl and would be of great help if i could learn from that.

        Providing "an example" might be of some teaching value; it almost certainly will allow you to turn it in to /boss|teacher|whomever/ as evidence of your learning. So my short answer is "Nope!"

        You'll actually learn a lot more by studying the references and the tutorials available here than by asking a gimmé like your OP.


        Spirit of the Monastery

        Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
        1. code
        2. verbatim error and/or warning messages
        3. a coherent explanation of what "doesn't work actually means.