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

I'm working on a project where I ask the user to input some data, which gets stored in a database, and then I give the user the chance to input more, change what they've done, or exit out. To do this I pull (some) of what they've entered back out of the database to present it to them.

My test script has uncovered an interesting problem: The first entry works just fine. Data is entered and presented to the user. The second entry however does not get pulled back out of the database and presented to the user. This is despite the fact that the data does get put into the database. (I am using Test::Expect and Test::DatabaseRow to test what is presented to the user and what is stored in the database, respectively.)

Any idea on what is going on?

use strict; use warnings; use DBI; use Term::UI; use Term::Readline; # Offers choices to the user to add or edit a server. sub server { my $term = $_[0]; # Get the database connection, and prepare the query # to get the machine names. my $dbh = db_connect(); my $stmt = $dbh->prepare( q[SELECT name FROM ] . $dbh->quote_identifier('machines') . q[ ORDER BY name;] ); # Read the machine names from the database. my $menu_array = $dbh->selectall_arrayref( $stmt ) or carp("Error reading from the database: $DBI::errstr\n"); $$menu_array[0] = [] unless defined( $$menu_array[0] ); # Now we present the choices to the user, and enter the UI loop. my $answer = $term->get_reply( prompt => 'Select Server: ', choices => [ 'Exit', 'Add Server', @{$$menu_a +rray[0]} ] ); while ( defined($answer) and $answer ne 'Exit' ) { print "$answer\n" if defined($debug); # If they got this far, they either want to add a server, # or they want to manage one. (get_reply() checked for invali +d # responses, and we checked for 'Exit' above.) if ( $answer eq 'Add Server' ) { add_server( $term, $dbh ); } else { manage_server( $term, $dbh, $answer ); } } ## end while ( defined($answer) ...) continue { $menu_array = $dbh->selectall_arrayref( $stmt ) or carp("Error reading from the database: $DBI::errstr\n"); $$menu_array[0] = [] unless defined( $$menu_array[0] ); $answer = $term->get_reply( prompt => 'Select Server: ', choices => [ 'Exit', 'Add Server', @{$$menu_a +rray[0]} ] ); } ## end continue } ## end sub server sub add_server { my ( $term, $dbh ) = @_; # Get the List of OS's from the database. # They then need to be munged into a flat list. my $stmt = $dbh->prepare( q[SELECT name FROM ] . $dbh->quote_identifier('os_types') . q[ ORDER BY name;] ); $stmt->execute() or carp("Error reading from the database: $DBI::e +rrstr\n"); my $os_list = $stmt->fetchall_arrayref( [0] ) or carp("Error reading from the database: $DBI::errstr\n"); $$os_list[0] = [] unless defined( $$os_list[0] ); my @flat_list = map { $$_[0] } @$os_list; # Ok, now we can get all the information from the user... my $server = $term->get_reply( prompt => 'Enter the name of the new server: ' + ); my $ip = $term->get_reply( prompt => 'Enter the IP address of the new ser +ver: ' ); my $os = $term->get_reply( prompt => 'Which OS is on the new serv +er: ', choices => \@flat_list ); if ( !$term->ask_yn( prompt => 'Is this correct?', print_me => "Server Name- $server\nIP Address- $ip\nO +S- $os\n", default => 'y' ) ) { print "Aborting.\n"; return; } ## end if ( !$term->ask_yn( prompt...)) # And once they've given it to us, enter it into the database. my $stmt1 = $dbh->prepare( q[INSERT INTO ] . $dbh->quote_identifier('machines') . q[ ( name, ip, os ) VALUES ( ?, ?, ? ) ] ); my $stmt2 = $dbh->prepare( q[INSERT INTO ] . $dbh->quote_identifier('machine_groups') . q[ ( group_name, machine_name ) VALUES ( ?, ? ) + ] ); if ( $stmt1->execute( $server, $ip, $os ) and $stmt2->execute( $server, $server ) ) { $dbh->commit() or carp("Commit Failed: $DBI::errstr\n"); print "Insert Succeded.\n"; } else { print "Insert Failed: $DBI::errstr\n"; $dbh->rollback(); } return; } ## end sub add_server

Replies are listed 'Best First'.
Re: Entering and Retrieving data from a database.
by lostjimmy (Chaplain) on Feb 17, 2010 at 17:12 UTC
    This is your problem: choices => [ 'Exit', 'Add Server', @{$$menu_array[0]} ]

    What $menu_array looks like is something like this:

    [ [ "server_name_1" ], [ "server_name_2" ], ]

    The statement @{$$menu_array[0]} grabs the first element of the array ref, which happens to also be an array ref ([ "server_name_1" ]), uses it as a list, which ends up simply being ( "server_name_1" ). I'm not familiar with the various Term modules, but I assuming that get_reply is expecting a list of answers. Something like this would probably work:

    $answer = $term->get_reply( prompt => 'Select Server: ', choices => [ 'Exit', 'Add Server', map { $_->[0] } + @$menu_array ]);

      Desh, and I'd even dealt with that problem elsewhere in the code. Thanks. I've been staring at this for two days.

Re: Entering and Retrieving data from a database.
by Anonymous Monk on Feb 17, 2010 at 15:57 UTC
    First, try writing the minimal program that fails. Then ask yourself how it failed. That is, did the query return an error or an empty record. If an error, address it. If an empty record, print the query as specified and try it manually.