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

Most Honorables,

I am running into an interesting challenge that I hope your glowing wit will lighten. I have an Entry widget in which my user can only enter the letters DATSOU. The program allows the user to keep some default that can be loaded. When my user saves default from that widget, I pick up the textvariable (he cannot type any other letters than DATSOU)and place it in my default file (everything is fine here). When the user loads the default from the file, I read the file in an array an assign the values with the corresponding variables. However, I get this error:
"Use of uninitialized value in string eq at file.pl line 200, <DEFAULTIN> line 21."
I have other entry widgets where I have restricted my user in other ways, like can type only numerical value, or only characters and those work just great. Enough chatting, here is my code:
#!/usr/bin/perl -w use strict; use Tk; use Tk::Entry; my @arraydefault; my $copytf; $copytf = $page2 ->Entry (-textvariable=> \$copyentry, -validate=> 'key', -validatecommand=> sub{$_[1] =~/['a','d', 't', 's', 'o', ' +u', 'A','D', 'T', 'S', 'O', 'U']/}, -invalidcommand=> sub{$page2->bell})->pack; my $loaddefaultb=>$mw->Button (-text=>'Load Defaults', -command => sub + {loaddef()})->pack; MainLoop; sub loaddef(){ open (DEFAULTIN, "<default.txt"); while(<DEFAULTIN>){ chomp; @arraydefault = split /,/; }#while $copyentry=$arraydefault[11];}
Thanks in advance for your precious time,
Looking foward to your replies,
Claire

Replies are listed 'Best First'.
Re: Changing the Textvariable of a restricted entry widget (TK)
by Tanktalus (Canon) on Aug 03, 2005 at 03:42 UTC

    I'm not really up on Tk programming, but that validate command sub looks fishy. ;-)

    -validatecommand => sub { $_[1] =~ /^[aAdDtTsSoOuU]*$/; },

    Comments:

    1. Whitespace is your friend. Use some. ;-)
    2. Check out the perlre document on how regular expressions work. You don't list the characters in quotes, separated by commas. Think of regular expressions as an entirely separate language from perl. It really is. Mostly.
    3. You want to check that the entire string is made up of only DATSOU letters - so you need to match the whole string. ^ matches the beginning, $ matches the end. The string in between is made up of zero or more (specified by the *) of the characters in the brackets.
    4. You can also use /[datsou]/i - the i at the end says this is case insensitive. It is also usually slower, but it's more readable. This would be the way I'd usually go myself, but I also wanted to show you code that was as close to what you already had as I could.
    5. Hope that helps.

      His validate command sub is fine. the validatecommand sub takes several args, and the second one is the char being added or deleted. It is just one char, whether use ^ and $ is not important here.

Re: Changing the Textvariable of a restricted entry widget (TK)
by graff (Chancellor) on Aug 03, 2005 at 05:06 UTC
    Just to clarify, "Use of uninitialized value in string eq" is a warning (because you have wisely put "-w" on the first line of the script). It is not an error. It is simply telling you that at line 200 in file.pl, you are comparing two values with the "eq" operator, and one of them has not been initialized yet (it is "undef" at the point where that line of code is executed).

    So is that really a problem? You don't actually say... is there something that the script is supposed to be doing that it does not do? Is it doing something (besides printing the warning) that it should not do?

    You don't seem to be showing us line 200 of file.pl, so we don't know what's actually going on. It's not even clear whether it has anything at all to do with your DEFAULTIN file handle. (Maybe it isn't related, but if it is, maybe knowing what is at line 21 of "default.txt" would be relevant.)