The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

SYNOPSIS

  package Something::That::Stores::Filehandles;

  use TB2::Mouse;
  use TB2::ThreadSafeFilehandleAccessor fh_accessors => [qw(this_fh that_fh)];

  my $obj = Something::That::Stores::Filehandles->new(
      this_fh   => \*STDOUT,
      that_fh   => \*STDERR,
  );

  use threads;
  use threads::shared;
  $obj = shared_clone($obj);

DESCRIPTION

This provides accessors specially written so objects can store and access filehandles without storing them internally. This is necessary if the object is to be shared across threads.

All structures which are shared between threads must have all their internal values and objects also be shared. Filehandles cannot be shared. This presents a bit of a problem for TB2::Formatter objects which contain TB2::Streamer objects which contain a filehandle.

How To Use It

    use TB2::ThreadSafeFilehandleAccessor fh_accessors => [@names];

This will create an accessor for each name in @names.

Limitations & bugs

The main limitation is changes to the filehandles in a thread will not be shared across threads or with the parent.

The filehandles will not be cleaned up on object destruction. This may hold the filehandle open and prevent the file from being flushed to the disk until the process exits.

SEE ALSO

TB2::Streamer::Print is the main target for this.