Home > Net >  stat function in file module
stat function in file module

Time:09-30

Can anyone help me to understand the meaning of below line?

#!/usr/bin/perl

use File::stat;

...

sub rootdev { return (stat readlink)[0] == (stat "/")[0]; }
my @vols = shuffle map {/.*\/([0-9] )/} grep {not rootdev} grep {-e readlink} grep {-l} glob("/dev/shm/v/*");

...

There is submodule like above, but I cannot understand the meaning of stat readlink)[0] == (stat "/")[0].

CodePudding user response:

( LIST )[ LIST ] is called a list slice.

It returns the elements of the first list identified by the indexes returned by the second list.

say for ( 'A'..'Z' )[ 0, 1, 2, 25 ];   # A B C Z

That means that ( stat ... )[0] returns the first value returned by stat.


The builtin stat operator returns information about the file. It returns a number of values, the first is the device id of the file.

So, when using the builtin stat, ( stat $path )[0] returns the device id of the file specified by $path.


But you're not using the built stat. You're using the one from File::stat. In that situation the sub you posted doesn't do anything useful. It effectively does return 0 because it's comparing the memory addresses of two different objects. The following is adjusted for using File:stat's stat:

use File::stat;

sub rootdev { return ( stat readlink )->dev == ( stat "/" )->dev; }

or

use File::stat;

sub rootdev { return stat( readlink )->dev == stat( "/" )->dev; }

Finally, it's really weird that the sub requires that its input be provided using $_ instead of as an argument.

CodePudding user response:

I got the answer.

The input of the submodule rootdev is grep {-e readlink} grep {-l} glob("/dev/shm/v/*") .

So, firstly get the filelist from /dev/shm/v/ directory and filter with grep only for symlink.

Then check the device id(first array info of stat function) and compare it with root device.

It's for checking the pointed device of symlink in /dev/shm/v is root device or not to include or exclude whatever.

  • Related