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

Hello, I have a quick popup question for you. I want a popup that pulls values and label data from a db. I keep getting a "Can't coerce array into hash" error. What am I missing? Here's the code:
my $rc=0; my ($u,$p); my $userid=$query->param('userid'); my $plain_pass=$query->param('password'); unless(open(PFD,$db)) my @users; while (<PFD>) { chomp; ($u,$p,$path)=split('\|',$_); push @users, $u; } close(PFD); unless(open(PFD,$db)) { &printError("Could not open user database"); return; } my @dir; while (<PFD>) { chomp; ($u,$p,$path)=split('\|',$_); push @dir, $path; } close(PFD); print $query->popup_menu(-name=>'directory', -values=>\@dir, -labels=>\@dir=>\@users);
Thanks for any help that you can provide.

Replies are listed 'Best First'.
Re: Popup Question
by Masem (Monsignor) on May 14, 2001 at 20:38 UTC
    Your code:
    print $query->popup_menu(-name=>'directory', -values=>\@dir, -labels=>\@dir=>\@users);
    That last statement, -labels=>\@dir=>\@users is probably being interpreted by perl as setting the anonymous hash defined by key \@dir, value \@users, and using that as the arguement to -labels, which is expecting an array. I think that "\@dir=>" is unnecessary in your case and can be removed.

    Also, you don't need to read through the database twice. You can fill both @users and @dir in the same pass through the DB.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
(Ovid) Re: Popup Question
by Ovid (Cardinal) on May 14, 2001 at 20:50 UTC
    I've reduced this to a minimal test case to show you a clean way of writing this:
    use CGI; use strict; my $query = CGI->new; my %users; while (<DATA>) { chomp; my ($user,$path)=( split '\|',$_ )[0,2]; warn "DB Error: $user already exists!" if exists $users{ $user }; $users{ $user } = $path; } print $query->popup_menu(-name => 'directory', -values => [sort keys %users], -labels => \%users); __DATA__ bob|asdf|/usr/data/bob alice|qwer|/usr/data/alice
    This prints:
    <select name="directory"> <option value="alice">/usr/data/alice</option> <option value="bob">/usr/data/bob</option> </select>

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Popup Question
by boo_radley (Parson) on May 14, 2001 at 20:38 UTC
    I bet it's this :
    print $query->popup_menu(-name=>'directory', -values=>\@dir, -labels=>\@dir=>\@users);
    The last bit of this, -labels=>\@dir=>\@users is probably malformed (This looks like CGI, but it could be tk, I dunno. I don't work with tk at all.)
    Cutting and pasting does not always save time *g*