Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show Perl console output in a GUI?

I have several simple Perl programs writing to the standard output, but this has some problems:

  • some of my users are scared by a console
  • my users work on Windows, so my program output is displayed on a cmd console, so I cannot control the way it is displayed (colors and terminal size), and worse, the console is not resizeable (at least in width)

To solve these problems, I would like to be able to display the output in a simple GUI interface as an option. The GUI should be responsive until the program finishes (it should be possible to scroll or resize the window while it's running).

What simple toolkit and widget can I use to do this? (I develop on Windows with ActivePerl).

like image 629
Jazz Avatar asked Dec 08 '09 13:12

Jazz


3 Answers

You can use any GUI option you like, and then you could use Tie::STDOUT to redefine the behavior of print and printf to the STDOUT filehandle to instead dump output into the widget of your choice. The only thing is that getting it to talk to your widgets across packages cleanly using the anonymous sub might be messy. Here's a short, crude example using Win32::GUI:

use Win32::GUI();
use Tie::STDOUT 
    print => sub {
        $main::textfield->Append(@_);
    };

my $main = Win32::GUI::Window->new(
        -name => 'Main',
        -text => 'Perl',
        -width => 220,
        -height => 230,
    );
our $textfield = $main->AddTextfield(
        -name   => "Output",
        -left   => 8,
        -top    => 8,
        -width  => 180,
        -height => 180,
        -readonly => 1,
        -multiline => 1,
        -vscroll => 1,
    );
$main->Show();
sub Main_Terminate {
        -1;
}


if(!fork()) {
    print "Hello.\n";
    for (1..20) {
      sleep 1;
      printf "More output %d\n", $_;
    }
} else {
    Win32::GUI::Dialog();
}

Note the call to Win32::GUI::Dialog() at the end is present to keep the window from closing as soon as the script is finished.

like image 150
Adam Bellaire Avatar answered Nov 10 '22 05:11

Adam Bellaire


From a quick search you do have a few options:

  1. Perl with NCurses (its a GUI, but it keeps it in the console)
  2. Perl with wxWindows wxPerl
  3. PerlTK
  4. GTK with Perl (see the wikibooks reference)

Also, there is a wikibooks reference for this.

like image 29
monksy Avatar answered Nov 10 '22 03:11

monksy


If you have Firefox installed on the machines, I've been working on the module XUL::Gui which lets you display your Perl gui using Firefox's rendering engine. Building on Adam's answer:

use XUL::Gui;
use Tie::STDOUT 
    print => sub {$ID{text}->value .= join '' => @_};

display Window title=>'Perl', minwidth=>640, minheight=>480,
    TextBox( FILL SCROLL id=>'text', multiline=>'true' ),
    delay {
        print "hello world\n";  # Output goes to the window.
        for (1..5) {
            printf "More output %d\n", $_;
        }
    };

Edit: fixed a bug with multi-line return values from the gui, example above is nicer now. works with XUL::Gui 0.35+

like image 4
Eric Strom Avatar answered Nov 10 '22 05:11

Eric Strom