On Feb 17, 2004, at 5:07 PM, Andrew Gaffney wrote:
In my module, I created a function that connects to a MySQL database and then returns a DB handle. In the script that receives the DB handle, I removed the 'use DBI;' line. I can make queries against the database without problems. Why does this work?
I presume that you did the
package My::DB use DBI;
and that your function is say
sub do_get_db_handle { .... $dbh = DBI->connect($data_source, $user, $pass, $driver); ... $dbh; }
hence it is called like
use My::DB; ... my $db = do_get_db_handle(@arglist); ... $rv = $db->do($statement);
That is the gist of my code, yes.
as Will has noted he thinks that the $dbh is a 'blessed' referece, if you rummage around inside it you will find that yes, it is a blessed reference. Since your module has already asserted the
'use DBI'
it will have already both 'required' and 'imported' all of the stuff 'needed' to understand what a blessed reference to a DBI is all about.
As such your calling script did not need to make the 'use DBI' since it would be required IN your module. This is a part of how/why one likes to encapsulate things into perl modules so that one can hide the common repeatively repeated redundent parts without having to keep retyping them over and over and over again in each new script that you need.
This is what I didn't quite understand. I didn't realize that Perl's "black magic" allowed the blessed reference to refer back to the object already in memory. In effect, the blessed reference carries around the entire class object with it.
-- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>