Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Mysql and Perl Module

by endymion (Acolyte)
on Jun 14, 2012 at 08:41 UTC ( #976144=note: print w/replies, xml ) Need Help??

in reply to Re: Mysql and Perl Module
in thread Mysql and Perl Module

Hello Tospo, the problem is it should be an module we can use for all of our customer scripts. The point is, that we need a module where I can only give a few arguments to it. Is it better to make another sub for the connection ?

Replies are listed 'Best First'.
Re^3: Mysql and Perl Module
by Anonymous Monk on Jun 14, 2012 at 09:26 UTC

    See this example program Re: Open multiple file handles?

    You can easily create a module with a function like this

    sub IntoDbImportCSV { my( $dbiconn, # dbi connection string $colnames, # column names $csvfile, # path to csv file $csvargs, # Text::CSV->new options hash ) = @_; my $dbh = DBI->connect( $dbiconn, undef, undef, { RaiseError => 1, PrintError => 1, }, ); open my($infh), '<', $csvfile ... my $csv_in = ... $dbh->begin_work; my $sth = $dbh->prepare($sql); while ( my $row = $csv_in->getline( $infh ) ) { $sth->execute( @{ $row } ); } $dbh->commit; $dbh->disconnect; } IntoDbImportCSV( 'dbi:SQLite:dbname=temp.test.sqlite', '/path/to/foo.cvs', ['ro', 'sham', 'bo' ], { quote_char => '"', sep_char => ',', allow_loose_escapes => 1, empty_is_undef => 1, binary => 1, auto_diag => 1, }, );
Re^3: Mysql and Perl Module
by tospo (Hermit) on Jun 14, 2012 at 09:29 UTC
    The ideal situation for that scenario is that your module takes some sort of config file with the DSN for the databse connection so that you can ship it with a tailor-made config file for each of your customers.
    Look at something like Config::General to handle such a config.
    Alternatively, you could make the DSN an argument that needs to be supplied by the user when running this script.
    If you have a lot of stuff to do with that database then it would also be worth exploring something like DBIx::Class to handle your database in a much more abstract way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976144]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-01-20 23:47 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.