Using dialog(1) this snippet allow users to use a GUI to configure options.
#!/usr/bin/perl
use IPC::Open3;
use File::Temp qw/ :mktemp /;
my $options={
'height'=>30,
'menuheight'=>20,
'width'=>50,
'title'=>"My dialog",
};
my $val={
'Options1'=>30,
'String_Options'=>30,
'Another_Options'=>30,
'Another_again'=>"My dialog",
};
perlog_run($options,$val);
sub create_cmd_line()
{
my ($hropt,$hrentry)=@_;
my $cmdline="dialog --nocancel --inputmenu \"$hropt->{'title'}\" $
+hropt->{'height'} $hropt->{'width'} $hropt->{'menuheight'}";
foreach(sort keys %$hrentry)
{
$cmdline .= " \"$_\" \"$hrentry->{$_}\"";
}
return $cmdline;
}
sub perlog_run()
{
my ($hropt,$hrentry)=@_;
my $d;
for(;;)
{
open OLDERR, ">&", \*STDERR;
close(STDERR);
(*STDERR,$filename)=mkstemp("perllogXXXXXX");
my $c=&create_cmd_line($hropt,$hrentry);
system("$c");
close (STDERR);
open STDERR, ">&", \*OLDERR;
close (OLDERR);
open IN,"<$filename";
$_=<IN>;
close(IN);
unlink $filename;
(/[^\s]+\s([^\s]+)\s(.+)$/)?$hrentry->{$1}=$2:last;
}
}
New version (FULL ini format supported!)
use Getopt::Long;
my $val={'height'=>30,'menuheight'=>20,'width'=>50,'title'=>"My dialog
+",};
my $path="config/ini/";
my $file,$help;
my %iniopts=('case'=>'sensitive');
GetOptions ("conf=s"=> \$file,
"path=s"=> \$path);
if($file eq "")
{
print RESET "**************"; print GREEN "PER";
print RESET "[L][DIA]";
print GREEN "LOG by Luca Benini - 2004 - GPL";
print RESET "**************\nUsage:\n\t";
print BLUE "$0 [--path=/path/to/ini/file] [--conf=inifile]\n"
+;
print RESET "Default path:\n\t";
print BLUE "$path";
print RESET "\n";
exit(0);
}
$val->{'title'}="$file | Luca Benini Configure";
my $Config = ReadINI("$path$file",%iniopts) or die "[READ] Unable to o
+pen $path$
file: $!";
foreach(keys %$Config)
{
my $t=$Config->{$_};
foreach(keys %$t)
{
delete $t->{$_} if (/#/);
}
}
my $i=perlog_config($val,$Config);
WriteINI("$path$file",$Config) or die "[WRITE] Unable to open $path$fi
+le: $!";
sub perlog_config()
{
my ($hropt,$hrentry)=@_;
while(1)
{
my $i=perlog_run_select($hropt,$hrentry);
return if ($i eq "");
perlog_run_rename($hropt,$hrentry->{$i});
}
}
sub create_cmd_line_menu()
{
my ($hropt,$hrentry)=@_;
my $cmdline="dialog --nocancel --menu \"$hropt->{'title'}\" $h
+ropt->{'he
ight'} $hropt->{'width'} $hropt->{'menuheight'}";
foreach(sort keys %$hrentry)
{
$cmdline .= " \"$_\" \"$_\"";
}
return $cmdline;
}
sub create_cmd_line_imenu()
{
my ($hropt,$hrentry)=@_;
my $cmdline="dialog --nocancel --inputmenu \"$hropt->{'title'}
+\" $hropt-
>{'height'} $hropt->{'width'} $hropt->{'menuheight'}";
foreach(sort keys %$hrentry)
{
$cmdline .= " \"$_\" \"$hrentry->{$_}\"";
}
return $cmdline;
}
sub perlog_run_select()
{
my ($hropt,$hrentry)=@_;
my $d;
open OLDERR, ">&", \*STDERR;
close(STDERR);
(*STDERR,$filename)=mkstemp("perllogXXXXXX");
my $c=&create_cmd_line_menu($hropt,$hrentry);
system("$c");
close (STDERR);
open STDERR, ">&", \*OLDERR;
close (OLDERR);
open IN,"<$filename";
$_=<IN>;
close(IN);
unlink $filename or die ;
return $_;
}
sub perlog_run_rename()
{
my ($hropt,$hrentry)=@_;
my $d;
for(;;)
{
open OLDERR, ">&", \*STDERR;
close(STDERR);
(*STDERR,$filename)=mkstemp("perllogXXXXXX");
my $c=&create_cmd_line_imenu($hropt,$hrentry);
system("$c");
close (STDERR);
open STDERR, ">&", \*OLDERR;
close (OLDERR);
open IN,"<$filename";
$_=<IN>;
close(IN);
unlink $filename or die ;
(/[^\s]+\s(.+)\s([^\s]+)$/)?$hrentry->{$1}=$2:last;
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.