#!/usr/bin/perl # use strict; use warnings; use HTML::Template; use Data::Dumper; my $path = '/mnt/data/Programming'; my $links = "$path/links.html"; my @strings = qw( file split opendir push sort sub % STDIN ); my %match; # Scan all the files and build a HoHoA # indexed by search string and file name # and containing the matched text. foreach my $file (glob("$path/*.pl")) { next if($file eq $links); next unless(-f $file); open(IN, '<', $file) or die "$file: $!"; my $content = do { local $/; }; close(IN); foreach my $string ( @strings ) { if(my @matches = ($content =~ m/((?:^.*\n)?(?:^.*$string.*$)+(?:\n.*$)?)/mg)) { $match{$string}{$file} = join("\n...\n", @matches); } } } # Reorganize the contents of %match into display order # and the format required by HTML::Template: # [ # { # string => "%", # files => [ # { # file => "/full/path/of/filex", # text => "matched text of file x", # }, # { # file => "/full/path/of/filey", # text => "matched text of file y", # }, # ... # ], # }, # { # string => "sub", # files => [ # { # file => "/full/path/of/filez", # text => "matched text of file z", # }, # ... # ], # }, # ... # ] my @params; foreach my $string (sort keys %match) { my @files; foreach my $file (sort keys %{$match{$string}}) { push(@files, { file => $file, text => $match{$string}{$file} }); } push(@params, { string => $string, files => \@files }); } # merge the data with the template and save it my $page = HTML::Template->new( filehandle => *DATA,); $page->param(STRINGS => \@params,); open(OUT, '>', $links) or die "$links: $!"; print OUT $page->output; close(OUT); __DATA__ My Program Links