Tie::Select - Provides a localized interface to the select
function
use strict; use warnings;
use Tie::Select;
{
local $SELECT = *STDERR;
print "This goes to STDERR";
}
print "This goes to STDOUT";
The Perl builtin print
, when not called with an explicit file handle, will print to the file handle designated by the select
command. This is a global action, which is bad. Further, it has an awkward interface for restoring a previous handle; on a call to select
a reference to the old handle is returned, which has to itself be select
-ed to restore the old handle. Better to see an example.
my $stdout = select *STDERR;
print "To STDERR";
select $stdout;
print "To STDOUT";
Tie::Select offers a localizable interface to select
. Simply assign a handle to the $SELECT
variable provided by this module to change the select
-ed handle. If this is done with local
the change is dynamically bound to the enclosing scope. Therefore the above example becomes simply:
{
local $SELECT = *STDERR;
print "To STDERR";
}
The inspiration for this type of interface is File::chdir which provides a similar localizable interface to the current working directory.
File::chdir - Allow localized working directory, inspiration for this module
Lexical::select - As the name implies, it provides a lexically scoped interface to the
select
function rather than dynamically scopedIO::Select - This time its an Object-Oriented interface to
select
http://github.com/jberger/Tie-Select
Joel Berger, <joel.a.berger@gmail.com>
Copyright (C) 2012 by Joel Berger
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.