in reply to File Find

Hello,

there are more than one ways to do it...

1. Call the programm with a parameterlist where the last is the filename you are looking for and remove this name before starting find with my $filename = pop(@ARGV); and then compare each found filename with $filename

2. Use named parameters, e.g. with the Getopt::* - modules (e.g Getopt::Long) 3. Use named parameters and care for them by yourself, e.g.

scriptname.pl --dir="/dir1/sdir1" --dir="/dir2/sdir2" --file="thisfile +.txt"
I don't know if there are modules that handle this case. If not, you could do it like the following:
my $params = &GetParameters(); my @dirs = @{ $params->{dir} }; my @file = @{ $params->{file} }; # and so on sub GetParameters { my @allowedParams = qw(file dir); die &PrintUsage() unless @ARGV; my $params = {}; # shell contain parameters foreach (@ARGV){ my ($key, $val) = split(/\=/, $_, 2); # split up by the first = # test if $key is a valid parameter unless ($key =~ s/^\-\-//) { # must start with -- die "Error: invalid param $key\n"; } unless ( grep {lc($key) eq lc($_)} @allowedParams){ die "Error: param $key not allowed\n"; } # maybe do some checks if $val is existing .... # add param to hashreference $params push (@{ $params->{$key} }, $val); } # for return ($params); } # GetParameters
But before you use this code (which I haven't tested), better look if there's a module on CPAN that solves this problem.

Btw: i think programming with use strict; and use warnings; might be a very good idea, because it is much easier to find "silly" and "not-so-silly" errors that way

Best regards,
perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Replies are listed 'Best First'.
Re: Re: File Find
by oaklander (Acolyte) on Mar 13, 2002 at 14:45 UTC
    Ran the script as mentioned and seem to get errors about around the &PrintUsage area. I tried using just die "print error\n" and still getting errors. Please advise on how I can get this to work?

      You would, of course, have to implement the PrintUsage subroutine. Adding something like this to the top should do the trick:

      sub PrintUsage{ print <<USAGE This is where you would type the error message that you want displayed if someone passes arguments to your script that you haven't specified, or if the syntax is wrong, etc. USAGE ; }