use warnings; use strict; use Tk; =head Parse a configuration text file (TemplateValues.txt) to gather configuration parameters, defaults and labels required to perform some task then present a user interface built from the information in the configuration file to capture user values for the parameters. =cut my $main = MainWindow->new (); if (! open configIn, "< TemplateValues.txt") { my $text = $main->Text (); $text->Insert ("Unable to open configuration file: $!"); $text->pack (); $main->Button (-text => "Quit", -command => sub {$main->destroy ()})->pack (); MainLoop (); exit (-1); } my %Parameters; while () { next if /^#/; #Skip comment lines chomp; next if ! length $_; my ($key, $default, $comment) = /^(\w+)\s(?:"(.*?)")?(.*)/; my $label = $main->Label (-text => $comment)->pack (); $Parameters {$key} = [ $main->Entry ( -validate => 'focusout', -validatecommand => sub {touch ($key);} )->pack (), $default ]; } touch (""); $main->Button (-text => "Cancel", -command => sub {$main->destroy ()})->pack (); $main->Button ( -text => "Create Project", -command => sub {tidyParams (); CreateProject (); $main->destroy ();} )->pack (); MainLoop; sub CreateProject { #Note that %Parameters's values are now the user supplied strings print join "\n", map {$_ . " => " . $Parameters{$_}} keys %Parameters; } sub touch { my $key = shift; Entry: for (keys %Parameters) { next if defined $key and $key eq $_; my $repKey = $_; my $default = ${$Parameters {$repKey}}[1]; next if ! defined $default or ! length $default; #Don't edit while ($default =~ /(_[a-zA-Z0-9]+_)/) { next Entry if ! defined $Parameters {$1}; my $subStr = ${$Parameters {$1}}[0]->get (); $default =~ s/$1/$subStr/eg; } ${$Parameters{$repKey}}[0]->delete (0, 'end'); ${$Parameters{$repKey}}[0]->insert (0, $default) if length $default; } ${$Parameters{$key}}[1] = undef if defined $key and length $key; #Prevent auto default updates return 1; } sub tidyParams { touch (); for my $key (keys %Parameters) { next if ! length $key; next if ! defined $Parameters{$key}; $Parameters{$key} = ${$Parameters {$key}}[0]->get (); } }