home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / All.pm < prev    next >
Encoding:
Perl POD Document  |  2004-05-07  |  45.8 KB  |  1,843 lines

  1. package IO::All;
  2. use strict;
  3. use warnings;
  4. use 5.006_001;
  5. our $VERSION = '0.17';
  6. use Spiffy 0.16 '-base', qw(!field);
  7. use Fcntl qw(:DEFAULT :flock);
  8. use Symbol;
  9. use File::Spec;
  10. our @EXPORT = qw(io);
  11.  
  12. spiffy_constructor 'io';
  13.  
  14. #===============================================================================
  15. # Basic Setup
  16. #===============================================================================
  17. sub field;
  18. field autoclose => 1;
  19. field block_size => 1024;
  20. field descriptor => undef;
  21. field domain => undef;
  22. field domain_default => 'localhost';
  23. field flags => {};
  24. field handle => undef;
  25. field io_handle => undef;
  26. field is_open => 0;
  27. field mode => undef;
  28. field name => undef;
  29. field perms => undef;
  30. field port => undef;
  31. field separator => $/;
  32. field tied_file => undef;
  33. field type => undef;
  34. field use_lock => 0;
  35.  
  36. sub proxy; 
  37. proxy 'autoflush';
  38. proxy 'eof';
  39. proxy 'fileno';
  40. proxy 'getc' => '<';
  41. proxy 'seek';
  42. proxy 'stat';
  43. proxy 'string_ref';
  44. proxy 'tell';
  45. proxy 'truncate';
  46.  
  47. sub proxy_open; 
  48. proxy_open print => '>';
  49. proxy_open printf => '>';
  50. proxy_open sysread => O_RDONLY;
  51. proxy_open syswrite => O_CREAT | O_WRONLY;
  52. proxy_open 'recv';
  53. proxy_open 'send';
  54.  
  55. #===============================================================================
  56. # Public class methods
  57. #===============================================================================
  58. sub new {
  59.     my $class = shift;
  60.     my $self = bless Symbol::gensym(), $class;
  61.     my ($args) = $self->parse_arguments(@_);
  62.     tie *$self, $self if $args->{-tie};
  63.     $self->use_lock(1) if $args->{-lock};
  64.     $self->init(@_);
  65. }
  66.  
  67. sub init {
  68.     my $self = shift;
  69.     my ($args, @values) = $self->parse_arguments(@_);
  70.     if (defined $args->{-file_name}) {
  71.         require IO::File;
  72.         $self->io_handle(IO::File->new);
  73.         $self->name($args->{-file_name});
  74.         $self->type('file');
  75.     }
  76.     elsif (defined $args->{-dir_name}) {
  77.         require IO::Dir;
  78.         $self->io_handle(IO::Dir->new);
  79.         $self->name($args->{-dir_name});
  80.         $self->type('dir');
  81.     }
  82.     elsif (defined $args->{-socket_name}) {
  83.         $self->name($args->{-socket_name});
  84.         $self->type('socket');
  85.     }
  86.     elsif (defined $args->{-file_handle}) {
  87.         $self->handle($args->{-file_handle});
  88.         $self->type('file');
  89.     }
  90.     elsif (defined $args->{-dir_handle}) {
  91.         $self->handle($args->{-dir_handle});
  92.         $self->type('dir');
  93.     }
  94.     elsif (defined $args->{-socket_handle}) {
  95.         $self->handle($args->{-socket_handle});
  96.         $self->type('socket');
  97.     }
  98.     unless (defined $self->name or defined $self->handle) {
  99.         if (@values) {
  100.             my $value = shift @values;
  101.             if (ref $value or ref(\ $value) eq 'GLOB') {
  102.                 $self->handle($value);
  103.             }
  104.             else {
  105.                 $self->name($value);
  106.             }
  107.             $self->descriptor($value);
  108.         }
  109.         else {
  110.             $self->temporary_file;
  111.         }
  112.     }
  113.     if (defined (my $name = $self->name)) {
  114.         my $type = 
  115.           $name =~ /(^\|.+|.+\|)$/ ? 'pipe' :
  116.           $name =~ /^[\w\-\.]*:\d{1,5}$/ ? 'socket' :
  117.           -f $name ? 'file' :
  118.           -d $name ? 'dir' :
  119.           -l $name ? 'link' :
  120.           undef;
  121.         $self->type($type);
  122.     }
  123.     return $self;
  124. }
  125.  
  126. #===============================================================================
  127. # Tie Interface
  128. #===============================================================================
  129. sub TIEHANDLE {
  130.     return $_[0] if ref $_[0];
  131.     my $class = shift;
  132.     my $self = bless Symbol::gensym(), $class;
  133.     $self->init(@_);
  134. }
  135.  
  136. sub READLINE {
  137.     goto &getlines if wantarray;
  138.     goto &getline;
  139. }
  140.  
  141. sub DESTROY {
  142.     no warnings;
  143.     my $self = shift;
  144.     untie *$self if tied *$self;
  145. }
  146.  
  147. sub BINMODE { return }
  148.  
  149. {
  150.     no warnings;
  151.     *GETC   = \&getc;
  152.     *PRINT  = \&print;
  153.     *PRINTF = \&printf;
  154.     *READ   = \&read;
  155.     *WRITE  = \&write;
  156.     *SEEK   = \&seek;
  157.     *TELL   = \&getpos;
  158.     *EOF    = \&eof;
  159.     *CLOSE  = \&close;
  160.     *FILENO = \&fileno;
  161. }
  162.  
  163. #===============================================================================
  164. # Public instance methods
  165. #===============================================================================
  166. sub accept {
  167.     my $self = shift;
  168.     $self->assert_open_socket('-listen');
  169.     my ($flags) = $self->parse_arguments(@_);
  170.     my $socket; 
  171.     while (1) {
  172.         $socket = $self->io_handle->accept;
  173.         last unless $flags->{-fork};
  174.         my $pid = fork;
  175.         $self->throw("Unable to fork for IO::All::accept")
  176.           unless defined $pid;
  177.         last unless $pid;
  178.         undef $socket;
  179.     }
  180.     my $io = ref($self)->new(-socket_handle => $socket);
  181.     $io->io_handle($socket);
  182.     $io->is_open(1);
  183.     return $io;
  184. }
  185.  
  186. sub All {
  187.     my $self = shift;
  188.     $self->all('-r');
  189. }
  190.  
  191. sub all {
  192.     my $self = shift;
  193.     my @args = @_;
  194.     my ($flags) = $self->parse_arguments(@args);
  195.     my @all;
  196.     while (my $io = $self->next) {
  197.         push @all, $io;
  198.         push @all, $io->all('-r')
  199.           if $flags->{-r} and $io->type eq 'dir';
  200.     }
  201.     return @all if $flags->{-no_sort};
  202.     return sort {$a->name cmp $b->name} @all;
  203. }
  204.  
  205. sub All_Dirs {
  206.     my $self = shift;
  207.     $self->all_dirs(-r => @_);
  208. }
  209.  
  210. sub all_dirs {
  211.     my $self = shift;
  212.     grep {$_->type eq 'dir'} $self->all(@_);
  213. }
  214.  
  215. sub All_Files {
  216.     my $self = shift;
  217.     $self->all_files(-r => @_);
  218. }
  219.  
  220. sub all_files {
  221.     my $self = shift;
  222.     grep {$_->type eq 'file'} $self->all(@_);
  223. }
  224.  
  225. sub All_Links {
  226.     my $self = shift;
  227.     $self->all_links(-r => @_);
  228. }
  229.  
  230. sub all_links {
  231.     my $self = shift;
  232.     grep {$_->type eq 'link'} $self->all(@_);
  233. }
  234.  
  235. sub append {
  236.     my $self = shift;
  237.     $self->assert_open('>>');
  238.     $self->print(@_);
  239. }
  240.  
  241. sub appendln {
  242.     my $self = shift;
  243.     $self->assert_open('>>');
  244.     $self->println(@_);
  245. }
  246.  
  247. sub backwards {
  248.     my $self = shift;
  249.     *$self->{backwards} = 1;
  250.     return $self;
  251. }
  252.  
  253. sub buffer {
  254.     my $self = shift;
  255.     if (not @_) {
  256.         *$self->{buffer} = do {my $x = ''; \ $x}
  257.           unless exists *$self->{buffer};
  258.         return *$self->{buffer};
  259.     }
  260.     my $buffer_ref = ref($_[0]) ? $_[0] : \ $_[0];
  261.     $$buffer_ref = '' unless defined $$buffer_ref;
  262.     return *$self->{buffer} = $buffer_ref;
  263. }
  264.  
  265. sub clear {
  266.     my $self = shift;
  267.     my $buffer = *$self->{buffer};
  268.     $$buffer = '';
  269. }
  270.  
  271. sub close {
  272.     my $self = shift;
  273.     return unless $self->is_open;
  274.     $self->is_open(0);
  275.     $self->shutdown
  276.       if $self->is_socket;
  277.     my $io_handle = $self->io_handle;
  278.     $self->unlock;
  279.     $self->io_handle(undef);
  280.     $self->mode(undef);
  281.     $io_handle->close(@_);
  282. }
  283.  
  284. sub getline {
  285.     my $self = shift;
  286.     return $self->getline_backwards
  287.       if *$self->{backwards};
  288.     my ($args, @values) = $self->parse_arguments(@_);
  289.     $self->assert_open('<');
  290.     my $line;
  291.     {
  292.         local $/ = @values ? shift(@values) : $self->separator;
  293.         $line = $self->io_handle->getline;
  294.     }
  295.     $self->error_check;
  296.     chomp($line) if $args->{-chomp};
  297.     return defined $line
  298.     ? $line
  299.     : $self->autoclose && $self->close && undef || 
  300.       undef;
  301. }
  302.  
  303. sub getlines {
  304.     my $self = shift;
  305.     return $self->getlines_backwards
  306.       if *$self->{backwards};
  307.     my ($args, @values) = $self->parse_arguments(@_);
  308.     $self->assert_open('<');
  309.     my @lines;
  310.     {
  311.         local $/ = @values ? shift(@values) : $self->separator;
  312.         @lines = $self->io_handle->getlines;
  313.     }
  314.     $self->error_check;
  315.     if ($args->{-chomp}) {
  316.         chomp for @lines;
  317.     }
  318.     return (@lines) or
  319.            $self->autoclose && $self->close && () or
  320.            ();
  321. }
  322.  
  323. sub is_dir {
  324.     my $self = shift;
  325.     ($self->type || '') eq 'dir';
  326. }
  327.  
  328. sub is_file {
  329.     my $self = shift;
  330.     ($self->type || '') eq 'file';
  331. }
  332.  
  333. sub is_link {
  334.     my $self = shift;
  335.     ($self->type || '') eq 'link';
  336. }
  337.  
  338. sub is_socket {
  339.     my $self = shift;
  340.     ($self->type || '') eq 'socket';
  341. }
  342.  
  343. sub is_string {
  344.     my $self = shift;
  345.     ($self->type || '') eq 'string';
  346. }
  347.  
  348. sub length {
  349.     my $self = shift;
  350.     length(${$self->buffer});
  351. }
  352.  
  353. sub next {
  354.     my $self = shift;
  355.     $self->assert_open_dir;
  356.     my $name = '.'; 
  357.     while ($name =~ /^\.{1,2}$/) {
  358.         $name = $self->io_handle->read;
  359.         unless (defined $name) {
  360.             $self->close;
  361.             return;
  362.         }
  363.     }
  364.     return IO::All->new(File::Spec->catfile($self->name, $name));
  365. }
  366.  
  367. sub open {
  368.     my $self = shift;
  369.     $self->throw("IO::All object already open")
  370.       if $self->is_open;
  371.     $self->is_open(1);
  372.     my ($mode, $perms) = @_;
  373.     $self->mode($mode) if defined $mode;
  374.     $self->mode('<') unless defined $self->mode;
  375.     $self->perms($perms) if defined $perms;
  376.     my @args;
  377.     unless ($self->is_dir) {
  378.         push @args, $self->mode;
  379.         push @args, $self->perms if defined $self->perms;
  380.     }
  381.  
  382.     if (defined $self->name) {
  383.         $self->open_name($self->name, @args);
  384.     }
  385.     elsif (defined $self->handle and
  386.            not $self->io_handle->opened
  387.           ) {
  388.         # XXX Not tested
  389.         $self->io_handle->fdopen($self->handle, @args);
  390.     }
  391.     return $self;
  392. }
  393.  
  394. sub println {
  395.     my $self = shift;
  396.     $self->print(map {/\n\z/ ? ($_) : ($_, "\n")} @_);
  397. }
  398.  
  399. sub read {
  400.     my $self = shift;
  401.     $self->assert_open('<');
  402.     my $length = (@_ or $self->type eq 'dir')
  403.     ? $self->io_handle->read(@_)
  404.     : $self->io_handle->read(
  405.         ${$self->buffer}, 
  406.         $self->block_size, 
  407.         $self->length,
  408.       );
  409.     $self->error_check;
  410.     return $length || $self->autoclose && $self->close && 0;
  411. }
  412.  
  413. {
  414.     no warnings;
  415.     *readline = \&getline;
  416. }
  417.  
  418. sub rmdir {
  419.     my $self = shift;
  420.     rmdir $self->name;
  421. }
  422.  
  423. sub shutdown {
  424.     my $self = shift;
  425.     my $how = @_ ? shift : 2;
  426.     $self->io_handle->shutdown(2);
  427. }
  428.  
  429. sub slurp {
  430.     my $self = shift;
  431.     $self->assert_open('<');
  432.     local $/;
  433.     my $slurp = $self->io_handle->getline;
  434.     $self->error_check;
  435.     $self->autoclose && $self->close;
  436.     return $slurp unless wantarray;
  437.     my $separator = $self->separator;
  438.     split /(?<=\Q$separator\E)/, $slurp;
  439. }
  440.  
  441. sub temporary_file {
  442.     my $self = shift;
  443.     require IO::File;
  444.     my $temp_file = IO::File::new_tmpfile()
  445.       or $self->throw("Can't create temporary file");
  446.     $self->io_handle($temp_file);
  447.     $self->error_check;
  448.     $self->autoclose(0);
  449.     $self->is_open(1);
  450. }
  451.  
  452. sub unlink {
  453.     my $self = shift;
  454.     unlink $self->name;
  455. }
  456.  
  457. sub unlock {
  458.     my $self = shift;
  459.     my $io_handle = $self->io_handle;
  460.     if ($self->use_lock) {
  461.         flock $io_handle, LOCK_UN;
  462.     }
  463. }
  464.  
  465. sub write {
  466.     my $self = shift;
  467.     $self->assert_open_file('>');
  468.     my $length = @_
  469.     ? $self->io_handle->write(@_)
  470.     : $self->io_handle->write(${$self->buffer}, $self->length);
  471.     $self->error_check;
  472.     $self->clear unless @_;
  473.     return $length;
  474. }
  475.  
  476. #===============================================================================
  477. # Implementation methods. Subclassable.
  478. #===============================================================================
  479. sub throw {
  480.     my $self = shift;
  481.     require Carp;
  482. #     Carp::croak(@_);
  483.     Carp::confess(@_);
  484. }
  485.  
  486. #===============================================================================
  487. # Private instance methods
  488. #===============================================================================
  489. sub assert_dirpath {
  490.     my $self = shift;
  491.     my $dir_name = $self->name
  492.       or $self->throw("No directory name for IO::All object");
  493.     return $dir_name if -d $dir_name or
  494.       mkdir($self->name, $self->perms || 0755) or
  495.       do {
  496.           require File::Path;
  497.           mkpath($dir_name);
  498.       } or
  499.       $self->throw("Can't make $dir_name"); 
  500. }
  501.  
  502. sub assert_open {
  503.     my $self = shift;
  504.     return if $self->is_open;
  505.     my $type = $self->type || '';
  506.     return $self->assert_open_file(@_) unless $type; 
  507.     my $method = "assert_open_$type";
  508.     return $self->$method(@_);
  509. }
  510.  
  511. sub assert_open_backwards {
  512.     my $self = shift;
  513.     return if $self->is_open;
  514.     require File::ReadBackwards;
  515.     my $file_name = $self->name;
  516.     my $io_handle = File::ReadBackwards->new($file_name)
  517.       or $self->throw("Can't open $file_name for backwards:\n$!");
  518.     $self->io_handle($io_handle);
  519.     $self->is_open(1);
  520. }
  521.  
  522. sub assert_open_dir {
  523.     my $self = shift;
  524.     return if $self->is_open;
  525.     require IO::Dir;
  526.     $self->type('dir');
  527.     $self->io_handle(IO::Dir->new)
  528.       unless defined $self->io_handle;
  529.     $self->open;
  530. }
  531.  
  532. sub assert_open_file {
  533.     my $self = shift;
  534.     return if $self->is_open;
  535.     $self->type('file');
  536.     require IO::File;
  537.     $self->io_handle(IO::File->new)
  538.       unless defined $self->io_handle;
  539.     $self->mode(shift) unless $self->mode;
  540.     $self->open;
  541. }
  542.  
  543. sub assert_open_pipe {
  544.     my $self = shift;
  545.     return if $self->is_open;
  546.     require IO::Handle;
  547.     $self->io_handle(IO::Handle->new)
  548.       unless defined $self->io_handle;
  549.     my $command = $self->name;
  550.     $command =~ s/(^\||\|$)//;
  551.     my $mode = shift;
  552.     my $pipe_mode = 
  553.       $mode eq '>' ? '|-' :
  554.       $mode eq '<' ? '-|' :
  555.       $self->throw("Invalid usage mode '$mode' for pipe");
  556.     CORE::open($self->io_handle, $pipe_mode, $command);
  557. }
  558.  
  559. sub assert_open_socket {
  560.     my $self = shift;
  561.     return if $self->is_open;
  562.     $self->type('socket');
  563.     $self->is_open(1);
  564.     require IO::Socket;
  565.     my ($flags) = $self->parse_arguments(@_);
  566.     $self->get_socket_domain_port;
  567.     my @args = $flags->{-listen}
  568.     ? (
  569.         LocalHost => $self->domain,
  570.         LocalPort => $self->port,
  571.         Proto => 'tcp',
  572.         Listen => 1,
  573.         Reuse => 1,
  574.     )
  575.     : (
  576.         PeerAddr => $self->domain,
  577.         PeerPort => $self->port,
  578.         Proto => 'tcp',
  579.     );
  580.     my $socket = IO::Socket::INET->new(@args)
  581.       or $self->throw("Can't open socket");
  582.     $self->io_handle($socket);
  583. }
  584.  
  585. sub assert_tied_file {
  586.     my $self = shift;
  587.     return $self->tied_file || do {
  588.         eval {require Tie::File};
  589.         $self->throw("Tie::File required for file array operations") if $@;
  590.         my $array_ref = do { my @array; \@array };
  591.         tie @$array_ref, 'Tie::File', $self->name;
  592.         $self->tied_file($array_ref);
  593.     };
  594. }
  595.  
  596. sub boolean_arguments {
  597.     my $self = shift;
  598.     (
  599.         qw(
  600.             -a -r 
  601.             -lock -chomp -fork -tie
  602.             -no_sort -listen
  603.         ),
  604.         $self->SUPER::boolean_arguments,
  605.     )
  606. }
  607.  
  608. sub error_check {
  609.     my $self = shift;
  610.     return unless $self->io_handle->can('error');
  611.     return unless $self->io_handle->error;
  612.     $self->throw($!);
  613. }
  614.  
  615. sub copy {
  616.     my $self = shift;
  617.     my $copy;
  618.     for (keys %{*$self}) {
  619.         $copy->{$_} = *$self->{$_};
  620.     }
  621.     $copy->{io_handle} = 'defined'
  622.       if defined $copy->{io_handle};
  623.     return $copy;
  624. }
  625.  
  626. sub get_socket_domain_port {
  627.     my $self = shift;
  628.     my ($domain, $port);
  629.     ($domain, $port) = split /:/, $self->name
  630.       if defined $self->name;
  631.     $self->domain($domain) unless defined $self->domain;
  632.     $self->domain($self->domain_default) unless $self->domain;
  633.     $self->port($port) unless defined $self->port;
  634. }
  635.  
  636. sub getline_backwards {
  637.     my $self = shift;
  638.     $self->assert_open_backwards;
  639.     return $self->io_handle->readline;
  640. }
  641.  
  642. sub getlines_backwards {
  643.     my $self = shift;
  644.     my @lines;
  645.     while (defined (my $line = $self->getline_backwards)) {
  646.         push @lines, $line;
  647.     }
  648.     return @lines;
  649. }
  650.  
  651. sub lock {
  652.     my $self = shift;
  653.     return unless $self->use_lock;
  654.     my $io_handle = $self->io_handle;
  655.     my $flag = $self->mode =~ /^>>?$/
  656.     ? LOCK_EX
  657.     : LOCK_SH;
  658.     flock $io_handle, $flag;
  659. }
  660.  
  661. sub open_file {
  662.     my $self = shift;
  663.     require IO::File;
  664.     my $handle = IO::File->new;
  665.     $self->io_handle($handle);
  666.     $handle->open(@_) 
  667.       or $self->throw($self->open_file_msg);
  668.     $self->lock;
  669. }
  670.  
  671. my %mode_msg = (
  672.     '>' => 'output',
  673.     '<' => 'input',
  674.     '>>' => 'append',
  675. );
  676. sub open_file_msg {
  677.     my $self = shift;
  678.     my $name = defined $self->name
  679.       ? " '" . $self->name . "'"
  680.       : '';
  681.     my $direction = defined $mode_msg{$self->mode}
  682.       ? ' for ' . $mode_msg{$self->mode}
  683.       : '';
  684.     return qq{Can't open file$name$direction:\n$!};
  685. }
  686.  
  687. sub open_dir {
  688.     my $self = shift;
  689.     require IO::Dir;
  690.     my $handle = IO::Dir->new;
  691.     $self->io_handle($handle);
  692.     $handle->open(@_)
  693.       or $self->throw($self->open_dir_msg);
  694. }
  695.  
  696. sub open_dir_msg {
  697.     my $self = shift;
  698.     my $name = defined $self->name
  699.       ? " '" . $self->name . "'"
  700.       : '';
  701.     return qq{Can't open directory$name:\n$!};
  702. }
  703.  
  704. sub open_name {
  705.     my $self = shift;
  706.     return $self->open_std if $self->descriptor eq '-';
  707.     return $self->open_string if $self->descriptor eq '$';
  708.     return $self->open_file(@_) unless defined $self->type;
  709.     return $self->open_file(@_) if $self->type eq 'file';
  710.     return $self->open_dir(@_) if $self->type eq 'dir';
  711.     return if $self->type eq 'socket';
  712.     return $self->open_file(@_);
  713. }
  714.  
  715. sub open_std {
  716.     my $self = shift;
  717.     my $fileno = $self->mode eq '>'
  718.     ? fileno(STDOUT)
  719.     : fileno(STDIN);
  720.     $self->io_handle->fdopen($fileno, $self->mode);
  721. }
  722.  
  723. sub open_string {
  724.     my $self = shift;
  725.     require IO::String;
  726.     $self->io_handle(IO::String->new);
  727. }
  728.  
  729. sub paired_arguments {
  730.     qw( 
  731.         -errors
  732.         -file_name -file_handle 
  733.         -dir_name -dir_handle 
  734.         -socket_name -socket_handle 
  735.     )
  736. }
  737.  
  738. #===============================================================================
  739. # Closure generating functions
  740. #===============================================================================
  741. sub field {
  742.     my $package = caller;
  743.     my ($field, $default) = @_;
  744.     no strict 'refs';
  745.     return if defined &{"${package}::$field"};
  746.     *{"${package}::$field"} =
  747.       sub {
  748.           my $self = shift;
  749.           unless (exists *$self->{$field}) {
  750.               *$self->{$field} = 
  751.                 ref($default) eq 'ARRAY' ? [] :
  752.                 ref($default) eq 'HASH' ? {} : 
  753.                 $default;
  754.           }
  755.           return *$self->{$field} unless @_;
  756.           *$self->{$field} = shift;
  757.       };
  758. }
  759.  
  760. sub proxy {
  761.     my $package = caller;
  762.     my ($proxy) = @_;
  763.     no strict 'refs';
  764.     return if defined &{"${package}::$proxy"};
  765.     *{"${package}::$proxy"} =
  766.       sub {
  767.           my $self = shift;
  768.           my @return = $self->io_handle->$proxy(@_);
  769.           $self->error_check;
  770.           wantarray ? @return : $return[0];
  771.       };
  772. }
  773.  
  774. sub proxy_open {
  775.     my $package = caller;
  776.     my ($proxy, @args) = @_;
  777.     no strict 'refs';
  778.     return if defined &{"${package}::$proxy"};
  779.     *{"${package}::$proxy"} =
  780.       sub {
  781.           my $self = shift;
  782.           $self->assert_open(@args);
  783.           my @return = $self->io_handle->$proxy(@_);
  784.           $self->error_check;
  785.           wantarray ? @return : $return[0];
  786.       };
  787. }
  788.  
  789. #===============================================================================
  790. # overloading
  791. #===============================================================================
  792. my $old_warn_handler = $SIG{__WARN__}; 
  793. $SIG{__WARN__} = sub { 
  794.     if ($_[0] !~ /^Useless use of .+ \(.+\) in void context/) {
  795.         goto &$old_warn_handler if $old_warn_handler;
  796.         warn(@_);
  797.     }
  798. };
  799.     
  800. use overload '""' => 'overload_stringify';
  801. use overload '|' => 'overload_bitwise_or';
  802. use overload '<<' => 'overload_left_bitshift';
  803. use overload '>>' => 'overload_right_bitshift';
  804. use overload '<' => 'overload_less_than';
  805. use overload '>' => 'overload_greater_than';
  806. use overload '${}' => 'overload_string_deref';
  807. use overload '@{}' => 'overload_array_deref';
  808. use overload '%{}' => 'overload_hash_deref';
  809. use overload '&{}' => 'overload_code_deref';
  810.  
  811. sub overload_bitwise_or { shift->overload_handler(@_, '|') }
  812. sub overload_left_bitshift { shift->overload_handler(@_, '<<') }
  813. sub overload_right_bitshift { shift->overload_handler(@_, '>>') }
  814. sub overload_less_than { shift->overload_handler(@_, '<') }
  815. sub overload_greater_than { shift->overload_handler(@_, '>') }
  816. sub overload_string_deref { shift->overload_handler(@_, '${}') }
  817. sub overload_array_deref { shift->overload_handler(@_, '@{}') }
  818. sub overload_hash_deref { shift->overload_handler(@_, '%{}') }
  819. sub overload_code_deref { shift->overload_handler(@_, '&{}') }
  820.  
  821. sub overload_table {
  822.     my $self = shift;
  823.     *$self->{overload_table} ||= {
  824.         'file < scalar' => 'overload_scalar_to_file',
  825.         'file > scalar' => 'overload_file_to_scalar',
  826.  
  827.         'file << scalar' => 'overload_scalar_addto_file',
  828.         'file >> scalar' => 'overload_file_addto_scalar',
  829.  
  830.         'file > file' => 'overload_file_to_file',
  831.         'file < file' => 'overload_file_from_file',
  832.         'file >> file' => 'overload_file_addto_file',
  833.         'file << file' => 'overload_file_addfrom_file',
  834.  
  835.         '${} file' => 'overload_file_as_scalar',
  836.         '@{} file' => 'overload_file_as_array',
  837.         '@{} dir' => 'overload_dir_as_array',
  838.         '%{} dir' => 'overload_dir_as_hash',
  839.         
  840.         'file | scalar' => 'overload_pipe_to',
  841.         'file | scalar swap' => 'overload_pipe_from',
  842.         
  843.         'socket < file' => 'overload_file_to_socket',
  844.         'file > socket' => 'overload_file_to_socket',
  845.         '&{} socket' => 'overload_socket_as_code',
  846.     };
  847. }
  848.  
  849. sub overload_handler {
  850.     my ($self) = @_;
  851.     my $method = $self->get_overload_method(@_);
  852.     $self->$method(@_);
  853. }
  854.  
  855. my $op_swap = {
  856.     '>' => '<', '>>' => '<<',
  857.     '<' => '>', '<<' => '>>',
  858. };
  859. sub get_overload_method {
  860.     my ($self, $arg1, $arg2, $swap, $operator) = @_;
  861.     if ($swap) {
  862.         $operator = $op_swap->{$operator} || $operator;
  863.     }
  864.     my $arg1_type = $self->get_argument_type($arg1);
  865.     my $key = ($operator =~ /\{\}$/)
  866.     ? "$operator $arg1_type"
  867.     : do {
  868.         my $arg2_type = $self->get_argument_type($arg2);
  869.         "$arg1_type $operator $arg2_type";
  870.     };
  871.     my $table = $self->overload_table;
  872.     return defined $table->{$key} 
  873.       ? $table->{$key}
  874.       : $self->overload_undefined($key);
  875. }
  876.  
  877. sub get_argument_type {
  878.     my $self = shift;
  879.     my $argument = shift;
  880.     my $ref = ref($argument);
  881.     return 'scalar' unless $ref;
  882.     return 'code' if $ref eq 'CODE';
  883.     return 'array' if $ref eq 'ARRAY';
  884.     return 'hash' if $ref eq 'HASH';
  885.     return 'ref' unless $argument->isa('IO::All');
  886.     my $type = $argument->type;
  887.     return defined $argument->name ? 'file' : 'unknown' 
  888.       unless defined $type;
  889.     return 'file' if $type eq 'pipe';
  890.     return $type;
  891. }
  892.  
  893. sub overload_stringify {
  894.     my $self = shift;
  895.     my $name = $self->name;
  896.     return defined($name) ? $name : overload::StrVal($self);
  897. }
  898.  
  899. sub overload_undefined {
  900.     my $self = shift;
  901.     my $key = shift;
  902.     warn "Undefined behavior for overloaded IO::All operation: '$key'";
  903.     return 'overload_noop';
  904. }
  905.  
  906. sub overload_noop {
  907.     return;
  908. }
  909.  
  910. sub overload_scalar_addto_file {
  911.     $_[1]->append($_[2]);
  912.     $_[1];
  913. }
  914.  
  915. sub overload_file_addto_file {
  916.     $_[2]->append(scalar $_[1]->slurp);
  917. }
  918.  
  919. sub overload_file_addfrom_file {
  920.     $_[1]->append(scalar $_[2]->slurp);
  921. }
  922.  
  923. sub overload_file_to_file {
  924.     require File::Copy;
  925.     File::Copy::copy($_[1]->name, $_[2]->name);
  926. }
  927.  
  928. sub overload_file_from_file {
  929.     require File::Copy;
  930.     File::Copy::copy($_[2]->name, $_[1]->name);
  931. }
  932.  
  933. sub overload_dir_as_array {
  934.     [ $_[1]->all ];
  935. }
  936.  
  937. sub overload_dir_as_hash {
  938.     +{ 
  939.         map {
  940.             (my $name = $_->name) =~ s/.*[\/\\]//;
  941.             ($name, $_);
  942.         } $_[1]->all 
  943.     };
  944. }
  945.  
  946. sub overload_file_as_array {
  947.     $_[1]->assert_tied_file;
  948. }
  949.  
  950. sub overload_scalar_to_file {
  951.     local $\;
  952.     $_[1]->print($_[2]);
  953.     $_[1];
  954. }
  955.  
  956. sub overload_file_as_scalar {
  957.     my $slurp = $_[1]->slurp;
  958.     return \$slurp;
  959. }
  960.  
  961. sub overload_file_to_scalar {
  962.     $_[2] = $_[1]->slurp;
  963. }
  964.  
  965. sub overload_file_addto_scalar {
  966.     $_[2] .= $_[1]->slurp;
  967. }
  968.  
  969. sub overload_socket_as_code {
  970.     my $self = shift;
  971.     sub {
  972.         my $coderef = shift;
  973.         while ($self->is_open) {
  974.             $_ = $self->getline;
  975.             &$coderef($self);
  976.         }
  977.     }
  978. }
  979.  
  980. sub overload_file_to_socket {
  981.     local $\;
  982.     $_[1]->print($_[2]->slurp);
  983.     $_[1]->close;
  984. }
  985.  
  986. 1;
  987. __END__
  988.  
  989. =head1 NAME
  990.  
  991. IO::All - IO::All of it to Graham and Damian!
  992.  
  993. =head1 NOTE
  994.  
  995. If you've just read the perl.com article at
  996. L<http://www.perl.com/pub/a/2004/03/12/ioall.html>, there have already been
  997. major additions thanks to the great feedback I've gotten from the Perl
  998. community. Be sure and read the latest doc. Things are changing fast.
  999.  
  1000. Many of the changes have to do with operator overloading for IO::All objects,
  1001. which results in some fabulous new idioms.
  1002.  
  1003. =head1 SYNOPSIS
  1004.  
  1005.     use IO::All;
  1006.  
  1007.     my $my_stuff = io('./mystuff')->slurp;  # Read a file
  1008.     my $more_stuff < io('./morestuff');     # Read another file
  1009.  
  1010.     io('./allstuff')->print($my_stuff, $more_stuff);  # Write to new file
  1011.  
  1012. or like this:
  1013.  
  1014.     io('./mystuff') > io('./allstuff');
  1015.     io('./morestuff') >> io('./allstuff');
  1016.  
  1017. or:
  1018.  
  1019.     my $stuff < io('./mystuff');
  1020.     io('./morestuff') >> $stuff;
  1021.     io('./allstuff') << $stuff;
  1022.  
  1023. or:
  1024.  
  1025.     ${io('./stuff')} . ${io('./morestuff')} > io('./allstuff');
  1026.  
  1027. =head1 SYNOPSIS II
  1028.  
  1029.     use IO::All;
  1030.  
  1031.     # Print name and first line of all files in a directory
  1032.     my $dir = io('./mydir'); 
  1033.     while (my $io = $dir->next) {
  1034.         print $io->name, ' - ', $io->getline
  1035.           if $io->is_file;
  1036.     }
  1037.  
  1038.     # Print name of all files recursively
  1039.     print "$_\n" for io('./mydir')->All_Files;
  1040.  
  1041. =head1 SYNOPSIS III
  1042.  
  1043.     use IO::All;
  1044.     
  1045.     # Various ways to copy STDIN to STDOUT
  1046.     io('-') > io('-');
  1047.     
  1048.     io('-') < io('-');
  1049.     
  1050.     io('-')->print(io('-')->slurp);
  1051.     
  1052.     my $stdin = io('-');
  1053.     my $stdout = io('-');
  1054.     $stdout->buffer($stdin->buffer);
  1055.     $stdout->write while $stdin->read;
  1056.     
  1057.     # Copy STDIN to a String File one line at a time
  1058.     my $stdin = io('-');
  1059.     my $string_out = io('$');
  1060.     while (my $line = $stdin->getline) {
  1061.         $string_out->print($line);
  1062.     }
  1063.  
  1064. =head1 SYNOPSIS IV
  1065.  
  1066.     use IO::All;
  1067.     
  1068.     # A forking socket server that writes to a log
  1069.     my $server = io('server.com:9999');
  1070.     my $socket = $server->accept('-fork');
  1071.     while (my $msg = $socket->getline) {
  1072.         io('./mylog')->appendln(localtime() . ' - $msg');
  1073.     }
  1074.     $socket->close;
  1075.  
  1076.     # A single statement web server for static files and cgis too
  1077.     io(":8080")->accept("-fork")->
  1078.       (sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / });
  1079.  
  1080. =head1 SYNOPSIS V
  1081.  
  1082.     use IO::All;
  1083.  
  1084.     # Write some data to a temporary file and retrieve all the paragraphs.
  1085.     my $temp = io;
  1086.     $temp->print($data);
  1087.     $temp->seek(0, 0);
  1088.     my @paragraphs = $temp->getlines('');
  1089.  
  1090. =head1 DESCRIPTION
  1091.  
  1092. "Graham Barr for doing it all. Damian Conway for doing it all different."
  1093.  
  1094. IO::All combines all of the best Perl IO modules into a single Spiffy
  1095. object oriented interface to greatly simplify your everyday Perl IO
  1096. idioms. It exports a single function called C<io>, which returns a new
  1097. IO::All object. And that object can do it all!
  1098.  
  1099. The IO::All object is a proxy for IO::File, IO::Dir, IO::Socket,
  1100. IO::String, Tie::File and File::ReadBackwards. You can use most of the
  1101. methods found in these classes and in IO::Handle (which they all inherit
  1102. from). IO::All is easily subclassable. You can override any methods and
  1103. also add new methods of your own.
  1104.  
  1105. Optionally, every IO::All object can be tied to itself. This means that
  1106. you can use most perl IO builtins on it: readline, <>, getc, print,
  1107. printf, syswrite, sysread, close. (Due to an unfortunate bug in Perl
  1108. 5.8.0 only, this option is turned off by default. See below.)
  1109.  
  1110. The distinguishing magic of IO::All is that it will automatically
  1111. open (and close) files, directories, sockets and io-strings for you.
  1112. You never need to specify the mode ('<', '>>', etc), since it is
  1113. determined by the usage context. That means you can replace this:
  1114.  
  1115.     open STUFF, '<', './mystuff'
  1116.       or die "Can't open './mystuff' for input:\n$!";
  1117.     local $/;
  1118.     my $stuff = <STUFF>;
  1119.     close STUFF;
  1120.  
  1121. with this:
  1122.  
  1123.     my $stuff < io('./mystuff');
  1124.  
  1125. And that is a B<good thing>!
  1126.  
  1127. =head1 USAGE
  1128.  
  1129. The use statement for IO::All can be passed several options:
  1130.  
  1131.     use IO::All;
  1132.     use IO::All '-base';
  1133.     use IO::All '-tie';
  1134.     use IO::All '-lock';
  1135.  
  1136. With the exception of '-base', these options are simply defaults that
  1137. are passed on to every C<io> function within the program.
  1138.  
  1139. =head2 Options
  1140.  
  1141. =over 4
  1142.  
  1143. =item * -base
  1144.  
  1145. Boolean. This option inherited from Spiffy, make the current package a
  1146. subclass of IO::All (which is a subclass of Spiffy). The option is also
  1147. available to packages that want to use the new subclass as a base class.
  1148.  
  1149.     package IO::Different;
  1150.     use IO::All '-base';
  1151.  
  1152. =over 4
  1153.  
  1154. =item * -tie
  1155.  
  1156. Boolean. This option says that all objects created by the C<io> function
  1157. should be tied to themselves.
  1158.  
  1159.     use IO::All qw(-tie);
  1160.     my $io = io('file1');
  1161.     my @lines = <$io>;
  1162.     $io->close;
  1163.  
  1164. As you can see, you can use both method calls and builtin functions on
  1165. the same object.
  1166.  
  1167. NOTE: If you use the C<-tie> option with Perl 5.8.0, you need may need
  1168. to call the close function explicitly. Due to a bug, these objects will
  1169. not go out of scope properly, thus the files opened for output will not
  1170. be closed. This is not a problem in Perl 5.6.1 or 5.8.1 and greater.
  1171.  
  1172. =item * -lock
  1173.  
  1174. Boolean. This option tells the object to flock the filehandle after open.
  1175.  
  1176. =back
  1177.  
  1178. =head1 COOKBOOK
  1179.  
  1180. This section describes some various things that you can easily cook up
  1181. with IO::All.
  1182.  
  1183. =head2 Operator Overloading
  1184.  
  1185. IO::All objects stringify to their file or directory name. Here we print the
  1186. contents of a directory:
  1187.  
  1188.     perl -MIO::All -le 'print for io(".")->all'
  1189.  
  1190. '>' and '<' move data between strings and files:
  1191.  
  1192.     $content1 < io('file1');
  1193.     $content1 > io('file2');
  1194.     io('file2') > $content3;
  1195.     io('file3') < $content3;
  1196.     io('file3') > io('file4');
  1197.     io('file5') < io('file4');
  1198.  
  1199. '>>' and '<<' do the same thing except the recipent string or file is
  1200. appended to.
  1201.  
  1202. An IO::All file used as an array reference becomes tied using Tie::File:
  1203.  
  1204.     $file = io('file');
  1205.     # Print last line of file
  1206.     print $file->[-1];
  1207.     # Insert new line in middle of file
  1208.     $file->[$#$file / 2] = 'New line';
  1209.  
  1210. IO::All directories used as hashes have file names as keys, and IO::All
  1211. objects as values:
  1212.  
  1213.     print io('dir')->{'foo.txt'}->slurp;
  1214.  
  1215. Files used as scalar references get slurped:
  1216.  
  1217.     print ${io('dir')->{'foo.txt'}};
  1218.  
  1219. =head2 File Locking
  1220.  
  1221. IO::All makes it very easy to lock files. Just use the C<-lock> flag. Here's a
  1222. standalone program that demonstrates locking for both write and read:
  1223.  
  1224.     use IO::All;
  1225.     my $io1 = io(-lock => 'myfile');
  1226.     $io1->println('line 1');
  1227.  
  1228.     fork or do {
  1229.         my $io2 = io(-lock => 'myfile');
  1230.         print $io2->slurp;
  1231.         exit;
  1232.     };
  1233.  
  1234.     sleep 1;
  1235.     $io1->println('line 2');
  1236.     $io1->println('line 3');
  1237.     $io1->unlock;
  1238.  
  1239. There are a lot of subtle things going on here. An exclusive lock is
  1240. issued for C<$io1> on the first C<println>. That's because the file
  1241. isn't actually opened until the first IO operation.
  1242.  
  1243. When the child process tries to read the file using C<$io2>, there is
  1244. a shared lock put on it. Since C<$io1> has the exclusive lock, the
  1245. slurp blocks.
  1246.  
  1247. The parent process sleeps just to make sure the child process gets a
  1248. chance. The parent needs to call C<unlock> or C<close> to release the
  1249. lock. If all goes well the child will print 3 lines.
  1250.  
  1251. =head2 Round Robin
  1252.  
  1253. This simple example will read lines from a file forever. When the last
  1254. line is read, it will reopen the file and read the first one again.
  1255.  
  1256.     my $io = io('file1.txt');
  1257.     $io->autoclose(1);
  1258.     while (my $line = $io->getline || $io->getline) {
  1259.         print $line;
  1260.     }
  1261.  
  1262. =head2 Reading Backwards
  1263.  
  1264. If you call the C<backwards()> method on an IO::All object, the
  1265. C<getline()> and C<getlines()> will work in reverse. They will read the
  1266. lines in the file from the end to the beginning.
  1267.  
  1268.     my @reversed;
  1269.     my $io = io('file1.txt');
  1270.     $io->backwards;
  1271.     while (my $line = $io->getline) {
  1272.         push @reversed, $line;
  1273.     }
  1274.  
  1275. or more simply:
  1276.  
  1277.     my @reversed = io('file1.txt')->backwards->getlines;
  1278.  
  1279. The C<backwards()> method returns the IO::All object so that you can
  1280. chain the calls.
  1281.  
  1282. NOTE: This operation requires that you have the File::ReadBackwards 
  1283. module installed.
  1284.     
  1285. =head2 Client/Server Sockets
  1286.  
  1287. IO::All makes it really easy to write a forking socket server and a
  1288. client to talk to it.
  1289.  
  1290. In this example, a server will return 3 lines of text, to every client
  1291. that calls it. Here is the server code:
  1292.  
  1293.     use IO::All;
  1294.  
  1295.     my $socket = io(':12345')->accept('-fork');
  1296.     $socket->print($_) while <DATA>;
  1297.     $socket->close;
  1298.  
  1299.     __DATA__
  1300.     On your mark,
  1301.     Get set,
  1302.     Go!
  1303.  
  1304. Here is the client code:
  1305.  
  1306.     use IO::All;
  1307.  
  1308.     my $io = io('localhost:12345');
  1309.     print while $_ = $io->getline;
  1310.  
  1311. You can run the server once, and then run the client repeatedly (in
  1312. another terminal window). It should print the 3 data lines each time.
  1313.  
  1314. Note that it is important to close the socket if the server is forking,
  1315. or else the socket won't go out of scope and close.
  1316.  
  1317. =head2 File Subclassing
  1318.  
  1319. Subclassing is easy with IO::All. Just create a new module and use
  1320. IO::All as the base class. Since IO::All is a Spiffy module, you do it
  1321. like this:
  1322.  
  1323.     package NewModule;
  1324.     use IO::All '-base';
  1325.  
  1326. You need to do it this way so that IO::All will export the C<io> function.
  1327. Here is a simple recipe for subclassing:
  1328.  
  1329. IO::Dumper inherits everything from IO::All and adds an extra method
  1330. called C<dump()>, which will dump a data structure to the file we
  1331. specify in the C<io> function. Since it needs Data::Dumper to do the
  1332. dumping, we override the C<open> method to C<require Data::Dumper> and
  1333. then pass control to the real C<open>.
  1334.  
  1335. First the code using the module:
  1336.  
  1337.     use IO::Dumper;
  1338.     
  1339.     io('./mydump')->dump($hash);
  1340.  
  1341. And next the IO::Dumper module itself:
  1342.  
  1343.     package IO::Dumper;
  1344.     use IO::All '-base';
  1345.     use Data::Dumper;
  1346.     
  1347.     sub dump {
  1348.         my $self = shift;
  1349.         $self->print(Data::Dumper::Dumper(@_));
  1350.         return $self;
  1351.     }
  1352.     
  1353.     1;
  1354.  
  1355. =head2 Inline Subclassing
  1356.  
  1357. This recipe does the same thing as the previous one, but without needing
  1358. to write a separate module. The only real difference is the first line.
  1359. Since you don't "use" IO::Dumper, you need to still call its C<import>
  1360. method manually.
  1361.  
  1362.     IO::Dumper->import;
  1363.     io('./mydump')->dump($hash);
  1364.     
  1365.     package IO::Dumper;
  1366.     use IO::All '-base';
  1367.     use Data::Dumper;
  1368.     
  1369.     sub dump {
  1370.         my $self = shift;
  1371.         $self->print(Data::Dumper::Dumper(@_));
  1372.         return $self;
  1373.     }
  1374.     
  1375. =head1 OPERATION NOTES
  1376.  
  1377. =over 4
  1378.  
  1379. =item *
  1380.  
  1381. IO::All will automatically be opened when the first read or write
  1382. happens. Mode is determined heuristically unless specified explicitly.
  1383.  
  1384. =item *
  1385.  
  1386. For input, IO::All objects will automatically be closed after EOF (or
  1387. EOD). For output, the object closes when it goes out of scope.
  1388.  
  1389. To keep input objects from closing at EOF, do this:
  1390.  
  1391.     $io->autoclose(0);
  1392.  
  1393. =item * 
  1394.  
  1395. You can always call C<open> and C<close> explicitly, if you need that
  1396. level of control.
  1397.  
  1398. =back
  1399.  
  1400. =head1 CONSTRUCTOR
  1401.  
  1402. NOTE: The C<io> function takes all the same parameters as C<new>.
  1403.  
  1404. =over 4
  1405.  
  1406. =item * new()
  1407.  
  1408.     new(file_descriptor,
  1409.         '-',
  1410.         '=',
  1411.         '$',
  1412.         -file_name => $file_name,
  1413.         -file_handle => $file_handle,
  1414.         -dir_name => $directory_name,
  1415.         -dir_handle => $directory_handle,
  1416.         '-tie',
  1417.        );
  1418.             
  1419. File descriptor is a file/directory name or file/directory handle or
  1420. anything else that can be used in IO operations. 
  1421.  
  1422. IO::All will use STDIN or STDOUT (depending on context) if file
  1423. descriptor is '-'. It will use an IO::String object if file
  1424. descriptor is '$'.
  1425.  
  1426. If file_descriptor is missing and neither C<-file_handle> nor
  1427. C<-dir_handle> is specified, IO::All will create a temporary file
  1428. which will be opened for both input and output.
  1429.  
  1430. C<-tie> uses the tie interface for a single object.
  1431.  
  1432. =back
  1433.  
  1434. =head1 INSTANCE METHODS
  1435.  
  1436. IO::All provides lots of methods for making your daily programming tasks
  1437. simpler. If you can't find what you need, just subclass IO::All and
  1438. add your own.
  1439.  
  1440. =over 4
  1441.  
  1442. =item * accept()
  1443.  
  1444. For sockets. Opens a server socket (LISTEN => 1, REUSE => 1). Returns an
  1445. IO::All socket object that you are listening on.
  1446.  
  1447. If the '-fork' option is specified, the process will automatically be forked
  1448. for every connection.
  1449.  
  1450. =item * all()
  1451.  
  1452. Return a list of IO::All objects for all files and subdirectories in a
  1453. directory. 
  1454.  
  1455. '.' and '..' are excluded.
  1456.  
  1457. The C<-r> flag can be used to get all files and subdirectories recursively.
  1458.  
  1459. The items returned are sorted by name unless the C<-no_sort> flag is used.
  1460.  
  1461. =item * All()
  1462.  
  1463. Same as C<all('-r')>.
  1464.  
  1465. =item * all_dirs()
  1466.  
  1467. Same as C<all()>, but only return directories.
  1468.  
  1469. =item * All_Dirs()
  1470.  
  1471. Same as C<all_dirs('-r')>.
  1472.  
  1473. =item * all_files()
  1474.  
  1475. Same as C<all()>, but only return files.
  1476.  
  1477. =item * All_Files()
  1478.  
  1479. Same as C<all_files('-r')>.
  1480.  
  1481. =item * all_links()
  1482.  
  1483. Same as C<all()>, but only return links.
  1484.  
  1485. =item * All_Links()
  1486.  
  1487. Same as C<all_links('-r')>.
  1488.  
  1489. =item * append()
  1490.  
  1491. Same as print, but sets the file mode to '>>'.
  1492.  
  1493. =item * appendf()
  1494.  
  1495. Same as printf, but sets the file mode to '>>'.
  1496.  
  1497. =item * appendln()
  1498.  
  1499. Same as println, but sets the file mode to '>>'.
  1500.  
  1501. =item * autoclose()
  1502.  
  1503. By default, IO::All will close an object opened for input when EOF is
  1504. reached. By closing the handle early, one can immediately do other
  1505. operations on the object without first having to close it.
  1506.  
  1507. If you don't want this behaviour, say so like this:
  1508.  
  1509.     $io->autoclose(0);
  1510.  
  1511. The object will then be closed when C<$io> goes out of scope, or you
  1512. manually call C<<$io->close>>.
  1513.  
  1514. =item * autoflush()
  1515.  
  1516. Proxy for IO::Handle::autoflush()
  1517.  
  1518. =item * backwards()
  1519.  
  1520. Sets the object to 'backwards' mode. All subsequent C<getline>
  1521. operations will read backwards from the end of the file.
  1522.  
  1523. Requires Uri Guttman's File::ReadBackwards CPAN module.
  1524.  
  1525. =item * block_size()
  1526.  
  1527. The default length to be used for C<read()> and C<sysread()> calls.
  1528. Defaults to 1024.
  1529.  
  1530. =item * buffer()
  1531.  
  1532. Returns a reference to the internal buffer, which is a scalar. You can
  1533. use this method to set the buffer to a scalar of your choice. (You can
  1534. just pass in the scalar, rather than a reference to it.)
  1535.  
  1536. This is the buffer that C<read()> and C<write()> will use by default.
  1537.  
  1538. You can easily have IO::All objects use the same buffer:
  1539.  
  1540.     my $input = io('abc');
  1541.     my $output = io('xyz');
  1542.     my $buffer;
  1543.     $output->buffer($input->buffer($buffer));
  1544.     $output->write while $input->read;
  1545.  
  1546. =item * clear()
  1547.  
  1548. Clear the internal buffer. This method is called by write() after it writes
  1549. the buffer.
  1550.  
  1551. =item * close()
  1552.  
  1553. Proxy for IO::Handle::close()
  1554.  
  1555. =item * domain()
  1556.  
  1557. Set the domain name or ip address that a socket should use.
  1558.  
  1559. =item * domain_default()
  1560.  
  1561. The domain to use for a socket if none is specified. Defaults to
  1562. 'localhost'.
  1563.  
  1564. =item * eof()
  1565.  
  1566. Proxy for IO::Handle::eof()
  1567.  
  1568. =item * fileno()
  1569.  
  1570. Proxy for IO::Handle::fileno()
  1571.  
  1572. =item * getc()
  1573.  
  1574. Proxy for IO::Handle::getc()
  1575.  
  1576. =item * getline()
  1577.  
  1578. Calls IO::File::getline(). You can pass in an optional record separator.
  1579.  
  1580. =item * getlines()
  1581.  
  1582. Calls IO::File::getlines(). You can pass in an optional record separator.
  1583.  
  1584. =item * hash()
  1585.  
  1586. This method will return a reference to a tied hash representing the
  1587. directory. This allows you to treat a directory like a hash, where the
  1588. keys are the file names, and the values call lstat, and deleting a key
  1589. deletes the file.
  1590.  
  1591. See IO::Dir for more information on Tied Directories.
  1592.  
  1593. =item * io_handle()
  1594.  
  1595. Direct access to the actual IO::Handle object being used.
  1596.  
  1597. =item * is_dir()
  1598.  
  1599. Returns boolean telling whether or not the IO::All object represents
  1600. a directory.
  1601.  
  1602. =item * is_file()
  1603.  
  1604. Returns boolean telling whether or not the IO::All object
  1605. represents a file.
  1606.  
  1607. =item * is_link()
  1608.  
  1609. Returns boolean telling whether or not the IO::All object represents
  1610. a symlink.
  1611.  
  1612. =item * is_open()
  1613.  
  1614. Find out it the IO::All is currently open for input/output.
  1615.  
  1616. =item * is_socket()
  1617.  
  1618. Returns boolean telling whether or not the IO::All object represents
  1619. a socket.
  1620.  
  1621. =item * is_string()
  1622.  
  1623. Returns boolean telling whether or not the IO::All object represents
  1624. an IO::String object.
  1625.  
  1626. =item * length()
  1627.  
  1628. Return the length of the internal buffer.
  1629.  
  1630. =item * mode()
  1631.  
  1632. Set the mode for which the file should be opened. Examples:
  1633.  
  1634.     $io->mode('>>');
  1635.     $io->mode(O_RDONLY);
  1636.  
  1637. =item * name()
  1638.  
  1639. Return the name of the file or directory represented by the IO::All
  1640. object.
  1641.  
  1642. =item * next()
  1643.  
  1644. For a directory, this will return a new IO::All object for each file
  1645. or subdirectory in the directory. Return undef on EOD.
  1646.  
  1647. =item * open()
  1648.  
  1649. Open the IO::All object. Takes two optional arguments C<mode> and
  1650. C<perms>, which can also be set ahead of time using the C<mode()> and
  1651. C<perms()> methods.
  1652.  
  1653. NOTE: Normally you won't need to call open (or mode/perms), since this
  1654. happens automatically for most operations.
  1655.  
  1656. =item * perms()
  1657.  
  1658. Sets the permissions to be used if the file/directory needs to be created.
  1659.  
  1660. =item * port()
  1661.  
  1662. Set the port number that a socket should use.
  1663.  
  1664. =item * print()
  1665.  
  1666. Proxy for IO::Handle::print()
  1667.  
  1668. =item * printf()
  1669.  
  1670. Proxy for IO::Handle::printf()
  1671.  
  1672. =item * println()
  1673.  
  1674. Same as print(), but adds newline to each argument unless it already
  1675. ends with one.
  1676.  
  1677. =item * read()
  1678.  
  1679. This method varies depending on its context. Read carefully (no pun
  1680. intended).
  1681.  
  1682. For a file, this will proxy IO::File::read(). This means you must pass
  1683. it a buffer, a length to read, and optionally a buffer offset for where
  1684. to put the data that is read. The function returns the length actually
  1685. read (which is zero at EOF).
  1686.  
  1687. If you don't pass any arguments for a file, IO::All will use its own
  1688. internal buffer, a default length, and the offset will always point at
  1689. the end of the buffer. The buffer can be accessed with the C<buffer()>
  1690. method. The length can be set with the C<block_size> method. The default
  1691. length is 1024 bytes. The C<clear()> method can be called to clear
  1692. the buffer.
  1693.  
  1694. For a directory, this will proxy IO::Dir::read().
  1695.  
  1696. =item * readline()
  1697.  
  1698. Same as C<getline()>.
  1699.  
  1700. =item * recv()
  1701.  
  1702. Proxy for IO::Socket::recv()
  1703.  
  1704. =item * rewind()
  1705.  
  1706. Proxy for IO::Dir::rewind()
  1707.  
  1708. =item * rmdir()
  1709.  
  1710. Delete the directory represented by the IO::All object.
  1711.  
  1712. =item * seek()
  1713.  
  1714. Proxy for IO::Handle::seek()
  1715.  
  1716. =item * send()
  1717.  
  1718. Proxy for IO::Socket::send()
  1719.  
  1720. =item * shutdown()
  1721.  
  1722. Proxy for IO::Socket::shutdown()
  1723.  
  1724. =item * slurp()
  1725.  
  1726. Read all file content in one operation. Returns the file content
  1727. as a string. In list context returns every line in the file.
  1728.  
  1729. =item * stat()
  1730.  
  1731. Proxy for IO::Handle::stat()
  1732.  
  1733. =item * string_ref()
  1734.  
  1735. Proxy for IO::String::string_ref()
  1736.  
  1737. Returns a reference to the internal string that is acting like a file.
  1738.  
  1739. =item * sysread()
  1740.  
  1741. Proxy for IO::Handle::sysread()
  1742.  
  1743. =item * syswrite()
  1744.  
  1745. Proxy for IO::Handle::syswrite()
  1746.  
  1747. =item * tell()
  1748.  
  1749. Proxy for IO::Handle::tell()
  1750.  
  1751. =item * throw()
  1752.  
  1753. This is an internal method that gets called whenever there is an error.
  1754. It could be useful to override it in a subclass, to provide more control
  1755. in error handling.
  1756.  
  1757. =item * truncate()
  1758.  
  1759. Proxy for IO::Handle::truncate()
  1760.  
  1761. =item * type()
  1762.  
  1763. Returns a string indicated the type of io object. Possible values are:
  1764.  
  1765.     file
  1766.     dir
  1767.     link
  1768.     socket
  1769.     string
  1770.     pipe
  1771.  
  1772. Returns undef if type is not determinable.
  1773.  
  1774. =item * unlink
  1775.  
  1776. Unlink (delete) the file represented by the IO::All object.
  1777.  
  1778. NOTE: You can unlink a file after it is open, and continue using it
  1779. until it is closed.
  1780.  
  1781. =item * unlock
  1782.  
  1783. Release a lock from an object that used the C<-lock> flag.
  1784.  
  1785. =item * write
  1786.  
  1787. Opposite of C<read()> for file operations only.
  1788.  
  1789. NOTE: When used with the automatic internal buffer, C<write()> will
  1790. clear the buffer after writing it.
  1791.  
  1792. =back
  1793.  
  1794. =head1 STABILITY
  1795.  
  1796. The goal of the IO::All project is to continually refine the module
  1797. to be as simple and consistent to use as possible. Therefore, in the
  1798. early stages of the project, I will not hesitate to break backwards
  1799. compatibility with other versions of IO::All if I can find an easier
  1800. and clearer way to do a particular thing.
  1801.  
  1802. IO is tricky stuff. There is definitely more work to be done. On the
  1803. other hand, this module relies heavily on very stable existing IO
  1804. modules; so it may work fairly well.
  1805.  
  1806. I am sure you will find many unexpected "features". Please send all
  1807. problems, ideas and suggestions to INGY@cpan.org.
  1808.  
  1809. =head2 Known Bugs and Deficiencies
  1810.  
  1811. Not all possible combinations of objects and methods have been tested. There
  1812. are many many combinations. All of the examples have been tested. If you find
  1813. a bug with a particular combination of calls, let me know.
  1814.  
  1815. If you call a method that does not make sense for a particular object,
  1816. the result probably won't make sense. No attempt is made to check for
  1817. improper usage.
  1818.  
  1819. Support for format_write and other format stuff is not supported yet.
  1820.  
  1821. =head1 SEE ALSO
  1822.  
  1823. IO::Handle, IO::File, IO::Dir, IO::Socket, IO::String, IO::ReadBackwards,
  1824. Tie::File
  1825.  
  1826. Also check out the Spiffy module if you are interested in extending this
  1827. module.
  1828.  
  1829. =head1 AUTHOR
  1830.  
  1831. Brian Ingerson <INGY@cpan.org>
  1832.  
  1833. =head1 COPYRIGHT
  1834.  
  1835. Copyright (c) 2004. Brian Ingerson. All rights reserved.
  1836.  
  1837. This program is free software; you can redistribute it and/or modify it
  1838. under the same terms as Perl itself.
  1839.  
  1840. See L<http://www.perl.com/perl/misc/Artistic.html>
  1841.  
  1842. =cut
  1843.