home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _982c5df4e0190bba3ce10215af197ffb < prev    next >
Text File  |  2004-06-01  |  4KB  |  140 lines

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