RE: embedding DB handle in object (was RE: adding key to DB objec t)

From: Will Rutherdale (Will.Rutherdale_at_sciatl.com)
Date: 03/30/05


To: dbi-users@perl.org
Date: Tue, 29 Mar 2005 18:31:57 -0500

my $dbh = DBI->connect( .... );
my $big_honkin_data_structure = { house => "semi", car => "oldsmobile", dbh
=> $dbh,
                                  age => 43, ... };
...

myFavouriteSubroutine( $big_honkin_data_structure->{dbh}, 3 );

...

sub myFavouriteSubroutine
{
    my ( $dbh, $num ) = @_;
    my $data = $dbh->selectall_arrayref( .... );
    ....
}

This is for simple data structures and you can do a lot of things with this
style. Objects are more complex and not needed for many applications. This
is basic Perl and not properly part of this mailing list.

Books have been suggested recently and are mentioned in perl.org. For
instance, _Programming Perl_ and _Perl Cookbook_.

-Will

-----Original Message-----
From: Brandon Metcalf [mailto:bmetcalf@nortel.com]
Sent: Tuesday 29 March 2005 17:59
To: Rutherdale, Will
Cc: dbi-users@perl.org
Subject: embedding DB handle in object (was RE: adding key to DB object)

In trying to take your suggesting and embed a DB handle in my own object,
this is what i've come up with. However, a error is being thrown when DBI
is trying to destory the handle which I understand. What I don't get is how
to do this properly.

This may be inappropriate for this forum.

  $ cat jj
  #!/usr/bin/perl

  use strict;
  use warnings;

  use ii;

  my $dbh = ii->dbconnect_new();
  __END__
  $ cat ii.pm
  package ii;

  use strict;
  use warnings;

  use DBI;

  use vars qw{ @EXPORT @ISA};

  use Exporter;
  @ISA = qw{ Exporter DBI::db };
  @EXPORT = qw{ &dbconnect_new };

  sub dbconnect_new {
     my ($pkg, $dbname) = @_;

     my $dbh;
     $dbh->{dbhandle} = DBI->connect("dbi:Pg:dbname='mydb';host='myhost'");

     bless $dbh, $pkg;
  }
  1;
  __END__
  $ ./jj
  SV = RV(0x810eb70) at 0xbfffdb10
    REFCNT = 1
    FLAGS = (ROK,READONLY)
    RV = 0x8108240
          (in cleanup) dbih_getcom handle ii=HASH(0x8108240) is not a DBI
handle (has no magic).

-- 
Brandon
     - - - - - - -  Appended by Scientific-Atlanta, Inc.  - - - - - - -  
This e-mail and any attachments may contain information which is confidential, proprietary, privileged or otherwise protected by law. The information is solely intended for the named addressee (or a person responsible for delivering it to the addressee). If you are not the intended recipient of this message, you are not authorized to read, print, retain, copy or disseminate this message or any part of it. If you have received this e-mail in error, please notify the sender immediately by return e-mail and delete it from your computer.


Relevant Pages

  • Re: debugger exiting
    ... strict and warnings pragmas. ... I think portraying Perl as a command-line tool limits it to fewer platforms than ... work only as a Unix shell command line. ...
    (perl.beginners)
  • Re: dns querry script.
    ... use warnings; ... use strict; ... C:\Dload> perl dns.pl ...
    (comp.lang.perl.misc)
  • Re: Any way to access global variable in Perl script from one module file?
    ... use strict; ... use warnings; ... a separate process has a completely separate memory space. ... There is probably no reason to create a new perl ...
    (comp.lang.perl.misc)
  • Re: Debug Help Please
    ... In Perl there is the expression TIMTOWTDI which means that you will probably get different opinions on "The Right Way" to do something in Perl. ... use strict; ... That is a specious argument because you can disable specific strictures or warnings in local scope: ... for all Failures ...
    (perl.beginners)
  • Re: List Variable becomes undefined inexplicably
    ... warnings and strictures very often find bugs in programs. ... It is inconsiderate of your audience to post without warnings and strict. ... There is a Perl and there is a perl. ... chomp $LINE; ...
    (comp.lang.perl.misc)