Search your unanswered perl queries
Google

Friday, May 8, 2009

Reading a directory Sorting the files

The following post might help the readers to open a directory. skip the current directory . and previous directory .. and considering only the files which needs to be sorted. In files we use Tie::File to tie the records to the file. So whatever we operate on records changes are automatically made to the file.


Sample Function:

use Tie::File;

sub _sort_deployment_files{

my $deploy_dir

opendir(DIR, $deploy_dir);
@files = grep { /^[^\.]/ && -f "$deploy_dir/$_" } readdir(DIR);
closedir DIR;

foreach my $file_name (@files) {
tie my @records, "Tie::File", $file_name, autochomp => 0, mode => O_RDWR;
sort @records;
untie @records;
}

return;
}