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