baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.pmuse 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);
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 preservedpackage 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;
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 | |
Re: pass variable at load time
by ikegami (Patriarch) on Nov 03, 2008 at 22:24 UTC | |
Re: pass variable at load time
by oxone (Friar) on Nov 03, 2008 at 23:13 UTC |