The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

IO::Mux - Multiplex several virtual streams over a real pipe/socket

SYNOPSIS

  use IO::Mux ;

  pipe(R, W) ;

  if (fork){
      my $mux = new IO::Mux(\*W) ;
      my $alice = $mux->new_handle() ;
      open($alice, 'alice') ;
      my $bob = $mux->new_handle() ;
      open($bob, 'bob') ;

      print $alice "Hi Alice!\n" ;
      print $bob "Hi Bob!\n" ;
  }
  else {
      my $mux = new IO::Mux(\*R) ;
      my $alice = $mux->new_handle() ;
      open($alice, 'alice') ;
      my $bob = $mux->new_handle() ;
      open($bob, 'bob') ;

      print scalar(<$bob>) ;
      print scalar(<$alice>) ;
  }

DESCRIPTION

IO::Mux allows you to multiplex sevaral virtual streams over a single pipe or socket. This is achieved by creating an IO::Mux object on each end of the real stream and then creating virtual handles (IO::Mux::Handle objects) from these IO::Mux objects.

Each IO::Mux::Handle object is assigned a unique identifier when opened, and IO::Mux::Handle objects on each end of the real stream that have the same identifier are "mapped" to each other.

CONSTRUCTOR

new ( HANDLE )

Creates a new IO::Mux object that multiplexes over HANDLE. autoflush will be turned on for HANDLE.

METHODS

$mux->get_handle ()

Returns the handle passed when $mux was created.

$mux->new_handle ()

Convenience method. Returns a new IO::Mux::Handle object created on $mux. Is equivalent to:

  new IO::Mux::Handle($mux) ;

The handle must then be opened before being used. See "open" in IO::Mux::Handle for more details.

NOTE

Once a handle has been passed to an IO::Mux object, it is important that it is not written to/read from directly as this will corrupt the IO::Mux stream.

Once the IO::Mux objects on both ends of the stream are out of scope (and have no data pending), normal usage of the handleis can resume.

SEE ALSO

IO::Mux::Handle

AUTHOR

Patrick LeBoutillier, <patl@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2005 by Patrick LeBoutillier

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.