http://www.perlmonks.org?node_id=18090


in reply to Odd file rename

Names changed to protect the guilty - can I do this while the file is open, or do I need to close it first? Please excuse my hideous indentation, it's a work in progress.
#!/usr/bin/perl @incoming = ( `ls /home/user/perl/testincoming` ); foreach $directory (@incoming) { chomp($directory); @files = ( `ls /home/user/perl/testincoming/$directory` ); foreach $file (@files) { $pattern = "PGP"; open(FILE,"/home/user/perl/testincoming/$directory/$file") or +die "Could n't open $file"; while (<FILE>) { if (/\Q$pattern\E/) { my $newname=$file; $newname=~s/^./o/; rename ($file, $newname); } close FILE; } } }