Re-introduce pure-Perl fall-back for abs_path,
[p5sagit/p5-mst-13.2.git] / lib / File / stat.pm
CommitLineData
36477c24 1package File::stat;
2use strict;
b395063c 3use warnings;
36477c24 4
b395063c 5use 5.6.0;
17f410f9 6our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
7
b75c8c73 8our $VERSION = '1.00';
9
36477c24 10BEGIN {
11 use Exporter ();
36477c24 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}
d8574e8f 21use vars @EXPORT_OK;
36477c24 22
8cc95fdb 23# Class::Struct forbids use of @ISA
24sub import { goto &Exporter::import }
25
ee28235b 26use Class::Struct qw(struct);
36477c24 27struct 'File::stat' => [
28 map { $_ => '$' } qw{
29 dev ino mode nlink uid gid rdev size
30 atime mtime ctime blksize blocks
31 }
32];
33
34sub 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
14d597e2 44sub lstat ($) { populate(CORE::lstat(shift)) }
36477c24 45
46sub 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
551;
56__END__
57
58=head1 NAME
59
2ae324a7 60File::stat - by-name interface to Perl's built-in stat() functions
36477c24 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
78This module's default exports override the core stat()
79and lstat() functions, replacing them with versions that return
80"File::stat" objects. This object has methods that
81return the similarly named structure field name from the
82stat(2) function; namely,
83dev,
84ino,
85mode,
86nlink,
87uid,
88gid,
89rdev,
90size,
91atime,
92mtime,
93ctime,
94blksize,
95and
96blocks.
97
98You may also import all the structure fields directly into your namespace
99as regular variables using the :FIELDS import tag. (Note that this still
100overrides your stat() and lstat() functions.) Access these fields as
101variables named with a preceding C<st_> in front their method names.
102Thus, C<$stat_obj-E<gt>dev()> corresponds to $st_dev if you import
103the fields.
104
105To access this functionality without the core overrides,
106pass the C<use> an empty import list, and then access
107function functions with their full qualified names.
108On the other hand, the built-ins are still available
109via the C<CORE::> pseudo-package.
110
111=head1 NOTE
112
8cc95fdb 113While this class is currently implemented using the Class::Struct
36477c24 114module to build a struct-like class, you shouldn't rely upon this.
115
116=head1 AUTHOR
117
118Tom Christiansen