Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.7 KiB

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