TransWikia.com

How to print absolute path of some file from c: root in perl?

Super User Asked on February 4, 2021

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:UsersppastOneDrivePlochab.pl line 5.  

Does anyone know, what is wrong?

One Answer

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 $!;

Answered by choroba on February 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP