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