in reply to Includes, strict and pointers.
For example:
and then you can do the following in your scripts:package Mylibrary; use strict; use DBI; my $database="somedb:localhost"; my $username="username"; my $password="password"; sub connect { my $dbh = DBI->connect("DBI:mysql:$database", "$username","$password") or error_fatal("blah"); return $dbh; } sub get_current_vote { my ($dbh) = @_; # if we're not connected (or weren't passed # a connection), connect $dbh ||= connect(); my $sth = $dbh->prepare("SELECT current_vote FROM vote_count;"); $sth->execute() or die $dbh->errstr(); my $current_vote = ($sth->fetchrow_array())[0]; return $current_vote; } 1;
# my funky script use strict; use DBI; use Mylibrary; # connect to library: my $dbh = Mylibrary::connect(); # get current vote: my $current_vote = Mylibrary::get_current_vote($dbh);
Update: Call the file with the Mylibrary package in it Mylibrary.pm, and either store it in the same directory with all the scripts that use it or add a use lib line in your scripts like the following:
Note that the use lib line must be before the use Mylibrary line.# my funky script use strict; use DBI; use lib '/path/to/my/library/'; use Mylibrary;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Includes, strict and pointers.
by joe++ (Friar) on Sep 23, 2002 at 09:04 UTC | |
|
Re: Re: Includes, strict and pointers.
by lagrenouille (Acolyte) on Sep 23, 2002 at 06:36 UTC |