Michael G Schwern > Test-Simple-1.005000_005 > TB2::ThreadSafeFilehandleAccessor



Annotate this POD


View/Report Bugs
Source   Latest Release: Test-Simple-1.005000_006



  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);


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.


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

syntax highlighting: