[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Why do I get an error using 'rename'?



On Fri, 12 Nov 1999, Robert Terwilliger wrote:

> Hi people.  I am trying to remove the '.out' suffix from each file in my
> directory.  Here is my code:
> 
> opendir(DIR, ':::healthcare.ucla.edu:pls:') or die 'cannot open directory';
> @files = readdir(DIR);
> closedir(DIR);
> 
> foreach $file (@files)
> {
>     if ($file =~ m/.out/)
>     {
>         ($temp = $file) =~ s/.out//;
>         rename ($file, $temp) or warn "Could not rename $file to $temp\n";
>     }
> } 
> 
You've already gotten the scoop on the real problem, but I think it's
worth pointing out that you're lucking out on one other thing.

If you want to make sure that ".out" is matched as a suffix, anchor the
match with "$". Furthermore, and I'm sure you know this but overlooked it,
"." is matching everything except newline. You actually want "\." - escape
the dot.

As it stands right now you would also match "mouth.pdf" or
"without_this_essential_file_all_things_crash.txt".

Regards, Arved



# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org