baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi,

my question is , can i somehow pass a variable to a module at load time. example:

i now i can do this :

use foo qw(copy);

to call only the copy subroutine but if i have example like this :

First script:

use strict; use warnings; use lib "."; use Max; my $rrr = Max->new(); my $pform = "GeneID varchar(30) not null PRIMARY KEY"; my $tname = "PT"; $rrr->createtable(form => $pform, table => $tname);
second script:
use strict; use warnings; use lib "."; use Max; my $rrr = Max->new(); my $pform = "GeneID varchar(30) not null PRIMARY KEY"; my $tname = "PT"; $rrr->createtable(form => $pform, table => $tname);
module.pm
package Max; use strict; use DBI; my $robi; <------------ DEFINE THIS VARIABLE AT LOAD TIME my $dbh = DBI->connect("dbi:SQLite:dbname=$robi", "", "",{RaiseError=> +1, AutoCommit=>1}); + print "$robi"; ################################################## sub new { ################################################## my ($class) = @_; my $hash = {}; bless($hash,$class); } ################################################## sub createtable{ ################################################## my ($self, %arg)=@_; my $form = $self->{form}=$arg{form}; my $tabler = $self->{table}=$arg{table}; $self->drop(argument => $tabler); my $stm = "create table $tabler ($form)"; $self->do_it_db(argument => $stm); } ################################################## sub do_it_db { ################################################## my ($self, %arg) =@_; my $stm = $self->{argument}=$arg{argument}; my $st = $dbh->prepare($stm); $st ->execute(); } ################################################## sub drop { ################################################## my ($self,%arg) = @_; my $arg = $self->{argument}=$arg{argument}; my $statement = "drop table if exists $arg"; $self-> do_it_db(argument => $statement) } 1;
the point here is that i want to create at load time two different databases. first one when i start the first script and the second one when i start the second script and then to do the same stuff in side of them. the object my $dbh is specific for Max.pm so the form in which it is called should be preserved

please don't mind the amount of code i try to create a useful example !

Replies are listed 'Best First'.
Re: pass variable at load time
by ccn (Vicar) on Nov 03, 2008 at 21:57 UTC

    You can set global variable in BEGIN block before use Max; line;

    BEGIN { $Max::robi = 'dbname'; }; use Max;

    And

    package Max; use strict; use DBI; use Carp; croak '$Max::robi undefined' unless defined $Max::robi; my $dbh = DBI->connect("dbi:SQLite:dbname=$Max::robi", "", "",{RaiseEr +ror=>1, AutoCommit=>1});
Re: pass variable at load time
by ikegami (Patriarch) on Nov 03, 2008 at 22:24 UTC

    How about

    use strict; use warnings; package Max; use DBI qw( ); my $robi; my $dbh; sub import { my $class = shift; if (@_) { $robi = shift; $dbh = DBI->connect( "dbi:SQLite:dbname=$robi", "", "", { RaiseError => 1, AutoCommit => 1}, ); } } ... 1;
    use Max '...dbname...';

    Or

    use strict; use warnings; package Max; use DBI qw( ); my $robi; my $dbh; sub init { (my $class, $robi) = @_; $dbh = DBI->connect( "dbi:SQLite:dbname=$robi", "", "", { RaiseError => 1, AutoCommit => 1}, ); } ... 1;
    use Max; Max->init('...dbname...');
Re: pass variable at load time
by oxone (Friar) on Nov 03, 2008 at 23:13 UTC
    If you follow Ikegami's first example above (which you probably should) and your module is using Exporter, note that creating your own import function will interfere with Exporter's behaviour.

    If so, the Exporter docs explain how to deal with this.