Ggavazzo has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks; I have hundreds of perl scripts that are executed via command, for a better use of users, I built a screen for windows in tk. I would like to make a screen where the user searches for the file, search for a certain script to run. The output of the file will be in the same folder as the original file.

Replies are listed 'Best First'.
Re: TK + Script
by choroba (Cardinal) on Apr 07, 2021 at 15:32 UTC
    Use getOpenFile to show the files to the user.
    #! /usr/bin/perl use strict; use warnings; use Tk; use Path::Tiny qw{ path }; my $safe_path = qr{^/safe/path}; sub select_and_run { my ($mw, $text) = @_; my $scriptfile = $mw->getOpenFile(-filetypes => [[Perl => '*.pl']] +); my $result; if ($scriptfile =~ $safe_path && -x $scriptfile) { my $output = qx{ $scriptfile }; my $exit_code = $?; path("$scriptfile.output")->spew($output); $result = "Exit code: $exit_code"; } else { $result = 'Cannot run'; } $text->insert(end => "$result\n"); } my $mw = 'MainWindow'->new(-title => 'Script Runner'); my $f = $mw->Frame->pack; my $text; my $button = $f->Button(-text => 'Run Script', -command => sub { select_and_run($mw, $text) } +)->pack; $text = $f->Text->pack; my $q = $f->Button(-text => 'Quit', -command => sub { Tk::exit })->pack; Tk::MainLoop();
    Update: Minor code fixes.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: TK + Script
by haukex (Archbishop) on Apr 07, 2021 at 14:41 UTC
    Wise Monks; I have hundreds of perl scripts that are executed via command, for a better use of users, I built a screen for windows in tk. I would like to make a screen where the user searches for the file, search for a certain script to run. The output of the file will be in the same folder as the original file.

    Ok, so which part are you having trouble with? How do I post a question effectively?

    If this is about running external commands, see Calling External Commands More Safely. For external programs that execute quickly, I recommend IPC::Run3, and for long-running external programs that you need to monitor the output of, see IPC::Run (though Tk may have its own module for that kind of thing, I'm not sure). If you need to manipulate filenames, see Path::Class (or the core File::Spec::Functions), and if you need to change the current working directory, see File::pushd (or the core chdir).

      Thank you for the answer. My scripts are all in perl, I was thinking about opening a graphical screen in tk, selecting the file, selecting script.pl and a button to execute.
Re: TK + Script
by tybalt89 (Monsignor) on Apr 08, 2021 at 02:12 UTC

    Just because I haven't done Tk for a while. Probably needs some tweaking for directories and paths.

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11130955 use warnings; use Tk; use Path::Tiny; my $code; my $file; my $execute; my $mw = MainWindow->new; my $button = $mw->Button(-text => 'Execute', -command => \&execute, -state => 'disabled', )->pack(-side => 'bottom'); my $frame = $mw->Frame->pack(-fill => 'both', -expand => 1); $frame->Label(-text => 'Perl Script', -fg => 'blue', )->grid(-row => 1, -column => 1, -sticky => 'ew'); $frame->Label(-text => 'Data File', -fg => 'blue', )->grid(-row => 1, -column => 2, -sticky => 'ew'); my $codelist = $frame->Scrolled(Listbox => -scrollbars => 'osoe', -exportselection => 0, )->grid(-row => 2, -column => 1, -sticky => 'nsew'); my $filelist = $frame->Scrolled(Listbox => -scrollbars => 'osoe', -exportselection => 0, )->grid(-row => 2, -column => 2, -sticky => 'nsew'); $frame->gridRowconfigure( 2, -weight => 1 ); $frame->gridColumnconfigure( $_, -weight => 1 ) for 1, 2; $codelist->bind('<ButtonRelease-1>' => sub {$code = $codelist->get($codelist->curselection); ready() } ); $filelist->bind('<ButtonRelease-1>' => sub {$file = $filelist->get($filelist->curselection); ready() } ); sub execute { $execute++; $mw->destroy; } sub ready { $code && $file and $button->configure(-state => 'normal') +} $codelist->insert(end => sort grep -x, path('.')->children(qr/\.pl$/) +); $filelist->insert(end => sort +path('.')->children(qr/\.d$/) ); MainLoop; $execute or exit; $code or die "no code selected"; $file or die "no file selected"; print "exec $code $file\n"; #exec 'perl', $code, $file; ### FIXME testing - uncomment to actually +run die "exec failed $!";
      Thank you very much...