vkk05
vkk05

Reputation: 3222

Perl readdir warning - Name "main::DIR" used only once: possible typo

I have a script which lists the possible file in particular directory. The code works fine, but how to avoid this warning?

#!/usr/bin/perl

use strict;
use warnings;
use autodie;

my $logpath = "C:/Users/Vinod/Perl/Log";

opendir(DIR, $logpath);
while (my $file = readdir(DIR)) {

    next unless (-f "$logpath/$file");
    print "FILENAME:$file\n";

}
closedir(DIR);

Warning shows while compiling or running the script is:

$ perl -cw log_fetch.pl
Name "main::DIR" used only once: possible typo at log_fetch.pl line ...
log_fetch.pl syntax OK

Upvotes: 2

Views: 352

Answers (1)

ikegami
ikegami

Reputation: 386386

This appears to be a weird side-effect of using use autodie;.

The warning can be silenced as follows:

sub x { *DIR }  # Silence spurious "used only once" warning.

However, you are needlessly using a global variable (*DIR). It would be far better to use a lexical variable, and this would moot the problem.

opendir(my $DIR, $logpath);

Upvotes: 2

Related Questions