RandomAccess.pod 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #------------------------------------------------------------------------------
  2. # File: RandomAccess.pod -- Documentation for File::RandomAccess
  3. #
  4. # Description: Buffer to support random access reading of sequential file
  5. #
  6. # Legal: Copyright (c) 2003-2016 Phil Harvey (phil at owl.phy.queensu.ca)
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the same terms as Perl itself.
  9. #------------------------------------------------------------------------------
  10. =head1 NAME
  11. File::RandomAccess - Random access reads of sequential file or scalar
  12. =head1 SYNOPSIS
  13. use File::RandomAccess;
  14. $raf = new File::RandomAccess(\*FILE, $disableSeekTest);
  15. $raf = new File::RandomAccess(\$data);
  16. $err = $raf->Seek($pos);
  17. $num = $raf->Read($buff, $bytes);
  18. =head1 DESCRIPTION
  19. Allows random access to sequential file by buffering the file if necessary.
  20. Also allows access to data in memory to be accessed as if it were a file.
  21. =head1 METHODS
  22. =over 4
  23. =item B<new>
  24. Creates a new RandomAccess object given a file reference or
  25. reference to data in memory.
  26. # Read from open file or pipe
  27. $raf = new File::RandomAccess(\*FILE);
  28. # Read from data in memory
  29. $raf = new File::RandomAccess(\$data);
  30. =over 4
  31. =item Inputs:
  32. 0) Reference to RandomAccess object or RandomAccess class name.
  33. 1) File reference or scalar reference.
  34. 2) Flag set if file is already random access (disables automatic SeekTest).
  35. =item Returns:
  36. Reference to RandomAccess object.
  37. =back
  38. =item B<SeekTest>
  39. Performs test seek() on file to determine if buffering is necessary. If
  40. the seek() fails, then the file is buffered to allow random access.
  41. B<SeekTest>() is automatically called from B<new> unless specified.
  42. $result = $raf->SeekTest();
  43. =over 4
  44. =item Inputs:
  45. 0) Reference to RandomAccess object.
  46. =item Returns:
  47. 1 if seek test passed (ie. no buffering required).
  48. =item Notes:
  49. Must be called before any other i/o.
  50. =back
  51. =item B<Tell>
  52. Get current position in file
  53. $pos = $raf->Tell();
  54. =over 4
  55. =item Inputs:
  56. 0) Reference to RandomAccess object.
  57. =item Returns:
  58. Current position in file
  59. =back
  60. =item B<Seek>
  61. Seek to specified position in file. When buffered, this doesn't quite
  62. behave like seek() since it returns success even if you seek outside the
  63. limits of the file.
  64. $success = $raf->Seek($pos, 0);
  65. =over 4
  66. =item Inputs:
  67. 0) Reference to RandomAccess object.
  68. 1) Position.
  69. 2) Whence (0=from start, 1=from cur pos, 2=from end).
  70. =item Returns:
  71. 1 on success, 0 otherwise
  72. =back
  73. =item B<Read>
  74. Read data from the file.
  75. $num = $raf->Read($buff, 1024);
  76. =over 4
  77. =item Inputs:
  78. 0) Reference to RandomAccess object.
  79. 1) Buffer.
  80. 2) Number of bytes to read.
  81. =item Returns:
  82. Number of bytes actually read.
  83. =back
  84. =item B<ReadLine>
  85. Read a line from file (end of line is $/).
  86. =over 4
  87. =item Inputs:
  88. 0) Reference to RandomAccess object.
  89. 1) Buffer.
  90. =item Returns:
  91. Number of bytes read.
  92. =back
  93. =item B<Slurp>
  94. Read whole file into buffer, without changing read pointer.
  95. =over 4
  96. =item Inputs:
  97. 0) Reference to RandomAccess object.
  98. =item Returns:
  99. Nothing.
  100. =back
  101. =item B<BinMode>
  102. Set binary mode for file.
  103. =over 4
  104. =item Inputs:
  105. 0) Reference to RandomAccess object.
  106. =item Returns:
  107. Nothing.
  108. =back
  109. =item B<Close>
  110. Close the file and free the buffer.
  111. =over 4
  112. =item Inputs:
  113. 0) Reference to RandomAccess object.
  114. =item Returns:
  115. Nothing.
  116. =back
  117. =back
  118. =head1 AUTHOR
  119. Copyright 2003-2016 Phil Harvey (phil at owl.phy.queensu.ca)
  120. This library is free software; you can redistribute it and/or modify it
  121. under the same terms as Perl itself.
  122. =head1 SEE ALSO
  123. L<Image::ExifTool(3pm)|Image::ExifTool>
  124. =cut
  125. # end