in reply to variables / consts for different scripts
# In file Database.pm package Database; our @ISA = qw(Exporter); our @EXPORT_OK = qw( DB_connect DB_disconnect ); use DBI; my %config = ( type => 'mysql', name => 'SUN', ); sub DB_connect { # Allow the user to override anything they want to in %config %config = (%config, @_); my $connect_string = join(':', 'DBI', $config{type}, $config{name}, ); my $dbh = DBI->connect( $connect_string, $config{user}, $config{password}, $config{options}, ); unless ($dbh) { die "Cannot open connection to $connect_string with $config{us +er} / $config{password}\n" . DBI->errstr, $/; } return $dbh; } sub DB_disconnect { my ($dbh) = @_; $dbh->disconnect if UNIVERSAL::isa($dbh, 'DBI'); return 1; } 1;
You can do something similar with your files. Now, you have connecting to a database abstracted out into something you can reuse.
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: variables / consts for different scripts
by Skeeve (Parson) on Aug 28, 2003 at 13:30 UTC | |
by dragonchild (Archbishop) on Aug 28, 2003 at 13:48 UTC |