LearningCpp LearningCpp - 1 year ago 63
Perl Question

How to check whether file content is empty , by traversing the array of filenames

i have a array of filenames , and i have to traverse this array and check whether the content of ALL the files is empty.

Here is the code

foreach my $reportFile (sort { getDateInName($b) <=> getDateInName($a)} @ReportFiles)
my @fileData = readFile($reportFile);
outputLog("FAIL: File Doesnt Contain Any Data.");

But in the code above, iam returning even if a single file is empty,
I would like to know how can we check whether ALL the content of ALL files is empty and then return.

So I would like to return only if none of the files in the array has content.

Even if one file has content i wouldnt return


Answer Source

Use List::Util::all:

Similar to any, except that it requires all elements of the @list to make the BLOCK return true. If any element returns false, then it returns false. If the BLOCK never returns false or the @list was empty then it returns true.

use List::Util 'all';

sub check_files {
  warn( "All files empty" ), return
    if all { -z } @_;


sub are_all_empty { all { -z } @_ }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download