#!/usr/bin/perl -w use strict; use warnings; my $dir = 'C:\Users\ZB\Desktop\Text Files'; opendir (DIR, $dir) or die $!; while (my $file = readdir(DIR)) { next if $file eq '.'; next if $file eq '..'; &countWords($dir, $file); #call sub } sub countWords { my $line = @_; #receive file as input my @array = split(/ /, $line); print "$#array\n"; } closedir(DIR);