If I'm understanding you correctly, perhaps the following will be helpful:
use strict;
use warnings;
chomp( my @fileNames = <DATA> );
my @sortedFileNames = map $_->[0],
sort { $b->[1] <=> $a->[1] }
map { my ( $d, $m, $y ) = /(\d+)/g; [ $_, "$y$m$d" ] }
grep /^backup_\d\d_\d\d_\d{4}.bak$/, @fileNames;
shift @sortedFileNames;
if (@sortedFileNames) {
print "$_\n" for @sortedFileNames;
#unlink @sortedFileNames;
}
__DATA__
backup_21_01_2013.bak
file.txt
backup_20_01_2013.bak
what_is_this.doc
backup_24_01_2013.bak
never_open_this.docx
backup_22_01_2013.bak
stuff.ini
backup_23_01_2013.bak
more_stuff.ini
deleteOldBackups.pl
Output (the files that would be deleted):
backup_23_01_2013.bak
backup_22_01_2013.bak
backup_21_01_2013.bak
backup_20_01_2013.bak
If you populate @fileNames with the file names in the directory where the backups live, it will grep them only allowing backup-patterned files through. Then, using a Schwartzian transform, it sorts the backup file names in decending order and shifts off the first element (most recent backup file name) from @sortedFileNames. As it is now, the file names in @sortedFileNames are printed, but the unlink line can be uncommented, so all but the most recent backup files are deleted.
** Please thoroughly test and verify this on a copy of the backup directory before going live with it. **