Having this perl:

#!/usr/bin/perl -w
use Cwd qw[abs_path];
use autodie;
opendir C, "c:\\";
@ar = map { abs_path($_) } readdir(C) or die $!;
print $ar[0];

I am trying to print an absolute path of first file from c:\, but an error:

Use of uninitialized value $ar[0] in print at C:\Users\ppast\OneDrive\Plocha\b.pl line 5.  

Does anyone know, what is wrong?

  • Check the success of opendir: opendir C, "C:\\" or die $!;. – choroba Jul 22 at 20:52

The readdir returns the file names, not paths to them. You need to prefix the paths yourself:

my @ar = map { abs_path('C:\\' . $_) } readdir C;

You should also check the success of opendir:

opendir C, "C:\\" or die $!;
| improve this answer | |
  • I am using autodie to check for opening the dir. Sorry, but didnt help – Herdsman Jul 22 at 21:11
  • @Herdsman: Sorry, I had a typo in my code, please, try again. – choroba Jul 22 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.