1use strict; use warnings; 2package IO::All::Pipe; 3 4use IO::All -base; 5use IO::File; 6 7const type => 'pipe'; 8 9sub pipe { 10 my $self = shift; 11 bless $self, __PACKAGE__; 12 $self->name(shift) if @_; 13 return $self->_init; 14} 15 16sub _assert_open { 17 my $self = shift; 18 return if $self->is_open; 19 $self->mode(shift) unless $self->mode; 20 $self->open; 21} 22 23sub open { 24 my $self = shift; 25 $self->is_open(1); 26 require IO::Handle; 27 $self->io_handle(IO::Handle->new) 28 unless defined $self->io_handle; 29 my $command = $self->name; 30 $command =~ s/(^\||\|$)//; 31 my $mode = shift || $self->mode || '<'; 32 my $pipe_mode = 33 $mode eq '>' ? '|-' : 34 $mode eq '<' ? '-|' : 35 $self->throw("Invalid usage mode '$mode' for pipe"); 36 CORE::open($self->io_handle, $pipe_mode, $command); 37 $self->_set_binmode; 38} 39 40my %mode_msg = ( 41 '>' => 'output', 42 '<' => 'input', 43 '>>' => 'append', 44); 45sub open_msg { 46 my $self = shift; 47 my $name = defined $self->name 48 ? " '" . $self->name . "'" 49 : ''; 50 my $direction = defined $mode_msg{$self->mode} 51 ? ' for ' . $mode_msg{$self->mode} 52 : ''; 53 return qq{Can't open pipe$name$direction:\n$!}; 54} 55 561; 57