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

Hi, i have this:
my $query = $conn->prepare("SELECT id, name FROM datos ORDER BY id +"); $query->execute(); while (@row = $query->fetchrow_array) { ($id, $name) = @row; $item = new_item($id, $name); if ($item eq '') { fatal("new_item($id, $name) failed: $item"); } push @il, $item; push @pack, ${$item}; } push @pack, 0; $menu = new_menu(pack "I*", @pack);

But all the options in the menu are made with the same
value, the value of the last register that the query returns
for example i have:

1 - Mario
2 - Diana
3 - Ismael

the menu shows:

3 - Ismael
3 - Ismael
3 - Ismael

why is that?? Am i doing something wrong??
thankx

Replies are listed 'Best First'.
Re: Making a Menu with a Database Query
by TGI (Parson) on Jul 26, 2003 at 03:19 UTC

    There really isn't enough here to solve your problem, but I'll give you some pointers and maybe they'll be of use to you.

    It looks like $item is a reference, based on your usage:

    push @pack, ${$item};
    Are you using item as a hard reference or a soft reference? If you don't know what I mean by this distinction, please read perlref. It is generally a bad idea to use soft references. Unless you thoroughly understand why you shouldn't use them, you shouldn't use them.

    It also looks like you are using global variables for everything. I don't see any use of my, our or local. It is generally a good idea to use lexical variables as opposed to package globals. Using package globals sets you up for some very hard to track bugs. See Coping with Scoping and Use strict warnings and diagnostics or die for more info.

    Try sticking some print or warn statements into your code at different spots. It is a very effective way to track down misbehaving variables. Or you can learn to use the perl debugger, but I've been too (short term) lazy to learn it, and just use prints.

    Seeing some code for new_item may be helpful in diagnosing the problem.


    TGI says moo

Re: Making a Menu with a Database Query
by skyknight (Hermit) on Jul 26, 2003 at 00:25 UTC

    First off... "It's not what you know that kills you, it's what you think you know." Obviously you are doing something wrong if you're not getting the results you want, but how do I know that what is wrong isn't in new_item or new_menu, both of which you use and neither of which you have shown me? Furthermore, I am dismayed by your lack of the "strict" pragma, as you may very well have misnamed a variable in a routine you haven't shown me. Lastly, I am quite bewildered by your usage of ${$item}. Did you actually mean to use a soft reference, and if you did, then why? I very seriously doubt this is doing what you think it is doing, and may very well be the root cause of your troubles.