Run an interactive terminal session, filtering the input and output
version 0.03
package My::Term::Filter; use Moose; with 'Term::Filter'; sub munge_input { my $self = shift; my ($got) = @_; $got =~ s/\ce/E- Elbereth\n/g; $got; } sub munge_output { my $self = shift; my ($got) = @_; $got =~ s/(Elbereth)/\e[35m$1\e[m/g; $got; } My::Term::Filter->new->run('nethack');
This module is a Moose role which implements running a program in a pty while being able to filter the data that goes into and out of it. This can be used to alter the inputs and outputs of a terminal based program (as in the \*(L"\s-1SYNOPSIS\s0\*(R"), or to intercept the data going in or out to record it or rebroadcast it (App::Ttyrec or App::Termcast, for instance).
This role is intended to be consumed by a class which implements its callbacks as methods; for a simpler callback-based \s-1API\s0, you may want to use Term::Filter::Callback instead.
The input filehandle to attach to the pty's input. Defaults to \s-1STDIN\s0.
The output filehandle to attach the pty's output to. Defaults to \s-1STDOUT\s0.
The IO::Pty::Easy object that the subprocess will be run under. Defaults to a newly created instance.
Returns the filehandles which will be monitored for reading. This list defaults to \*(C`input\*(C' and \*(C`pty\*(C'.
Add an input handle to monitor for reading. After calling this method, the \*(C`read\*(C' callback will be called with $fh as an argument whenever data is available to be read from $fh.
Remove $fh from the list of input handles being watched for reading.
Run the command specified by @cmd, as though via \*(C`system\*(C'. The callbacks that have been defined will be called at the appropriate times, to allow for manipulating the data that is sent or received.
The following methods may be defined to interact with the subprocess:
Called when the process has just been started. The parameters to \*(C`run\*(C' are passed to this callback.
Called when the process terminates. Will not be called if \*(C`setup\*(C' is never run (for instance, if the process fails to start).
Called whenever there is new data coming from the \*(C`input\*(C' handle, before it is passed to the pty. Must return the data to send to the pty (and the default implementation does this), but can do other things with the data as well.
Called whenever the process running on the pty has produced new data, before it is passed to the \*(C`output\*(C' handle. Must return the data to send to the \*(C`output\*(C' handle (and the default implementation does this), but can do other things with the data as well.
Called when a filehandle other than \*(C`input\*(C' or \*(C`pty\*(C' has data available (so will never be called unless you call \*(C`add_input_handle\*(C' to register your handle with the event loop). Receives the handle with data available as its only argument.
Called when an exception state is detected in any handle in \*(C`input_handles\*(C' (including the default ones). Receives the handle with the exception state as its only argument.
Called whenever the parent process receives a \*(C`SIGWINCH\*(C' signal, after it propagates that signal to the subprocess. \*(C`SIGWINCH\*(C' is sent to a process running on a terminal whenever the dimensions of that terminal change. This callback can be used to update any other handles watching the subprocess about the new terminal size.
No known bugs.
Please report any bugs through \s-1RT:\s0 email \*(C`bug-term-filter at rt.cpan.org\*(C', or browse to http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Term-Filter <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Term-Filter>.
IO::Pty::Easy
App::Termcast
App::Ttyrec
You can find this documentation for this module with the perldoc command.
perldoc Term::Filter
You can also look for information at:
AnnoCPAN: Annotated \s-1CPAN\s0 documentation http://annocpan.org/dist/Term-Filter <http://annocpan.org/dist/Term-Filter>
\s-1CPAN\s0 Ratings http://cpanratings.perl.org/d/Term-Filter <http://cpanratings.perl.org/d/Term-Filter>
\s-1RT:\s0 \s-1CPAN\s0's request tracker http://rt.cpan.org/NoAuth/Bugs.html?Dist=Term-Filter <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Term-Filter>
Search \s-1CPAN\s0 http://search.cpan.org/dist/Term-Filter <http://search.cpan.org/dist/Term-Filter>
Jesse Luehrs <doy at tozt dot net>
This software is copyright (c) 2012 by Jesse Luehrs.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.