Wx::Pe...sStream(3pm)
π Wx::Perl::ProcessStream π
okay bestie, so this is the manual page for Wx::Perl::ProcessStream β the thing that lets you peek at the IO of external processes via events. Version of the file itself: Wx::Perl::ProcessStream(3pm), user-contributed Perl documentation, and the module version is 0.32. Let's get into it π«Ά
β¨ VERSION β¨
Version 0.32 β that's the number, no notes π
πΈ SYNOPSYS πΈ
(this is spelled exactly how the original spells it, don't come for me)
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;
}
π DESCRIPTION π
So this module gives you the STDOUT, STDERR and exit codes of processes that are running asynchronously β and it hands them to you through events. It's for those long-running or blocking processes that keep chirping out periodic updates on state via STDOUT. And yes bestie, simple IPC is possible via STDIN. Iconic.
β οΈ But do NOT use this module just to collect the output of another process. That's giving "using a whole forklift to open a snack." For that super simple case, the way simpler move is:
my ($status, $output) = Wx::ExecuteStdout( 'perl -e"print qq($_\n) for(@INC);"' );
π Wx::Perl::ProcessStream::Process π
π« Methods π«
π new
Create a new Wx::Perl::ProcessStream::Process object. But heads up β creating it does not run anything. You then must use the Run method to actually execute your command. Main character energy: born, then you press go.
my $process = Wx::Perl::ProcessStream::Process->new($command, $name, $eventhandler, $readmethod);
The args, bestie:
$commandβ the 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. (yes, "arbitray", that's how it's written, I'm just the messenger π)$eventhandlerβ the Wx EventHandler (Wx:Window) that will handle events for this process.$readmethodβ'read'or'readline'(default ='readline'), an optional param. From Wx version0.75you 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 theWx::InputStream->READmethod to read bytes.readlineβ uses theWx::InputStream->READLINEmethod to read bytesgetcβ alias forread(getc not actually used)
π SetMaxLines
Set the maximum number of lines that will be read from a continuous stream before raising a EVT_WXP_PROCESS_STREAM_MAXLINES event. The default is 1000.
β οΈ fr fr: a continuous stream will cause your application to hang. This is the safety rail, don't skip it.
$process->SetMaxLines(10);
π Run
Run the process with the parameters passed to new. On success, returns the process object itself. That's so you can do the whole chain: my $process = Wx::Perl::ProcessStream->new($command, $name, $self)->Run; β returns undef if the process could not be started. π
my $process = Wx::Perl::ProcessStream::Process->new($command, $name, $eventhandler, $readmethod);
$process->Run;
π CloseInput
Close the STDIN stream of the external process. (Some processes may not close until STDIN is closed β very "won't leave until you say goodbye" of them.)
$process->CloseInput();
π GetAppCloseAction
Returns the current process signal that will used on application exit. Either wxpSIGTERM or wxpSIGKILL. See SetAppCloseAction.
my $action = $process->GetAppCloseAction();
π GetExitCode
Returns the process exit code. It is undefined until a wxpEVT_PROCESS_STREAM_EXIT event has been received. Patience, babe.
my $exitcode = $process->GetExitCode();
π GetProcessName
Returns the process name as passed to the OpenProcess constructor.
my $processname = $process->GetProcessName();
π GetStdErrBuffer
This returns a reference to an array containing all the lines sent by the process to stderr. Calling this clears the process object internal stderr buffer. (This has no effect on the actual process I/O buffers β it's our little notebook, not the process's own pipes.)
my $arryref = $process->GetStdErrBuffer();
π GetStdOutBuffer
This returns a reference to an array containing all the lines sent by the process to stdout. Calling this clears the process object internal stdout buffer. (This has no effect on the actual process I/O buffers.)
my $arryref = $process->GetStdOutBuffer();
π GetStdErrBufferLineCount
This returns the number of lines currently in the stderr buffer.
my $count = $process->GetStdErrBufferLineCount();
π GetStdOutBufferLineCount
This returns the number of lines currently in the stdout buffer.
my $count = $process->GetStdOutBufferLineCount();
π PeekStdErrBuffer
This returns a reference to an array containing all the lines sent by the process to stderr. To retrieve the buffer and clear it, call GetStdErrBuffer instead. Peek = look, don't touch π
my $arryref = $process->PeekStdErrBuffer();
π PeekStdOutBuffer
This returns a reference to an array containing all the lines sent by the process to stdout. To retrieve the buffer and clear it, call GetStdOutBuffer instead.
my $arryref = $process->PeekStdOutBuffer();
π GetProcessId
Returns the process id assigned by the system.
my $processid = $process->GetProcessId();
π GetPid
Returns the process id assigned by the system. (same energy as GetProcessId, just shorter, a little nickname moment π)
my $processid = $process->GetPid();
π IsAlive
Check if the process still exists in the system. Returns 1 if process exists, 0 if process does not exist. If the process has already signalled its exit, the IsAlive method will always return 0. Therefore IsAlive should always return 0 (false) once a EVT_WXP_PROCESS_STREAM_EXIT event has been sent.
my $isalive = $process->IsAlive();
π KillProcess
Send a SIGKILL signal to the external process.
$process->KillProcess();
π SetAppCloseAction
When your application exits, any remaining Wx::Perl::ProcessStream::Process objects will be signaled to close. The default signal is wxpSIGTERM but you can change this to wxpSIGKILL if you are sure this is what you want. (Saying "if you are sure" is the manpage equivalent of a friend going "okay but are you SURE sure" π)
$process->SetAppCloseAction( $newaction );
$newaction = one of wxpSIGTERM, wxpSIGKILL
π TerminateProcess
Send a SIGTERM signal to the external process.
$process->TerminateProcess();
π WriteProcess
Write to the STDIN of process.
$process->WriteProcess( $writedata . "\n" );
$writedata = The data you wish to write. Remember to add any appropriate line endings your external process may expect.
π Wx::Perl::ProcessStream π
π« Methods π«
π OpenProcess
Run an external process.
π₯ DEPRECATED β use Wx::Perl::ProcessStream::Process->new()->Run; instead. She's retired but still showing up to the party.
If the process is launched successfully, returns a Wx::Perl::ProcessStream::Process object. If the process could not be launched, returns undef;
my $process = Wx::Perl::ProcessStream->OpenProcess($command, $name, $eventhandler, $readmethod);
The args:
$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::Processobject$readmethodβ'getc'or'readline'(default ='readline'), an optional param. From Wx version0.75you 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 theWx::InputStream->GetCmethod to read bytes.'readline', uses the wxPerl implementation ofWx::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.
β¨ GetDefaultAppCloseAction β¨
okay so bestie, this one returns the default on application close action that will be given to new processes. When your application exits, any remaining Wx::Perl::ProcessStream::Process objects will be signalled to close. The default signal is wxpSIGTERM but you can change this to wxpSIGKILL if you are sure this is what you want. Whenever a new process is opened, it is given the application close action returned by GetDefaultAppCloseAction. You can also set the application close action at an individual process level.
my $def-action = Wx::Perl::ProcessStream->SetDefaultAppCloseAction();
$def-action will be one of wxpSIGTERM or wxpSIGKILL; (default wxpSIGTERM)
β¨ SetDefaultAppCloseAction β¨
Sets the default on application close action that will be given to new processes. See GetDefaultAppCloseAction.
Wx::Perl::ProcessStream->SetDefaultAppCloseAction( $newdefaction );
$newdefaction = one of wxpSIGTERM or wxpSIGKILL
β¨ SetDefaultMaxLines β¨
Sets the default maximum number of lines that will be processed continuously from an individual process. If a process produces a continuous stream of output, this would hang your application. This setting provides a maximum number of lines that will be read from the process streams before control is yielded and the events can be processed. Additionally, a EVT_WXP_PROCESS_STREAM_MAXLINES event will be sent to the eventhandler. The setting can also be set on an individual process basis using $process->SetMaxLines
Wx::Perl::ProcessStream->SetDefaultMaxLines( $maxlines );
the default maxlines number is 1000
β¨ GetPollInterval β¨
Get the current polling interval. See SetPollInterval.
$milliseconds = Wx::Perl::ProcessStream->GetPollInterval();
β¨ SetPollInterval β¨
When all buffers are empty but there are still running external process, the module will pause before polling the processes again for output. By default, the module waits for 500 milliseconds. You can set the value of this polling interval with this method. Internally, a Wx::Timer object is used to handle polling and the value you set here is passed directly to that. The precision of the intervals is OS dependent.
Wx::Perl::ProcessStream->SetPollInterval( $milliseconds );
$milliseconds = number of milliseconds to wait when no buffer activity
π Wx::Perl::ProcessStream::ProcessEvent π
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 β¨
Install an event handler for an event of type wxpEVT_PROCESS_STREAM_STDOUT exported on request by this module. The event subroutine will receive a Wx::Perl::ProcessStream::ProcessEvent for every line written to STDOUT by the external process.
EVT_WXP_PROCESS_STREAM_STDOUT( $eventhandler, $codref );
β¨ EVT_WXP_PROCESS_STREAM_STDERR β¨
Install an event handler for an event of type wxpEVT_PROCESS_STREAM_STDERR exported on request by this module. The event subroutine will receive a Wx::Perl::ProcessStream::ProcessEvent for every line written to STDERR by the external process.
EVT_WXP_PROCESS_STREAM_STDERR( $eventhandler, $codref );
β¨ EVT_WXP_PROCESS_STREAM_EXIT β¨
Install an event handler for an event of type wxpEVT_PROCESS_STREAM_EXIT exported on request by this module. The event subroutine will receive a Wx::Perl::ProcessStream::ProcessEvent when the external process exits.
EVT_WXP_PROCESS_STREAM_EXIT( $eventhandler, $codref );
β¨ EVT_WXP_PROCESS_STREAM_MAXLINES β¨
Install an event handler for an event of type wxpEVT_PROCESS_STREAM_MAXLINES exported on request by this module. The event subroutine will receive a Wx::Perl::ProcessStream::ProcessEvent when the external process produces a continuous stream of lines on stderr and stdout that exceed the max lines set via $process->SetMaxLines or Wx::Perl::ProcessStream->SetDefaultMaxLines.
EVT_WXP_PROCESS_STREAM_MAXLINES( $eventhandler, $codref );
π¦ Methods π¦
π GetLine π
For events of type wxpEVT_PROCESS_STREAM_STDOUT and wxpEVT_PROCESS_STREAM_STDERR this will return the line written by the process.
π GetProcess π
This returns the process that raised the event. If this is a wxpEVT_PROCESS_STREAM_EXIT event you should destroy the process with $process->Destroy;
π COPYRIGHT & LICENSE π
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.
πΈ ACKNOWLEDGEMENTS πΈ
Thanks to Johan Vromans for testing and suggesting a better interface.
π AUTHOR π
Mark Dootson, "<mdootson at cpan.org>"
π SEE ALSO π
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::ProcessWx::ExecuteWx::ExecuteArgsWx::ExecuteCommandWx::ExecuteStdoutWx::ExecuteStdoutStderr
And that's the tea, bestie! βοΈ This was Wx::Pe...sStream(3pm) from perl v5.32.0, dated 2021-01-04. Now go make those processes slay! π
β¨
π¦ where this page comes from
- debian bookworm: libwx-perl-processstream-perl 0.32-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libwx-perl-processstream-perl 0.32-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libwx-perl-processstream-perl 0.32-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms