# good, open with error checking
opendir(DIR, "$dir") || die "can't open $dir $!\n";
# maybe name the variable @filenames - as there is more than one
# or get rid of the variable altogether by saying
# foreach my $oldname ( readdir(DIR) )
my @filename = readdir (DIR);
# declare filename with my and give better name (see above)
# foreach my $oldname (@filename)
foreach $filename (@filename) {
# this matches against the special variable $_
# (which you are not using) you want
# if ($filename =~ / /)
if (/ /) {
# the regex is completely wrong you want either
# s/ +/./g or tr/ /./ try to figure out the difference
# and $newfile is never assigned a value so:
my $newfile = $oldfile;
$newfile =~ s/ +/./g;
# you don't have to do the rename when you didn't
# change anything, so
rename($oldname, $newfile) unless $oldname eq $newfile;
}
}
closedir(DIR);
####
#!/usr/bin/perl -w
# rename - Larry's filename fixer
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = ) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
####
% rename 's/\.orig$//' *.orig
% rename 'tr/A-Z/a-z/ unless /^Make/' *
% rename '$_ .= ".bad"' *.f
% rename 'print "$_: "; s/foo/bar/ if =~ /^y/i' *
% find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/'
####
% rename 's/ +/./g' *
or
% rename 'tr/ /./' *