1*0Sstevel@tonic-gatepackage File::stat; 2*0Sstevel@tonic-gateuse 5.006; 3*0Sstevel@tonic-gate 4*0Sstevel@tonic-gateuse strict; 5*0Sstevel@tonic-gateuse warnings; 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateour(@EXPORT, @EXPORT_OK, %EXPORT_TAGS); 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gateour $VERSION = '1.00'; 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gateBEGIN { 12*0Sstevel@tonic-gate use Exporter (); 13*0Sstevel@tonic-gate @EXPORT = qw(stat lstat); 14*0Sstevel@tonic-gate @EXPORT_OK = qw( $st_dev $st_ino $st_mode 15*0Sstevel@tonic-gate $st_nlink $st_uid $st_gid 16*0Sstevel@tonic-gate $st_rdev $st_size 17*0Sstevel@tonic-gate $st_atime $st_mtime $st_ctime 18*0Sstevel@tonic-gate $st_blksize $st_blocks 19*0Sstevel@tonic-gate ); 20*0Sstevel@tonic-gate %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] ); 21*0Sstevel@tonic-gate} 22*0Sstevel@tonic-gateuse vars @EXPORT_OK; 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate# Class::Struct forbids use of @ISA 25*0Sstevel@tonic-gatesub import { goto &Exporter::import } 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gateuse Class::Struct qw(struct); 28*0Sstevel@tonic-gatestruct 'File::stat' => [ 29*0Sstevel@tonic-gate map { $_ => '$' } qw{ 30*0Sstevel@tonic-gate dev ino mode nlink uid gid rdev size 31*0Sstevel@tonic-gate atime mtime ctime blksize blocks 32*0Sstevel@tonic-gate } 33*0Sstevel@tonic-gate]; 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gatesub populate (@) { 36*0Sstevel@tonic-gate return unless @_; 37*0Sstevel@tonic-gate my $stob = new(); 38*0Sstevel@tonic-gate @$stob = ( 39*0Sstevel@tonic-gate $st_dev, $st_ino, $st_mode, $st_nlink, $st_uid, $st_gid, $st_rdev, 40*0Sstevel@tonic-gate $st_size, $st_atime, $st_mtime, $st_ctime, $st_blksize, $st_blocks ) 41*0Sstevel@tonic-gate = @_; 42*0Sstevel@tonic-gate return $stob; 43*0Sstevel@tonic-gate} 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gatesub lstat ($) { populate(CORE::lstat(shift)) } 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gatesub stat ($) { 48*0Sstevel@tonic-gate my $arg = shift; 49*0Sstevel@tonic-gate my $st = populate(CORE::stat $arg); 50*0Sstevel@tonic-gate return $st if $st; 51*0Sstevel@tonic-gate my $fh; 52*0Sstevel@tonic-gate { 53*0Sstevel@tonic-gate local $!; 54*0Sstevel@tonic-gate no strict 'refs'; 55*0Sstevel@tonic-gate require Symbol; 56*0Sstevel@tonic-gate $fh = \*{ Symbol::qualify( $arg, caller() )}; 57*0Sstevel@tonic-gate return unless defined fileno $fh; 58*0Sstevel@tonic-gate } 59*0Sstevel@tonic-gate return populate(CORE::stat $fh); 60*0Sstevel@tonic-gate} 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate1; 63*0Sstevel@tonic-gate__END__ 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate=head1 NAME 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gateFile::stat - by-name interface to Perl's built-in stat() functions 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate=head1 SYNOPSIS 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate use File::stat; 72*0Sstevel@tonic-gate $st = stat($file) or die "No $file: $!"; 73*0Sstevel@tonic-gate if ( ($st->mode & 0111) && $st->nlink > 1) ) { 74*0Sstevel@tonic-gate print "$file is executable with lotsa links\n"; 75*0Sstevel@tonic-gate } 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate use File::stat qw(:FIELDS); 78*0Sstevel@tonic-gate stat($file) or die "No $file: $!"; 79*0Sstevel@tonic-gate if ( ($st_mode & 0111) && $st_nlink > 1) ) { 80*0Sstevel@tonic-gate print "$file is executable with lotsa links\n"; 81*0Sstevel@tonic-gate } 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate=head1 DESCRIPTION 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gateThis module's default exports override the core stat() 86*0Sstevel@tonic-gateand lstat() functions, replacing them with versions that return 87*0Sstevel@tonic-gate"File::stat" objects. This object has methods that 88*0Sstevel@tonic-gatereturn the similarly named structure field name from the 89*0Sstevel@tonic-gatestat(2) function; namely, 90*0Sstevel@tonic-gatedev, 91*0Sstevel@tonic-gateino, 92*0Sstevel@tonic-gatemode, 93*0Sstevel@tonic-gatenlink, 94*0Sstevel@tonic-gateuid, 95*0Sstevel@tonic-gategid, 96*0Sstevel@tonic-gaterdev, 97*0Sstevel@tonic-gatesize, 98*0Sstevel@tonic-gateatime, 99*0Sstevel@tonic-gatemtime, 100*0Sstevel@tonic-gatectime, 101*0Sstevel@tonic-gateblksize, 102*0Sstevel@tonic-gateand 103*0Sstevel@tonic-gateblocks. 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gateYou may also import all the structure fields directly into your namespace 106*0Sstevel@tonic-gateas regular variables using the :FIELDS import tag. (Note that this still 107*0Sstevel@tonic-gateoverrides your stat() and lstat() functions.) Access these fields as 108*0Sstevel@tonic-gatevariables named with a preceding C<st_> in front their method names. 109*0Sstevel@tonic-gateThus, C<$stat_obj-E<gt>dev()> corresponds to $st_dev if you import 110*0Sstevel@tonic-gatethe fields. 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gateTo access this functionality without the core overrides, 113*0Sstevel@tonic-gatepass the C<use> an empty import list, and then access 114*0Sstevel@tonic-gatefunction functions with their full qualified names. 115*0Sstevel@tonic-gateOn the other hand, the built-ins are still available 116*0Sstevel@tonic-gatevia the C<CORE::> pseudo-package. 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate=head1 BUGS 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gateAs of Perl 5.8.0 after using this module you cannot use the implicit 121*0Sstevel@tonic-gateC<$_> or the special filehandle C<_> with stat() or lstat(), trying 122*0Sstevel@tonic-gateto do so leads into strange errors. The workaround is for C<$_> to 123*0Sstevel@tonic-gatebe explicit 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate my $stat_obj = stat $_; 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gateand for C<_> to explicitly populate the object using the unexported 128*0Sstevel@tonic-gateand undocumented populate() function with CORE::stat(): 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate my $stat_obj = File::stat::populate(CORE::stat(_)); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate=head1 NOTE 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gateWhile this class is currently implemented using the Class::Struct 135*0Sstevel@tonic-gatemodule to build a struct-like class, you shouldn't rely upon this. 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate=head1 AUTHOR 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gateTom Christiansen 140