문제

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' );
도움이 되었습니까?

해결책

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;

다른 팁

You can do it with an array slice (stat $filename)[2]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top