DBI OO clash

From: Kurt George Gjerde (kurt.gjerde_at_infomedia.uib.no)
Date: 10/27/04

  • Next message: Alessandro Bonvicini: "5.8.4 test-simple.ppd not found"
    Date: Wed, 27 Oct 2004 12:38:13 +0200
    To: Perl - DBI users <dbi-users@perl.org>
    
    
    

    Hi,

    Hopefully someone can explain this to me.

    The attached script causes an object's destructor not to be called!

    This happens when a non-existing hash key is bound to a DBI statement
    handle with execute() (or bind_param()) (see the save() method).

    If the hash key exists (in the object / blessed hashref) then it's ok
    (also for undefined values). If not, the destructor (DESTROY) is not
    called; not even during global garbage collection.

    My line-up is: Win32, Perl 5.8.3, DBI 1.43 (PPM states 1.45)

    -Kurt.

    -- 
    Kurt George Gjerde
    kurt.gjerde@infomedia.uib.no
    ICQ:156792385  TLF:55588396
    Universitetet i Bergen
    
    

    #!/user/bin/perl

    #
    # The following script causes the object's destructor not to be called!
    #
    # This happens when a non-existing hash key is bound to a DBI statement
    # handle (in the save() method).
    #
    # If 'extra' is defined (in the object / blessed hashref) then it's ok.
    # If not, the destructor (DESTROY) is not called; not even during global
    # garbage collection.
    #
    # My line-up is: Win32, Perl 5.8.3, DBI 1.43 (PPM states 1.45)
    #

    #####
    # This script uses MySQL.
    #####
    # SEE BELOW FOR CREATE TABLE STATEMENT.
    #####


    use strict;
    use DBI;

    print "DBI VERSION: $DBI::VERSION\n\n";


    my $dbh = DBI->connect('DBI:mysql:database=test','root','PASSWORD');

    ### THIS IS OK
    # my $person = Person->new( name=>'Janet', age=>'38', extra=>'asd', dbh=>$dbh );

    ### THIS IS NOT
    my $person = Person->new( name=>'Janet', age=>'38', dbh=>$dbh );


    $person->save();
    $person->delete();



    ##########

    package Person;

    sub new {
      my ($class, %args) = @_;
      my $self = \%args;
      bless $self, $class;
      print "NEW $self->{name}\n";
      return $self;
    }

    sub DESTROY {
      my $self = shift;
      print "DESTROYED $self->{name}\n";
    }

    sub save {
      my $self = shift;
      my $sth = $self->{dbh}->prepare('insert into dbi_test (name, age, extra) values (?,?,?)');

      $sth->execute( $self->{name}, $self->{age}, $self->{extra} );

      $sth->finish;
      print "SAVED $self->{name}\n";
    }

    sub delete {
      my $self = shift;
      my $sth = $self->{dbh}->prepare('delete from dbi_test where name=?');
      $sth->execute($self->{name});
      $sth->finish;
      print "DELETED $self->{name}\n";
    }


    __END__


    # CREATE TABLE dbi_test (mysql)

    drop table if exists dbi_test;

    create table dbi_test (
      name varchar(32),
      age int,
      extra varchar(32),
      
      primary key (name)
    ) type=myisam;


  • Next message: Alessandro Bonvicini: "5.8.4 test-simple.ppd not found"

    Relevant Pages

    • Re: Scope of variables in C# script section of an ASP.NET page
      ... > class that some of the other C# script functions can access. ... > My thinking was I could open up a TextWriter in the constructor of the ... > class and then close the file in the destructor. ... this C# scripting seems to be dissimilar to plain old C# ...
      (microsoft.public.dotnet.framework.aspnet)
    • Re: destructors
      ... I don't have to explicitly define a destructor ... In terms of freeing memory it makes sense that it isn't really an ... Once a request (script) has been ... the server automagically restores the memory ...
      (comp.lang.php)