Hi Monks!
I have a procedure whose task is to count checksums for files that are a input argument in subroutine. Argument can be:
- All files ('*') in a directory ($DIR).
- Files with the extensions, for example: '*.foo *.bar *.pl *.pm'
- Files with names beginning, for example: 'FOO* bar*'
--------------8<-------------- sub md5files { use File::Basename; my $dir = $_[0]; # where the files are (must be a di +rect path from root /) my $what = $_[1] || '*'; # what files, default = '*' (all fi +les) my $md5file = "$dir/checksums.md5"; my %hash_md5; my @t = split(/ /, $what); open (MD5OUT, '>', $md5file ) or die "Err: $!"; foreach my $f (@t) { foreach my $file2md5 (<$dir/$f>) { next if "$file2md5" eq "$md5file"; my $md5 = &md5calc($file2md5); if($md5 ne "") { $file2md5 =~ s/$dir\///; print MD5OUT $md5 . " " . $file2md5 . "\n"; $hash_md5{$md5} = $file2md5; } else { return ""; } } } close MD5OUT; return %hash_md5; } --------------8<--------------
Above sub can be called as follows:
my $DIR = '/tmp';
my %md5hash = &md5files($DIR);
my %md5hash = &md5files($DIR,'FOO* bar*');
my %md5hash = &md5files($DIR,'*.foo *.bar *.pl *.pm');
Is it possible to simplify my procedure? Could it be more perlish?
Thanks in advance.
Best regards,
--
Scottie
In reply to Pass the arguments to the procedure in an easier way by Scottie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |