#!/usr/bin/perl -w # Form boxes, build forms - get answers (C) - Fred Cohen 2004 - Distri +buted with no warranty # example: # formbox.pl fill label test fill crlf radio "one two three four" crlf + label this entry 23 "this is sad" crlf fill label that \ # NOT RIGHT hr fills width with horizontal bar # NOT YET sub form pop up a new form within this form # NOT YET display [integer] create an output area integer lines +long - the width of the rest of the world # NOT YET show [integer] [pathname] show pathname in display +number integer # NOT YET print [integer] [statement] show results of eval in +display number [integer] # #syntax entry [integer] [default] entry 20 'this example' + Create entry area #syntax entry [fill] [default] entry fill 'this example' + Entry with available width # label [value] label 'test label + Add a label # fill fill + fill width # crlf crlf + next line (frame) # OK OK + OK button # hr hr + Horizontal rule # check [word]+ check 'affirm entry' + Checkbox # mark [word] mark 'affirm entry' + Mark box # list [word]+ list 'one two three four five' + List box with these options # radio [word]+ radio 'one two three four five +' Radio with these buttons # slide [integer] [integer] slide 12 34 + slider from 12 to 34 or whatever # eval [word] [statement] eval 'add' '$z=2+3+4;print "=$ +z\n";' evaluate statement when button pushed # file [pathname] file /root/testfile + add file to the end of pending commands require 5.002; use English; use Tk; use Tk::DialogBox; my $ans=""; $debug=0; $fcount=1; $count=0; $evalcount=0; sub done {for ($i=1;$i<=$count;$i++) {$ans=eval "\$answer$i";chomp $an +s; print "$ans\n";} exit;} sub nextword {$w=pop @todo;return $w;} sub godoit {while ($todo[0]) {$word=nextword; if ($word eq "list") {if ($debug) {print "List-";} $count++; eval "\$answer$count=\"\";"; $things=nextword; @things=split(/\s/, $things); my $thingtoeval="\$textframe$fcount->Optionmenu(-optio +ns => ["; foreach $item (@things) {$thingtoeval=$thingtoeval."'$ +item',";} $thingtoeval=$thingtoeval."], -variable => \\\$answer$ +count) ->pack(-side => 'left', -fill => 'x');"; eval $thingtoeval; } elsif ($word eq "radio") {if ($debug) {print "Radio-";} $count++; eval "\$answer$count=\"\";"; $things=nextword; @things=split(/\s/, $things); foreach $item (@things) foreach $item (@things) {eval "\$textframe$fcount->Radiobutton(-text = +> '$item', -variable => \\\$answer$count, -value => '$item') ->pack(-side => 'left', -fill => 'x'); +";} } elsif ($word eq "mark") {if ($debug) {print "Mark-";} $count++; $things=nextword; eval "\$answer$count=0;"; eval "\$textframe$fcount->Checkbutton(-text=> '$things +', -indicatoron=> \$answer$count, -variable => \\\$answer$count) -> pack (-side => 'left');"; } elsif ($word eq "check") {if ($debug) {print "Check-";} $count++; $things=nextword; eval "\$answer$count=0;"; eval "\$textframe$fcount->Checkbutton(-text=> '$things +', -indicatoron=> \\\$answer$count, -variable => \\\$answer$count) -> pack (-side => 'left');"; } elsif ($word eq "slide") {if ($debug) {print "Slide-";} $count++; $from=nextword; $to=nextword; eval "\$answer$count=0;"; eval "\$textframe$fcount->Scale(-from=> $from, -to => +$to, -showvalue => '1', -orient => 'horizontal', -variable => \\\$answer$count) -> pack (-side => 'left', - +fill => 'x', -expand => '1');"; } elsif ($word eq "label") {if ($debug) {print "Label-";} $things=nextword; eval "\$textframe$fcount->Label(-text => \$things)->pa +ck(-side => 'left');"; } elsif ($word eq "eval") {if ($debug) {print "Eval-";} $evalcount++; $default=nextword; $things=nextword; eval "sub sub$evalcount {$things};"; eval "\$textframe$fcount->Button(-text => \$default, - +command => sub {sub$evalcount;})->pack(-side => 'left', -expand => '1', -fill => 'both');"; } elsif ($word eq "OK") {if ($debug) {print "OK-";} $evalcount++; eval "\$textframe$fcount->Button(-text => \"OK\", -com +mand => sub {done;})->pack(-side => 'left', -expand => '1', -fill => 'both');"; } elsif ($word eq "fill") {if ($debug) {print "Fill-";} $things=""; eval "\$textframe$fcount->Label(-text => \" \")->pack( +-side => 'left', -expand => '1');"; } elsif ($word eq "hr") {if ($debug) {print "Hr-";} $things=""; eval "\$textframe$fcount->Text(-text => \"-\")->pack(- +side => 'left', -expand => '1', -fill => 'x');"; } elsif ($word eq "crlf") {if ($debug) {print "Crlf-";} $fcount++; # unbelieveably - frames must explicitly be built in the global +context and cannot be eval'ed here # eval "\$textframe$fcount = \$MW->Frame()->pack(-expand + => '1', -fill => 'both',-side => 'top');"; } elsif ($word eq "entry") {if ($debug) {print "Entry-";} $count++; $things=nextword; $default=nextword; eval "\$answer$count='$default';"; if ($things eq "fill") {eval "\$textframe$fcount->Entr +y(-textvariable=> \\\$answer$count, -width => 1, -relief => 'sunken')->pack(-side => 'left', -expand => ' +1', -fill => 'x');";} else {eval "\$textframe$fcount->Entry(-textvariable +=> \\\$answer$count, -width => $things, -relief => 'sunken')-> pack(-side => 'left');";} } elsif ($word eq "label") {if ($debug) {print "Label-";} $things=nextword; eval "\$textframe$fcount->Label(-text => \$things)->pa +ck(-side => 'left');"; } elsif ($word eq "file") {if ($debug) {print "File-";} $default=nextword; @content=`cat $default`; foreach $things (@content) {chomp($things);unshift(@to +do,$things);if ($things eq "crlf") {$frames++;};}; return; } else {print "Ignoring strange word $word\n";} } } $frames=1; my $MW = MainWindow->new; $MW->title("FormBox"); $MW->Label(-text => "Version 1.1 - Fred Cohen")->pack(-side => 'bottom +'); eval "\$textframe$fcount = \$MW->Frame()->pack(-expand => '1', -fill = +> 'both',-side => 'top');"; while (scalar(@ARGV) > 0) {$nextitem=$ARGV[0];unshift(@todo, $nextitem +);shift; if ($nextitem eq "crlf") {$frames++;};} if ($debug) {print "building $frames frames\n";} for ($fdone=0;$fdone<=$frames;$fdone++) {eval "\$textframe$fdone = \$MW->Frame()->pack(-expand => '1', -fill => 'both',-side => ' +top');";} if ($debug) {for $x (@todo) {print "$x:";};} while (scalar(@todo) > 0) {for (;$fdone<=$frames;$fdone++) {eval "\$textframe$fdone = \$MW->Frame()->pack(-expand => '1', -fill => 'both',-s +ide => 'top');";} godoit();} MainLoop;

In reply to formbox.pl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.