Hi everybody. I'm using Class::DBI inside a project to access an SQLite database. It works very well, but I'm facing a problem: the path to the database file (the db name) have to be harcoded inside the package.

I have the following packages

The DBI classes

package MyModule::DBI; use base Class::DBI; use warnings; use strict; our $VERSION = '0.01'; my $dsn = "dbi:SQLite:dbname=/path/to/db"; __PACKAGE__ -> set_db('Main', $dsn); 1;

And

package MyModule::Data; use base MyModule::DBI; use warnings; use strict; our $VERSION = '0.01'; __PACKAGE__ -> table('data'); __PACKAGE__ -> columns(All => qw(column1 column2 column3)); 1;

And the main module

package MyModule; use MyModule::Data; use warnings; use strict; our $VERSION = '0.01'; sub new { my ($class, %args) = @_; my $self = bless({%args}, $class); return $self; } 1;

It could be obvious but my question is, is it possible to call the new method and pass the path to the database and use it in MyModule::DBI? If yes, how?

What I want to do is something like

use MyModule; my $obj = MyModule -> new(db_path => '/path/to/file');

Thank you

Alex's Log - http://alexlog.co.cc

In reply to Class::DBI and database name by alexbio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.