home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_mlb.zip / File / stat.pm < prev   
Text File  |  1997-11-25  |  3KB  |  114 lines

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