Perl programmer for hire: download my resume (PDF).
John Bokma MexIT
freelance Perl programmer

Attempt to free unreferenced scalar, part II

Tuesday, April 25, 2006 | 0 comments

See Attempt to free unreferenced scalar for the first part.

# dbi-dbm-lwp-bug.pl
#
# Results in:
#
#   Attempt to free unreferenced scalar: SV 0x1aa9694,
#   Perl interpreter: 0x224304
#   at C:/Perl/lib/Errno.pm line 15.
#
# for -t1 and -t3
#
# $Id$

use strict;
use warnings;

use DBI;
use LWP::UserAgent;

use Data::Dumper;

my $dbh = DBI->connect('dbi:DBM:');
$dbh->{ RaiseError } = 1;

my $opt = shift;
defined $opt or die <<'USAGE';
options:

    -c      create test DBM database
    -t1     selectall_hashref (fails)
    -t2     selectall_hashref (succeeds)
    -t3     fetchall_hashref (fails)
USAGE

if ( $opt eq '-c' ) {

    $dbh->do( 'CREATE TABLE test ( name TEXT )' );
    $dbh->do( "INSERT INTO test VALUES( 'some name' )" );
    print "Test database has been created in the cwd\n";
    exit;
}

my $query = 'SELECT name FROM test';
my $key_field = 'name';

$opt eq '-t1' and selectall_hashref_fails( $dbh, $query, $key_field );
$opt eq '-t2' and selectall_hashref_succeeds( $dbh, $query, $key_field );
$opt eq '-t3' and fetchall_hashref_fails( $dbh, $query, $key_field );

my $ua = LWP::UserAgent->new();
my $response = $ua->get( 'http://localhost/' );

exit;


sub selectall_hashref_fails {

    my ( $dbh, $query, $key_field ) = @_;

    my $results = $dbh->selectall_hashref( $query, $key_field );

    print Dumper $results;
}


sub fetchall_hashref_fails {

    my ( $dbh, $query, $key_field ) = @_;

    my $sth = $dbh->prepare( $query );
    $sth->execute;

    my $results = $sth->fetchall_hashref( $key_field );

    $sth->finish;

    print Dumper $results;
}


sub selectall_hashref_succeeds {

    my ( $dbh, $query, $key_field ) = @_;

    my $sth = $dbh->prepare( $query );
    $sth->execute();

    my $results;

    while ( my $row = $sth->fetchrow_hashref() ) {

        $results->{ $row->{ $key_field } } = $row;
    }
    $sth->finish;

    print Dumper $results;
}

First create the test database with the -c option, then try the -t1 option (fails), -t2 option (succeeds), and -t3 option (fails).

Also today

Please post a comment | read 0 comments | RSS feed