I want to pass the db handle from my perl script to my python script. I want to ensure that python script runs in the same session as the perl one.
I'v tried this so far -
Perl wrapper to call Python subroutine while passing DB Handle. Doesn’t work whether I pass the handle as dbh or $dbh Option1
# ----------------------------------------------------------------- #
# # Connect to MIDB schema and get connection handle
# # ----------------------------------------------------------------- #
my $dbh = ConnDb::connect_to_midb();
#
# ----------------------------------------------------------------- #
# # Call Python subroutine b
# ----------------------------------------------------------------- #
py_eval(<<'END');
from test_phy import b
b(dbh)
END
$ perl c_pl
Traceback (most recent call last):
File "<string>", line 2, in <module>
NameError: name 'dbh' is not defined
Error -- py_eval raised an exception at c_pl line 29.
Option2
# ----------------------------------------------------------------- #
# # Connect to MIDB schema and get connection handle
# # ----------------------------------------------------------------- #
my $dbh = MiDb::connect_to_midb();
#
# ----------------------------------------------------------------- #
# # Import python subroutine b
# ----------------------------------------------------------------- #
#
py_eval(<<'END');
from test_phy import b
END
# ----------------------------------------------------------------- #
# Call Python subroutine.
# ----------------------------------------------------------------- #
py_bind_func("main::Bar", "__main__", "b($dbh)");
print "The meaning of life is: ", Bar(), "\n";
$ perl a_pl
'b(DBI::db=HASH(0x23e6870))' is not a callable object at (eval 20) line 3.
Ideally, you should not pass DB handle to another process. Do the operation then close connection and open when you need it in the 2nd process. Let alone passing from Perl to python which doesn't make sense.