Lktee wrote:
>
> Hi, now I design another way.
Hello,
> First I request user enter the directory want to search first, if the
> directory exist in the system, script will search the entire directory. If
> the filename is same will display out, else no result will return. If
> directory not exist will request user try another directory.
>
> So, the script like below:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use File::Find;
> find ( \&callback, "/$target");
>
> my $target;
> while (1) {
> print "what directory want search?";
> $target = <STDIN>
> chmod $target;
> if (-d $target) {
> print "Yes, target is a directory.\n";
> next;
> }
> else{
> print "No, $target not a directory.\n";
>
> sub callback{
> print $File::Find::name, "\n" if $File::Find::name =~
> /File1$/;
> }
>
> May I know any wrong in my coding? Because I cannot run the script. Any idea
> about this?
You need to put the find() call in the loop and put the sub callback {}
outside the loop and you also need to chomp the contents of $target.
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
sub callback {
print "$File::Find::name\n" if $_ eq 'File1';
}
my $target;
while (1) {
print "what directory want search?";
chomp( my $target = <STDIN> );
unless ( -d $target ) {
print "No, $target not a directory.\n";
next;
}
print "Yes, target is a directory.\n";
find( \&callback, $target );
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>