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


Multi - Print to multiple filehandles with one output call


    use IO::File::Multi;
    $mult_obj=new IO::File::Multi;
    $mult_obj->print("This will be printed to several filehandles\n");
    $mult_obj->printf("This will be printed to %d filehandles\n",
        scalar @{$mult_obj->{handles}});
    @handle_refs = $mult_obj->members();


This module requires that the user have the IO::File module installed (it comes with the perl distribution). Create objects for each of the output filehandles you'll have - then call the print() and printf() methods to send output to ALL the filehandles associated with an object.


Look at the SYNOPSIS section. Also, here is a simple implementation of the unix tee(1) program (non-append mode):

  use IO::File::Multi;
  $mh=new IO::File::Multi;
  for (@ARGV) { $mh->open(">$_"); }
  while (<STDIN>) { $mh->print($_); }


I don't think using my()s the way I am in the open() method is all that good. binmode isn't supported, but I don't see anybody using that anyways. In order to use fcntl(), fileno(), or flock() you'll have to access the filehandles yourself by calling members(). There's no write() yet (but I'm working on it!). Also, any limitations to the IO::File module also apply here.

It has been pointed out to me that multiple open() calls on the same file under MacOS will fail. Hopefully, the carp() call will help with catching this.


Nem W Schlecht ( Comments, bugs fixes, and suggestions welcome.