I am writing a perl script to scan all *.html and *.jsp files underneath a
directory recursively and print strings (urls) matching a pattern.
I can do that fine. Problem is when I try to store what it outputs to
console into an array, not able to make it to print the contents.
The reason I am trying to even store it inside an array is to enable me to
sort it next. Any help/pointers greatly appreciated.
Thanks
Script below:
===========
#!/usr/bin/perl -w
#use strict
use File::Find;
my $dir_to_process = "C:/MyDirectory";
#opendir DH, $dir_to_process or die "Cannot open $dir_to_process:$1";
print "Files in $dir_to_process are:\n";
$i = 0;
@url_array = (1..2000); # initialized to have enough space in the array
find (\&wanted, $dir_to_process);
print "Printing url_array\n:";
for ($j=0; $j <= $#url_array; $j++)
{
print ($url_array[$j], "\n"); # This DOES NOT print url strings as
expected!!!!!
}
sub wanted {
return unless -f; #skip directories
if ( -f and /.html?/ ) {
$file = $_;
open FILE, $file;
@lines = <FILE>;
close FILE;
for $line (@lines) {
if ($line =~ /somepattern/) {
#print"$line";
$url_line = $';
$url_line =~ /\"/;
$url = $`;
print "$i ";
print "$url";
print "\n"; # This prints FINE the string as expected!
$url_array[$i] = $url;
$i++;
}
}
}
if ( -f and /.jsp?/ ) {
$file = $_;
open FILE, $file;
@lines = <FILE>;
close FILE;
for $line (@lines) {
if ($line =~ /somepattern) {
$url_line = $';
#print $';
$url_line =~ /\"/;
$url = $`;
print "$i ";
print "$url";
print "\n"; # This prints FINE the string as expected!
$url_array[$i] = $url;
$i++;
}
}
}
}