#!/usr/bin/perl use strict; use warnings; my $tmp_dir="/tmp/"; opendir (DIR, $tmp_dir) or die $!; while (my $file_name = readdir(DIR)) { my $abs_path = $tmp_dir.$file_name; unless(-d $abs_path) #Ignore if it is a directory { &Process_EachFile($abs_path); # Do call a function and process each file } else { print "$abs_path is a directory\n"; } } closedir(DIR); sub Process_EachFile { my ($filename) = @_; print $filename; open (FH,'<',"$filename") or die $!; while(my $each_line=) { print $each_line."\n"; #Read file content here and do your calculation. } }