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

Hola, here's a more refined version of something I whined about before:
##################################### # what this does: Dialog Box that writes a hashed financial +goal (float $num, string $desc) into a .dat file GF # # important variables defined: TextEntry $numNewGoal marked +by Label $tag, TextBox $descNewGoal. ##################################### my $addGoalButn=$features->Button(-text=>'Add Goal', -command= +>sub { $n_prompt=$main->DialogBox(-title=>"Add a \$ value and expl +anation", -buttons=>["OK","Cancel"]), $tag=$n_prompt->add(Label, -text=>"\$"), $numNewGoal=$n_prompt->add(Entry), $descNewGoal=$n_prompt->add(Text, -height=>'5', -width=>'3 +0'), $n_prompt->Show(), $num=$numNewGoal->get(), $desc=$descNewGoal->get("1.0", "end"), #paid upgrade option: rewrite below for Excel/SQL connectivity open(GF, "+<$file_goals") || warn("Your file is empty."), print GF join('\n', $num, $desc), close(GF) });
Should work, right? Wrong. The DialogBox shows OK/Cancel Buttons but no entries. There are a couple of these, plus the appropriate adding trigger buttons to the main GUI that work fine....Any help is appreciated.

Replies are listed 'Best First'.
Re: DialogBox needs Entry, Text
by chromatic (Archbishop) on Aug 11, 2002 at 21:53 UTC

    I don't see any *direct* reason that your entries aren't present, but I do see some other mistakes. You have one repeated syntax error, and one logic error. First, you're separating statements with commas and not semicolons. That makes them into clauses, not separate statements:

    my ($i) = ('bar', 'baz'), print "foo\n"; print "<$i>\n";

    Besides that, if the file open fails, you'll still try to print on a closed filehandle. That's pretty unnecessary.

Re: DialogBox needs Entry, Text
by graff (Chancellor) on Aug 11, 2002 at 23:21 UTC
    Well, this is a bit of a drag, I suppose -- and certainly it's something that the DialogBox doc should be more clear about -- but just using "add(Widget)" isn't enough... you have to "->pack()" it, too. Here's a nutshell example of a version that seemed to work for me; it's not pretty, but...
    my $addGoalButn = $features->Button(-text=>'Add Goal', -command=> \&mkDialogBox, )->pack(); sub mkDialogBox { $n_prompt = $main->DialogBox(-title=>"Add a \$ value and explanati +on", -buttons=>["OK","Cancel"]); $n_prompt->add(Label, -text=>"\$")->pack(-side => 'left'); $numNewGoal = $n_prompt->add(Entry)->pack(-side => 'left'); $descNewGoal = $n_prompt->add(Text, -height=>'5', -width=>'30')->pack(-side => 'left') +; $n_prompt->Show(); $num = $numNewGoal->get(); $desc = $descNewGoal->get("1.0", "end"); #paid upgrade option: rewrite below for Excel/SQL connectivity $desc =~ s/\s*$/\n/; (open(GF, ">>$file_goals") and print GF join("\n", $num, $desc) and close GF) or warn "unable to save date to $file_goals\n"; }
    Note, BTW, a couple subtle changes in the "upgrade" part at the end. (1) it looks like your intent was simply to append to the end of a file, but if it doesn't exist yet, opening "+<$file" will fail. Using ">>$file" will always work (unless there's a disk or permission problem). (2) You don't want to use '\n', unless you really want the literal 2-character string '\n' to show up in the output (i.e. to look exactly like '45\nComment'). (3) It's worth your while to normalize the final whitespace on whatever the user typed into the "description" field; other formatting may be worthwhile as well.