Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Can't use string as HASH ref

by dimmesdale (Friar)
on Jul 23, 2002 at 15:20 UTC ( [id://184447] : perlquestion . print w/replies, xml ) Need Help??

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

Okay, Tye, I'll post it. I hope this helps. I'll try to explain the problem more in depth. I have two optionmenus and I want them to interact -- one of them contains directories, the other contains files (certain files connected to that directory).

I created a callback on the option menu that holds the directory, but apart from other problems during testing yesterday, doday I'm getting an error message: Can't use string "string here" as a HASH ref. Here's the code, some things, of course, taken out.

sub generate_options { ... opendir(DIR,$_[0]->{base}); my @subdirs = grep /[Tt][Ss]\d+/, readdir(DIR); closedir(DIR); ... my $nb = $mw->NoteBook->grid; ... for(1..$_[0]->{conds}) { $page->[$_-1] = $nb->add("Page$_", -label => "Condition $_") } for(1..$_[0]->{conds}) { ... my $dom = $page->[$_-1]->Optionmenu( -variable => \$info->{subdir}->[$_-1], -options => [@subdirs], ); my $fom = $page->[$_-1]->Optionmenu( -variable => \$info->{file}->[$_-1], -options => get_files ($_[0]->{base},\@subdirs, 0), ); # This here is where I get into trouble: $info->{subdir}... # is spitting out hash ref error described above $dom->configure(-command => sub { $fom->configure(-options => get_files($_[0]->{base},$info->{subdir}->[$_-1])) }); my $b = $page->[$_-1]->Button( -text => "Set", -command => [\&record, { info => $info, indx => $_-1 } +], ); ... $dom->grid( $fom ); ... } ... } sub get_files { opendir(DIR,$_[0] . '\\' . $_[1]->[$_[2]]); my @files = grep /.txt$/, readdir(DIR); closedir(DIR); return [@files]; }

Replies are listed 'Best First'.
Re: Can't use string as HASH ref
by Abigail-II (Bishop) on Jul 23, 2002 at 15:51 UTC
    That probably means $info contains string here when the callback happens. Since you haven't shown us any code that declares or sets $info, not much more can be said.


      I say  my $info

      I want to leave it to the Optionmenu to 'declare' which directory $info->{subdir}->[$indx] shoud be. Is there anyway to do that? I tried to see what Ev(...) would do, but I still got the original error.

        Where do you have the my $info? And where do you set $info. I suggest carefully inspecting your code and see where $info gets modified. It's very likely that that's were it's going wrong.


Re: Can't use string as HASH ref
by hossman (Prior) on Jul 23, 2002 at 22:44 UTC
    The amount of code you have included, and the amount you have removed, makes it really hard to get a clear picture of what's going on (I didn't even realize this was a Tk question untill i drilled down your back and forth with Abigail)

    Can you post a short sample program (in it's entirety) that demonstrates the problem you are having without outside dependacies -- ie: make a single widow with a single menu, use a hard coded list of options/directories, have "get_files" return [ map {"$_[0]/$_"; } qw(yakko wakko dot) ], etc....

    A small example that people can read completely run is extremely valuable -- and the process of making it can help you isolate where/what exactly the problem is.