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