Leaked source code of windows server 2003
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.

169 lines
4.0 KiB

  1. #
  2. package IO::File;
  3. =head1 NAME
  4. IO::File - supply object methods for filehandles
  5. =head1 SYNOPSIS
  6. use IO::File;
  7. $fh = new IO::File;
  8. if ($fh->open("< file")) {
  9. print <$fh>;
  10. $fh->close;
  11. }
  12. $fh = new IO::File "> file";
  13. if (defined $fh) {
  14. print $fh "bar\n";
  15. $fh->close;
  16. }
  17. $fh = new IO::File "file", "r";
  18. if (defined $fh) {
  19. print <$fh>;
  20. undef $fh; # automatically closes the file
  21. }
  22. $fh = new IO::File "file", O_WRONLY|O_APPEND;
  23. if (defined $fh) {
  24. print $fh "corge\n";
  25. $pos = $fh->getpos;
  26. $fh->setpos($pos);
  27. undef $fh; # automatically closes the file
  28. }
  29. autoflush STDOUT 1;
  30. =head1 DESCRIPTION
  31. C<IO::File> inherits from C<IO::Handle> and C<IO::Seekable>. It extends
  32. these classes with methods that are specific to file handles.
  33. =head1 CONSTRUCTOR
  34. =over 4
  35. =item new ( FILENAME [,MODE [,PERMS]] )
  36. Creates a C<IO::File>. If it receives any parameters, they are passed to
  37. the method C<open>; if the open fails, the object is destroyed. Otherwise,
  38. it is returned to the caller.
  39. =item new_tmpfile
  40. Creates an C<IO::File> opened for read/write on a newly created temporary
  41. file. On systems where this is possible, the temporary file is anonymous
  42. (i.e. it is unlinked after creation, but held open). If the temporary
  43. file cannot be created or opened, the C<IO::File> object is destroyed.
  44. Otherwise, it is returned to the caller.
  45. =back
  46. =head1 METHODS
  47. =over 4
  48. =item open( FILENAME [,MODE [,PERMS]] )
  49. C<open> accepts one, two or three parameters. With one parameter,
  50. it is just a front end for the built-in C<open> function. With two or three
  51. parameters, the first parameter is a filename that may include
  52. whitespace or other special characters, and the second parameter is
  53. the open mode, optionally followed by a file permission value.
  54. If C<IO::File::open> receives a Perl mode string ("E<gt>", "+E<lt>", etc.)
  55. or a ANSI C fopen() mode string ("w", "r+", etc.), it uses the basic
  56. Perl C<open> operator (but protects any special characters).
  57. If C<IO::File::open> is given a numeric mode, it passes that mode
  58. and the optional permissions value to the Perl C<sysopen> operator.
  59. The permissions default to 0666.
  60. For convenience, C<IO::File> exports the O_XXX constants from the
  61. Fcntl module, if this module is available.
  62. =back
  63. =head1 SEE ALSO
  64. L<perlfunc>,
  65. L<perlop/"I/O Operators">,
  66. L<IO::Handle>
  67. L<IO::Seekable>
  68. =head1 HISTORY
  69. Derived from FileHandle.pm by Graham Barr E<lt>F<[email protected]>E<gt>.
  70. =cut
  71. require 5.005_64;
  72. use strict;
  73. our($VERSION, @EXPORT, @EXPORT_OK, @ISA);
  74. use Carp;
  75. use Symbol;
  76. use SelectSaver;
  77. use IO::Seekable;
  78. use File::Spec;
  79. require Exporter;
  80. @ISA = qw(IO::Handle IO::Seekable Exporter);
  81. $VERSION = "1.08";
  82. @EXPORT = @IO::Seekable::EXPORT;
  83. eval {
  84. # Make all Fcntl O_XXX constants available for importing
  85. require Fcntl;
  86. my @O = grep /^O_/, @Fcntl::EXPORT;
  87. Fcntl->import(@O); # first we import what we want to export
  88. push(@EXPORT, @O);
  89. };
  90. ################################################
  91. ## Constructor
  92. ##
  93. sub new {
  94. my $type = shift;
  95. my $class = ref($type) || $type || "IO::File";
  96. @_ >= 0 && @_ <= 3
  97. or croak "usage: new $class [FILENAME [,MODE [,PERMS]]]";
  98. my $fh = $class->SUPER::new();
  99. if (@_) {
  100. $fh->open(@_)
  101. or return undef;
  102. }
  103. $fh;
  104. }
  105. ################################################
  106. ## Open
  107. ##
  108. sub open {
  109. @_ >= 2 && @_ <= 4 or croak 'usage: $fh->open(FILENAME [,MODE [,PERMS]])';
  110. my ($fh, $file) = @_;
  111. if (@_ > 2) {
  112. my ($mode, $perms) = @_[2, 3];
  113. if ($mode =~ /^\d+$/) {
  114. defined $perms or $perms = 0666;
  115. return sysopen($fh, $file, $mode, $perms);
  116. }
  117. if (! File::Spec->file_name_is_absolute($file)) {
  118. $file = File::Spec->catfile(File::Spec->curdir(),$file);
  119. }
  120. $file = IO::Handle::_open_mode_string($mode) . " $file\0";
  121. }
  122. open($fh, $file);
  123. }
  124. 1;