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


Tk::Stderr - capture standard error output, display in separate window


        use Tk::Stderr;

        $mw = MainWindow->new->InitStderr;
        print STDERR 'stuff';   ## goes to standard error window
        warn 'eek!';            ## likewise


This module captures that standard error of a program and redirects it to a read only text widget, which doesn't appear until necessary. When it does appear, the user can close it; it'll appear again when there is more output.


These are actually added to the MainWindow class.


The first time this method called, it does the following things:


Creates a MainWindow holding a read-only scrollable text widget, and withdraws this window until it's actually needed.


Ties STDERR to a special handle that adds the output to this text widget.


Installs a $SIG{__WARN__} handler that redirects the output from warn to this window as well (by printing it to STDERR).

On the remaining calls, it:


Increments a reference count of "other" MainWindows.


Installs an OnDestroy handler that decrements this reference count, so that it can detect when it's the only MainWindow left and destroy itself.

$errwin = $mw->StderrWindow;

Returns a reference to the main window holding the text. You can use this to configure the window itself or the widgets it contains. The only advertised subwidget is 'text', which is the scrolled read-only text widget.

$old = $mw->RedirectStderr($boolean);

Enables or disables the redirection of standard error to the text window. Set $boolean to true to enable redirection, false to disable it. Returns the previous value of the enabled flag.

If InitStderr has never been called, this routine will call it if $boolean is true.


Kevin Michael Vail <>