Bruno9779 Bruno9779 - 2 months ago 7
Perl Question

Perl: using stat()

I am trying to use stat() in a perl script.
I have read the documentation on perldocs that says to use it like this:

$mode = (stat($filename))[2];


But this does not work:

$mode = (stat($filename))[2];
print $mode;

Use of uninitialized value $mode in print at ...


I think another syntax is needed to access the values returned by stat. Data::dumper returns this structure:

@stat = stat($filename);
print Dumper(@stat);

$VAR1 = bless( [
48,
305368,
33188,
1,
0,
0,
0,
'2011',
1397569653,
1397569653,
1397569653,
4096,
8
], 'File::stat' );

cjm cjm
Answer

Apparently, you also have the line

use File::stat;

in your program, and forgot to mention it. File::stat overrides the built-in stat function with a version that returns objects. You should read its documentation instead.

In your case, you'd want:

use File::stat;

my $mode = stat($filename)->mode;