Re-introduce pure-Perl fall-back for abs_path,
[p5sagit/p5-mst-13.2.git] / lib / File / stat.pm
1 package File::stat;
2 use strict;
3 use warnings;
4
5 use 5.6.0;
6 our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
7
8 our $VERSION = '1.00';
9
10 BEGIN { 
11     use Exporter   ();
12     @EXPORT      = qw(stat lstat);
13     @EXPORT_OK   = qw( $st_dev     $st_ino    $st_mode 
14                        $st_nlink   $st_uid    $st_gid 
15                        $st_rdev    $st_size 
16                        $st_atime   $st_mtime  $st_ctime 
17                        $st_blksize $st_blocks
18                     );
19     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
20 }
21 use vars @EXPORT_OK;
22
23 # Class::Struct forbids use of @ISA
24 sub import { goto &Exporter::import }
25
26 use Class::Struct qw(struct);
27 struct 'File::stat' => [
28      map { $_ => '$' } qw{
29          dev ino mode nlink uid gid rdev size
30          atime mtime ctime blksize blocks
31      }
32 ];
33
34 sub populate (@) {
35     return unless @_;
36     my $stob = new();
37     @$stob = (
38         $st_dev, $st_ino, $st_mode, $st_nlink, $st_uid, $st_gid, $st_rdev,
39         $st_size, $st_atime, $st_mtime, $st_ctime, $st_blksize, $st_blocks ) 
40             = @_;
41     return $stob;
42
43
44 sub lstat ($)  { populate(CORE::lstat(shift)) }
45
46 sub stat ($) {
47     my $arg = shift;
48     my $st = populate(CORE::stat $arg);
49     return $st if $st;
50     no strict 'refs';
51     require Symbol;
52     return populate(CORE::stat \*{Symbol::qualify($arg)});
53 }
54
55 1;
56 __END__
57
58 =head1 NAME
59
60 File::stat - by-name interface to Perl's built-in stat() functions
61
62 =head1 SYNOPSIS
63
64  use File::stat;
65  $st = stat($file) or die "No $file: $!";
66  if ( ($st->mode & 0111) && $st->nlink > 1) ) {
67      print "$file is executable with lotsa links\n";
68  } 
69
70  use File::stat qw(:FIELDS);
71  stat($file) or die "No $file: $!";
72  if ( ($st_mode & 0111) && $st_nlink > 1) ) {
73      print "$file is executable with lotsa links\n";
74  } 
75
76 =head1 DESCRIPTION
77
78 This module's default exports override the core stat() 
79 and lstat() functions, replacing them with versions that return 
80 "File::stat" objects.  This object has methods that
81 return the similarly named structure field name from the
82 stat(2) function; namely,
83 dev,
84 ino,
85 mode,
86 nlink,
87 uid,
88 gid,
89 rdev,
90 size,
91 atime,
92 mtime,
93 ctime,
94 blksize,
95 and
96 blocks.  
97
98 You may also import all the structure fields directly into your namespace
99 as regular variables using the :FIELDS import tag.  (Note that this still
100 overrides your stat() and lstat() functions.)  Access these fields as
101 variables named with a preceding C<st_> in front their method names.
102 Thus, C<$stat_obj-E<gt>dev()> corresponds to $st_dev if you import
103 the fields.
104
105 To access this functionality without the core overrides,
106 pass the C<use> an empty import list, and then access
107 function functions with their full qualified names.
108 On the other hand, the built-ins are still available
109 via the C<CORE::> pseudo-package.
110
111 =head1 NOTE
112
113 While this class is currently implemented using the Class::Struct
114 module to build a struct-like class, you shouldn't rely upon this.
115
116 =head1 AUTHOR
117
118 Tom Christiansen