Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
PerlMonks snippet runner

by Hue-Bond (Priest)
on Oct 14, 2006 at 20:30 UTC ( #578318=sourcecode: print w/replies, xml ) Need Help??
Category: GUI Programming
Author/Contact Info David Serrano
email: rot13 decode_base64 'ZHNlcnJhbm81IGF0IHlhaG9vLmVzCg=='

I'm very lazy. When I'm at the monastery, I hate to open an $EDITOR just for pasting that small snippet, saving it to a file ("Overwrite?" yes, of course) and then run it. So what I usually do is run perl at the shell prompt, and paste there. This has the drawback that, to modify it, I have to copy-paste part of it, change something and copy-paste the rest. And when the output of the program is so large that my terminal runs out of buffer space, it is lost. These problems notwithstanding, I'm very fond of this way of doing things since that's how I began using Perl. Just type, do a Ctrl-D and see the action!

This Gtk2 program has a text box in it, and a "Run" button. Just paste the code there, and click the button (or hit Alt-R). The output of the code goes to's STDOUT, so it has to be executed from a terminal. Snippets are run under -t to encourage good programming habits without standing in the way.


use warnings;
use strict;
use Gtk2;

## todo: change mnemonic into an accelerator (^D)
## todo: do another one for quitting (^Q)
## bug (fixed by forking): halts on infinite loops :^P

my ($window, $vbox, $ptext, $but, $label);
my $font = 'Monospace 10';
my $perl = '/usr/bin/perl';

sub do_run {
    my $pid;
    my $buf = $ptext->get_buffer;
    local $SIG{'CHLD'} = 'IGNORE';

    warn "fork: $!" and return unless defined ($pid = fork);
    if ($pid) { ## parent

    ## child
    $pid = open my $fd, '|-', $perl, '-t' or die "fork: $!";
    print "\n$perl PID: $pid\n";  ## for easy killing
    print $fd $buf->get_text ($buf->get_bounds, 1);
    close $fd;


$window = Gtk2::Window->new ('toplevel');
$vbox   = Gtk2::VBox->new (0, 0); 
$ptext  = Gtk2::TextView->new;
$but    = Gtk2::Button->new;
$label  = Gtk2::Label->new_with_mnemonic ('_Run!');

$but->signal_connect (clicked => \&do_run);
$but->add ($label);
$but->set_size_request (50, 30);

$ptext->set_size_request (50, 20);
$ptext->set_border_width (4);
$ptext->modify_font (Gtk2::Pango::FontDescription->from_string ($font)
$ptext->get_buffer->set_text (<<_DEF_TEXT_);
use warnings;
use strict;
use Data::Dumper;

\$ENV{'PATH'} = \$ENV{'CDPATH'} = '/usr/bin:/bin';


$vbox->add ($ptext);
$vbox->pack_end ($but, 0, 0, 0);

$window->signal_connect (destroy => sub { Gtk2->main_quit; });
$window->set_default_size (700, 400);
$window->add ($vbox);
Replies are listed 'Best First'.
Re: snippet runner
by zentara (Archbishop) on Oct 15, 2006 at 12:31 UTC
    Nice idea.

    I have something similar at Gtk2 Visual Grep which I use for pasting code into Perlmonk's nodes. Just run "vgrep n searchword" to search a directory for files with that searchword in it's filename, then click on the result links to open them. Then it's a simple matter of right-click->select-all followed by right-click->copy then a copy operation into the Perlmonk's editor box.

    Maybe you could combine the 2 scripts, to have 2 modes, search for keywords, or write new.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: snippet runner
by ambrus (Abbot) on Oct 15, 2006 at 14:41 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://578318]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2020-09-29 20:19 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (153 votes). Check out past polls.