http://qs321.pair.com?node_id=1151969


in reply to check for latest file, remove and touch file?

This seems overcomplicated.

Do you want the "latest file" as per the filesystem or the highest-sequence file as per your naming convention? Is the inode change time important? The modification time? Is it just the filename? What if the files get out of sync and you end up having S0007 but not S0006? Do you want to still delete S0005 if it exists? Why not sort your files, pop the two newest off the array, then delete the rest if making sure you have the two newest available is what you're actually trying to do?

You should learn to use Perl modules, but if you're in a hurry what does the Unix touch command buy you here that open and close don't?

sub my_touch { my $f = shift; open my $fh, '>>', $f or die "Can't write to $f: $!\n"; close $fh; }

Why are you shelling out to ls when you have opendir and readdir or glob?

Also, I'm not going to recommend a formatting standard because that's kind of your own choice, but I do recommend you choose one and stick to it. It will be easier to pick one that is fairly common.

Update: changed the mode of the open in the example code to append per the suggestion by AnomalousMonk.