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