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

The basic representation of my prog:
my %file_system = ( '/path/subpath1/'=>['file1', 'file2', 'file3'], '/path/=>['fil01', 'fil02', 'fil03', 'fil04', 'fil05'], '/'=>['f001', 'f002'] ); my $cpath = ''; my @folders = sort keys %file_system; for my $folder (@folders){ print "$file_system{$folder}\n"; #Everythng is ok. Prints an array re +fernce, such as: ARRAY(0x3381e118) print join(';', @{$file_system{$folder}}), "\n"; #Everythng is ok. Pr +ints an array } my $listFolders = $win->BrowseEntry( -label=>'Folder:', -choices=>[@folders], #Also tried \@folders, and also, omitting -choi +ces parameter, used $listFolders->insert('end', @folders) -variable=>\$cpath, -browsecmd=>\&listFiles(); )->pack( -side=>'top', -anchor=>'nw', -fill=>'x', -expand=>1 ); #Testing $cpath, after selection: print "$file_system{$cpath}\n"; #Sometimes prints an array refernce, s +uch as: ARRAY(0x3381e118), sometimes not, even, though, visually, the + $cpath looks the same as the according path in the %fule_system var print join(';', @{$file_system{$folder}}), "\n"; #Sometime prints an a +rray, sometimes an array is NULL. #If I iterate keys %file_system, I see the path, that's in $cpath var, + but for some reason, it doesn't match. #Testing $cpath inside the function called from the Tk widget: sub listFiles{ print "$file_system{$cpath}\n"; #Sometimes prints an array refernce, +such as: ARRAY(0x3381e118), sometimes not, even, though, visually, th +e $cpath looks the same as the according path in the %fule_system var print join(';', @{$file_system{$folder}}), "\n"; #Sometime prints an +array, sometimes an array is NULL. }

Seems, like, sometimes, for some reason, -variable in the BrowseEntry returns the corrupt value, that doesn't match as a key in the original var, although, visually, looks the same, as in the original hash.

Update:

Devel::Peek helped me to reveal that, indeed, it was an encoding problem, as Corion noted. I implicitly set an encoding and the problem went away

Replies are listed 'Best First'.
Re: maybe, a corrupt selected entry in Tk::BrowseEntry
by Corion (Patriarch) on Dec 22, 2014 at 14:47 UTC

    Please show us what $cpath actually contains, instead of describing it as "visually the same". See $Data::Dumper::Useqq for how to make Data::Dumper output invisible characters.

    Also, consider trying to find out what triggers the "sometimes" situations. You might also want to record more information, like whether $file_system{ $cpath } exists instead of blindly grabbing its value.

      Please show us what $cpath actually contains, instead of describing it as "visually the same". See $Data::Dumper::Useqq for how to make Data::Dumper output invisible characters.
      the value in a @folders array element:
      /media/igor/chmk/home/zamutnii/BackUp/LanBackup/СЭД/FkClnt1/SUBSYS/PRINT/RTF/
      the value in %file_system's key:
      /media/igor/chmk/home/zamutnii/BackUp/LanBackup/СЭД/FkClnt1/SUBSYS/PRINT/RTF/
      the value in $cpath:
      /media/igor/chmk/home/zamutnii/BackUp/LanBackup/СЭД/FkClnt1/SUBSYS/PRINT/RTF/
      the first two match, but the last one doesn't match with either.
      I will check out Data Dumper
      Also, consider trying to find out what triggers the "sometimes" situations. You might also want to record more information, like whether $file_system{ $cpath } exists instead of blindly grabbing its value.
      I gave examples in my code that I was testing it for existence. A wish to solve what triggers, is exactly, the reason I posted the question, because, I've been trying to solve this problem for a one a half of the day already.

        Most likely, you have two strings that are similar in how they display but are different in how they are encoded. The easiest way to get two such strings is by reading one from a file and the other from the filesystem, for example via readdir.

        If you are certain that your file system stores filenames as UTF-8, you can Encode::decode the filenames. You should do likewise with the filenames read from your file or stored in your program code. That way, the filenames should be consistent.

Re: maybe, a corrupt selected entry in Tk::BrowseEntry
by Anonymous Monk on Dec 22, 2014 at 14:50 UTC
    If I iterate keys %file_system, I see the path, that's in $cpath var, but for some reason, it doesn't match.
    Well, this is what Devel::Peek is for... I'd try
    use Devel::Peek; Dump $cpath;
      Yea, I will check out, what Devel Peek will Dump for me. Thanx. I will edit the original question to post my results.
        Dump array keys too. So far it looks like an encoding issue to me, btw...