
s660117
Novice
Oct 21, 2012, 12:49 PM
Post #29 of 31
(1189 views)
|
|
Re: [FishMonger] Using \? in a variable
[In reply to]
|
Can't Post
|
|
FishMonger, Even though the whole point of the exercise was to write a recursive script, I have rewritten it using File::Find --
#! /usr/bin/perl -w use strict; use warnings; use Getopt::Long; use File::Find; use Data::Dumper; my ($pattern, $dir, $str, $file); GetOptions ( 'd=s' => \$dir, 'p=s' => \$pattern, 's' => \$str, 'f' => \$file, ); die usage() unless ($dir and $pattern and ($str or $file)); $pattern = quotemeta($pattern); print Dumper ($pattern, $dir, $file, $str); -d $dir || die "Directory not found. Terminating $!"; print "Now scanning $dir for \"$pattern\"\n"; if ($file) { find_files($dir, $pattern); } else { parse_files($dir, $pattern); } #---------------------------------------------------------------------------------- sub find_files { find(sub { if (/$pattern/) { print "$File::Find::name found\n"; } return; } , $dir) } sub parse_files { find(sub { my $file_name = $_; open(FILE,$file_name) || die "Failed to open $file_name $!"; while (<FILE>) {(/$pattern/i) && print "$file_name => $_"}; close(FILE); return } , $dir) } sub usage { print "Usage: $0 -d <dir> -o <option> -s -f\n\n", "Example: $0 -d . -o 'some *? option' -f\n\n"; return; I remain mystified by the unexpected results I got when I entered a pattern of unquoted question marks. Thanks for all your help, s660117
|