CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [mohan] Searching for a string: Edit Log



Zhris
Enthusiast

Feb 2, 2015, 6:12 PM


Views: 22636
Re: [mohan] Searching for a string

Hi,

After I last posted, I modified the code slightly to support short circuiting ( via operation attribute "done" ), chomping the line ( in case printing last line which doesn't have newline ) and have compacted a few of the blocks into a single line ( personal preference ). I will use this version instead, although its a little less readable. Fundamentally, in order to support your requirement adjustments, I modified the @filedefs array so that each of the opf operations have their own set of action coderefs that do the desirable thing accordingly.


Code
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',
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} ) ? 'get is found' : 'no get is 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";
<>;
}



Quote
Whoooooohhh !! That's one hell of a code there !!]


It is indeed one hell of a code and covers some more advanced aspects of Perl. As briefly mentioned previously, the aim was to write a more manageable code that supports all your current requirements and potential variations in the future. It is important that you understand what it is doing in order to modify it yourself in the future. If there is anything you don't understand feel free to ask about it. You may wish to use it as a basis to develop the simpler approach that tejas provided instead.

Regards,

Chris


(This post was edited by Zhris on Feb 2, 2015, 8:17 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Feb 2, 2015, 6:13 PM
Post edited by Zhris (Enthusiast) on Feb 2, 2015, 6:17 PM
Post edited by Zhris (Enthusiast) on Feb 2, 2015, 6:35 PM
Post edited by Zhris (Enthusiast) on Feb 2, 2015, 6:37 PM
Post edited by Zhris (Enthusiast) on Feb 2, 2015, 6:39 PM
Post edited by Zhris (Enthusiast) on Feb 2, 2015, 6:39 PM
Post edited by Zhris (Enthusiast) on Feb 2, 2015, 8:17 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives