Wx::Perl::ProcessStream(3pm) | User Contributed Perl Documentation | Wx::Perl::ProcessStream(3pm) |
Wx::Perl::ProcessStream - access IO of external processes via events
Version 0.32
use Wx::Perl::ProcessStream qw( :everything ); EVT_WXP_PROCESS_STREAM_STDOUT ( $self, \&evt_process_stdout ); EVT_WXP_PROCESS_STREAM_STDERR ( $self, \&evt_process_stderr ); EVT_WXP_PROCESS_STREAM_EXIT ( $self, \&evt_process_exit ); EVT_WXP_PROCESS_STREAM_MAXLINES ( $self, \&evt_process_maxlines ); my $proc1 = Wx::Perl::ProcessStream::Process->new('perl -e"print qq($_\n) for(@INC);"', 'MyName1', $self); $proc1->Run; my $command = 'executable.exe parm1 parm2 parm3' my $proc2 = Wx::Perl::ProcessStream::Process->new($command, 'MyName2', $self) ->Run; my @args = qw( executable.exe parm1 parm2 parm3 ); my $proc3 = Wx::Perl::ProcessStream::Process->new(\@args, 'MyName2', $self); $proc3->Run; my $proc4 = Wx::Perl::ProcessStream::Process->new(\@args, 'MyName2', $self, 'readline')->Run; my $proc5 = Wx::Perl::ProcessStream::Process->new(\@args, 'MyName2', $self); sub evt_process_stdout { my ($self, $event) = @_; $event->Skip(1); my $process = $event->GetProcess; my $line = $event->GetLine; if($line eq 'something we are waiting for') { $process->WriteProcess('a message to stdin'); $process->CloseInput() if($finishedwriting); } ............ # To Clear Buffer my @buffers = @{ $process->GetStdOutBuffer }; } sub evt_process_stderr { my ($self, $event) = @_; $event->Skip(1); my $process = $event->GetProcess; my $line = $event->GetLine; print STDERR qq($line\n); # To Clear Buffer my @errors = @{ $process->GetStdErrBuffer }; } sub evt_process_exit { my ($self, $event) = @_; $event->Skip(1); my $process = $event->GetProcess; my $line = $event->GetLine; my @buffers = @{ $process->GetStdOutBuffer }; my @errors = @{ $process->GetStdErrBuffer }; my $exitcode = $process->GetExitCode; ............ $process->Destroy; } sub evt_process_maxlines { my ($self, $event) = @_; my $process = $event->GetProcess; ..... bad process $process->Kill; }
This module provides the STDOUT, STDERR and exit codes of asynchronously running processes via events. It may be used for long running or blocking processes that provide periodic updates on state via STDOUT. Simple IPC is possible via STDIN.
Do not use this module simply to collect the output of another process. For that, it is much simpler to do:
my ($status, $output) = Wx::ExecuteStdout( 'perl -e"print qq($_\n) for(@INC);"' );
Methods
my $process = Wx::Perl::ProcessStream::Process->new($command, $name, $eventhandler, $readmethod); $command = command text (and parameters) you wish to run. You may also pass a reference to an array containing the command and parameters. $name = an arbitray name for the process. $eventhandler = the Wx EventHandler (Wx:Window) that will handle events for this process. $readmethod = 'read' or 'readline' (default = 'readline') an optional param. From Wx version 0.75 you can specify the method you wish to use to read the output of an external process. The default depends on your Wx version ( 'getc' < 0.75,'readline' >= 0.75) read -- uses the Wx::InputStream->READ method to read bytes. readline -- uses the Wx::InputStream->READLINE method to read bytes getc -- alias for read (getc not actually used)
$process->SetMaxLines(10);
my $process = Wx::Perl::ProcessStream::Process->new($command, $name, $eventhandler, $readmethod); $process->Run;
$process->CloseInput();
my $action = $process->GetAppCloseAction();
my $exitcode = $process->GetExitCode();
my $processname = $process->GetProcessName();
my $arryref = $process->GetStdErrBuffer();
my $arryref = $process->GetStdOutBuffer();
my $count = $process->GetStdErrBufferLineCount();
my $count = $process->GetStdOutBufferLineCount();
my $arryref = $process->PeekStdErrBuffer();
my $arryref = $process->PeekStdOutBuffer();
my $processid = $process->GetProcessId();
my $processid = $process->GetPid();
my $isalive = $process->IsAlive();
$process->KillProcess();
$process->SetAppCloseAction( $newaction ); $newaction = one of wxpSIGTERM, wxpSIGKILL
$process->TerminateProcess();
$process->WriteProcess( $writedata . "\n" ); $writedata = The data you wish to write. Remember to add any appropriate line endings your external process may expect.
Methods
my $process = Wx::Perl::ProcessStream->OpenProcess($command, $name, $eventhandler, $readmethod); $command = command text (and parameters) you wish to run. You may also pass a reference to an array containing the command and parameters. $name = an arbitray name for the process. $eventhandler = the Wx object that will handle events for this process. $process = Wx::Perl::ProcessStream::Process object $readmethod = 'getc' or 'readline' (default = 'readline') an optional param. From Wx version 0.75 you can specifiy the method you wish to use to read the output of an external process. The default depends on your Wx version ( 'getc' < 0.75, 'readline' >= 0.75) 'getc' uses the Wx::InputStream->GetC method to read bytes. 'readline', uses the wxPerl implementation of Wx::InputStream->READLINE.
If the process could not be started then zero is returned. You should destroy each process after it has completed. You can do this after receiving the exit event.
my $def-action = Wx::Perl::ProcessStream->SetDefaultAppCloseAction(); $def-action will be one of wxpSIGTERM or wxpSIGKILL; (default wxpSIGTERM)
Wx::Perl::ProcessStream->SetDefaultAppCloseAction( $newdefaction ); $newdefaction = one of wxpSIGTERM or wxpSIGKILL
Wx::Perl::ProcessStream->SetDefaultMaxLines( $maxlines ); the default maxlines number is 1000
$milliseconds = Wx::Perl::ProcessStream->GetPollInterval();
Wx::Perl::ProcessStream->SetPollInterval( $milliseconds ); $milliseconds = number of milliseconds to wait when no buffer activity
A Wx::Perl::ProcessStream::ProcessEvent is sent whenever an external process started with OpenProcess writes to STDOUT, STDERR or when the process exits.
Event Connectors
EVT_WXP_PROCESS_STREAM_STDOUT( $eventhandler, $codref );
EVT_WXP_PROCESS_STREAM_STDERR( $eventhandler, $codref );
EVT_WXP_PROCESS_STREAM_EXIT( $eventhandler, $codref );
EVT_WXP_PROCESS_STREAM_MAXLINES( $eventhandler, $codref );
Methods
Copyright (C) 2007-2010 Mark Dootson, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Thanks to Johan Vromans for testing and suggesting a better interface.
Mark Dootson, "<mdootson at cpan.org>"
The distribution includes examples in the 'example' folder. From the source root, run
perl -Ilib example/psexample.pl
You can enter commands, execute them and view results.
You may also wish to consult the wxWidgets manuals for:
Wx::Process
Wx::Execute
Wx::ExecuteArgs
Wx::ExecuteCommand
Wx::ExecuteStdout
Wx::ExecuteStdoutStderr
2021-01-04 | perl v5.32.0 |