home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / perl / 5.8.8 / IO / Handle.pm < prev    next >
Encoding:
Perl POD Document  |  2006-07-07  |  7.4 KB  |  371 lines

  1. package IO::Handle;
  2.  
  3. use 5.006_001;
  4. use strict;
  5. our($VERSION, @EXPORT_OK, @ISA);
  6. use Carp;
  7. use Symbol;
  8. use SelectSaver;
  9. use IO ();    # Load the XS module
  10.  
  11. require Exporter;
  12. @ISA = qw(Exporter);
  13.  
  14. $VERSION = "1.25";
  15. $VERSION = eval $VERSION;
  16.  
  17. @EXPORT_OK = qw(
  18.     autoflush
  19.     output_field_separator
  20.     output_record_separator
  21.     input_record_separator
  22.     input_line_number
  23.     format_page_number
  24.     format_lines_per_page
  25.     format_lines_left
  26.     format_name
  27.     format_top_name
  28.     format_line_break_characters
  29.     format_formfeed
  30.     format_write
  31.  
  32.     print
  33.     printf
  34.     getline
  35.     getlines
  36.  
  37.     printflush
  38.     flush
  39.  
  40.     SEEK_SET
  41.     SEEK_CUR
  42.     SEEK_END
  43.     _IOFBF
  44.     _IOLBF
  45.     _IONBF
  46. );
  47.  
  48. ################################################
  49. ## Constructors, destructors.
  50. ##
  51.  
  52. sub new {
  53.     my $class = ref($_[0]) || $_[0] || "IO::Handle";
  54.     @_ == 1 or croak "usage: new $class";
  55.     my $io = gensym;
  56.     bless $io, $class;
  57. }
  58.  
  59. sub new_from_fd {
  60.     my $class = ref($_[0]) || $_[0] || "IO::Handle";
  61.     @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
  62.     my $io = gensym;
  63.     shift;
  64.     IO::Handle::fdopen($io, @_)
  65.     or return undef;
  66.     bless $io, $class;
  67. }
  68.  
  69. #
  70. # There is no need for DESTROY to do anything, because when the
  71. # last reference to an IO object is gone, Perl automatically
  72. # closes its associated files (if any).  However, to avoid any
  73. # attempts to autoload DESTROY, we here define it to do nothing.
  74. #
  75. sub DESTROY {}
  76.  
  77. ################################################
  78. ## Open and close.
  79. ##
  80.  
  81. sub _open_mode_string {
  82.     my ($mode) = @_;
  83.     $mode =~ /^\+?(<|>>?)$/
  84.       or $mode =~ s/^r(\+?)$/$1</
  85.       or $mode =~ s/^w(\+?)$/$1>/
  86.       or $mode =~ s/^a(\+?)$/$1>>/
  87.       or croak "IO::Handle: bad open mode: $mode";
  88.     $mode;
  89. }
  90.  
  91. sub fdopen {
  92.     @_ == 3 or croak 'usage: $io->fdopen(FD, MODE)';
  93.     my ($io, $fd, $mode) = @_;
  94.     local(*GLOB);
  95.  
  96.     if (ref($fd) && "".$fd =~ /GLOB\(/o) {
  97.     # It's a glob reference; Alias it as we cannot get name of anon GLOBs
  98.     my $n = qualify(*GLOB);
  99.     *GLOB = *{*$fd};
  100.     $fd =  $n;
  101.     } elsif ($fd =~ m#^\d+$#) {
  102.     # It's an FD number; prefix with "=".
  103.     $fd = "=$fd";
  104.     }
  105.  
  106.     open($io, _open_mode_string($mode) . '&' . $fd)
  107.     ? $io : undef;
  108. }
  109.  
  110. sub close {
  111.     @_ == 1 or croak 'usage: $io->close()';
  112.     my($io) = @_;
  113.  
  114.     close($io);
  115. }
  116.  
  117. ################################################
  118. ## Normal I/O functions.
  119. ##
  120.  
  121. # flock
  122. # select
  123.  
  124. sub opened {
  125.     @_ == 1 or croak 'usage: $io->opened()';
  126.     defined fileno($_[0]);
  127. }
  128.  
  129. sub fileno {
  130.     @_ == 1 or croak 'usage: $io->fileno()';
  131.     fileno($_[0]);
  132. }
  133.  
  134. sub getc {
  135.     @_ == 1 or croak 'usage: $io->getc()';
  136.     getc($_[0]);
  137. }
  138.  
  139. sub eof {
  140.     @_ == 1 or croak 'usage: $io->eof()';
  141.     eof($_[0]);
  142. }
  143.  
  144. sub print {
  145.     @_ or croak 'usage: $io->print(ARGS)';
  146.     my $this = shift;
  147.     print $this @_;
  148. }
  149.  
  150. sub printf {
  151.     @_ >= 2 or croak 'usage: $io->printf(FMT,[ARGS])';
  152.     my $this = shift;
  153.     printf $this @_;
  154. }
  155.  
  156. sub getline {
  157.     @_ == 1 or croak 'usage: $io->getline()';
  158.     my $this = shift;
  159.     return scalar <$this>;
  160.  
  161. *gets = \&getline;  # deprecated
  162.  
  163. sub getlines {
  164.     @_ == 1 or croak 'usage: $io->getlines()';
  165.     wantarray or
  166.     croak 'Can\'t call $io->getlines in a scalar context, use $io->getline';
  167.     my $this = shift;
  168.     return <$this>;
  169. }
  170.  
  171. sub truncate {
  172.     @_ == 2 or croak 'usage: $io->truncate(LEN)';
  173.     truncate($_[0], $_[1]);
  174. }
  175.  
  176. sub read {
  177.     @_ == 3 || @_ == 4 or croak 'usage: $io->read(BUF, LEN [, OFFSET])';
  178.     read($_[0], $_[1], $_[2], $_[3] || 0);
  179. }
  180.  
  181. sub sysread {
  182.     @_ == 3 || @_ == 4 or croak 'usage: $io->sysread(BUF, LEN [, OFFSET])';
  183.     sysread($_[0], $_[1], $_[2], $_[3] || 0);
  184. }
  185.  
  186. sub write {
  187.     @_ >= 2 && @_ <= 4 or croak 'usage: $io->write(BUF [, LEN [, OFFSET]])';
  188.     local($\) = "";
  189.     $_[2] = length($_[1]) unless defined $_[2];
  190.     print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
  191. }
  192.  
  193. sub syswrite {
  194.     @_ >= 2 && @_ <= 4 or croak 'usage: $io->syswrite(BUF [, LEN [, OFFSET]])';
  195.     if (defined($_[2])) {
  196.     syswrite($_[0], $_[1], $_[2], $_[3] || 0);
  197.     } else {
  198.     syswrite($_[0], $_[1]);
  199.     }
  200. }
  201.  
  202. sub stat {
  203.     @_ == 1 or croak 'usage: $io->stat()';
  204.     stat($_[0]);
  205. }
  206.  
  207. ################################################
  208. ## State modification functions.
  209. ##
  210.  
  211. sub autoflush {
  212.     my $old = new SelectSaver qualify($_[0], caller);
  213.     my $prev = $|;
  214.     $| = @_ > 1 ? $_[1] : 1;
  215.     $prev;
  216. }
  217.  
  218. sub output_field_separator {
  219.     carp "output_field_separator is not supported on a per-handle basis"
  220.     if ref($_[0]);
  221.     my $prev = $,;
  222.     $, = $_[1] if @_ > 1;
  223.     $prev;
  224. }
  225.  
  226. sub output_record_separator {
  227.     carp "output_record_separator is not supported on a per-handle basis"
  228.     if ref($_[0]);
  229.     my $prev = $\;
  230.     $\ = $_[1] if @_ > 1;
  231.     $prev;
  232. }
  233.  
  234. sub input_record_separator {
  235.     carp "input_record_separator is not supported on a per-handle basis"
  236.     if ref($_[0]);
  237.     my $prev = $/;
  238.     $/ = $_[1] if @_ > 1;
  239.     $prev;
  240. }
  241.  
  242. sub input_line_number {
  243.     local $.;
  244.     () = tell qualify($_[0], caller) if ref($_[0]);
  245.     my $prev = $.;
  246.     $. = $_[1] if @_ > 1;
  247.     $prev;
  248. }
  249.  
  250. sub format_page_number {
  251.     my $old;
  252.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  253.     my $prev = $%;
  254.     $% = $_[1] if @_ > 1;
  255.     $prev;
  256. }
  257.  
  258. sub format_lines_per_page {
  259.     my $old;
  260.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  261.     my $prev = $=;
  262.     $= = $_[1] if @_ > 1;
  263.     $prev;
  264. }
  265.  
  266. sub format_lines_left {
  267.     my $old;
  268.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  269.     my $prev = $-;
  270.     $- = $_[1] if @_ > 1;
  271.     $prev;
  272. }
  273.  
  274. sub format_name {
  275.     my $old;
  276.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  277.     my $prev = $~;
  278.     $~ = qualify($_[1], caller) if @_ > 1;
  279.     $prev;
  280. }
  281.  
  282. sub format_top_name {
  283.     my $old;
  284.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  285.     my $prev = $^;
  286.     $^ = qualify($_[1], caller) if @_ > 1;
  287.     $prev;
  288. }
  289.  
  290. sub format_line_break_characters {
  291.     carp "format_line_break_characters is not supported on a per-handle basis"
  292.     if ref($_[0]);
  293.     my $prev = $:;
  294.     $: = $_[1] if @_ > 1;
  295.     $prev;
  296. }
  297.  
  298. sub format_formfeed {
  299.     carp "format_formfeed is not supported on a per-handle basis"
  300.     if ref($_[0]);
  301.     my $prev = $^L;
  302.     $^L = $_[1] if @_ > 1;
  303.     $prev;
  304. }
  305.  
  306. sub formline {
  307.     my $io = shift;
  308.     my $picture = shift;
  309.     local($^A) = $^A;
  310.     local($\) = "";
  311.     formline($picture, @_);
  312.     print $io $^A;
  313. }
  314.  
  315. sub format_write {
  316.     @_ < 3 || croak 'usage: $io->write( [FORMAT_NAME] )';
  317.     if (@_ == 2) {
  318.     my ($io, $fmt) = @_;
  319.     my $oldfmt = $io->format_name($fmt);
  320.     CORE::write($io);
  321.     $io->format_name($oldfmt);
  322.     } else {
  323.     CORE::write($_[0]);
  324.     }
  325. }
  326.  
  327. # XXX undocumented
  328. sub fcntl {
  329.     @_ == 3 || croak 'usage: $io->fcntl( OP, VALUE );';
  330.     my ($io, $op) = @_;
  331.     return fcntl($io, $op, $_[2]);
  332. }
  333.  
  334. # XXX undocumented
  335. sub ioctl {
  336.     @_ == 3 || croak 'usage: $io->ioctl( OP, VALUE );';
  337.     my ($io, $op) = @_;
  338.     return ioctl($io, $op, $_[2]);
  339. }
  340.  
  341. # this sub is for compatability with older releases of IO that used
  342. # a sub called constant to detemine if a constant existed -- GMB
  343. #
  344. # The SEEK_* and _IO?BF constants were the only constants at that time
  345. # any new code should just chech defined(&CONSTANT_NAME)
  346.  
  347. sub constant {
  348.     no strict 'refs';
  349.     my $name = shift;
  350.     (($name =~ /^(SEEK_(SET|CUR|END)|_IO[FLN]BF)$/) && defined &{$name})
  351.     ? &{$name}() : undef;
  352. }
  353.  
  354. # so that flush.pl can be deprecated
  355.  
  356. sub printflush {
  357.     my $io = shift;
  358.     my $old;
  359.     $old = new SelectSaver qualify($io, caller) if ref($io);
  360.     local $| = 1;
  361.     if(ref($io)) {
  362.         print $io @_;
  363.     }
  364.     else {
  365.     print @_;
  366.     }
  367. }
  368.  
  369. 1;
  370.