Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
how would i put this code to connect to a database in a function. The code itself is inside a function which needs to 'return' if the connection fails. This is the code as it is now:
The only thing i can think of is really convoluted and i don't know how to tell if the return type is a string or a connection object. I borrowed the typeof function from another language :)#database connection variables defined here with 'our' scope sub function1 { eval { my $db = DBI->connect("DBI:mysql:database=$sql_database;host=$ +sql_host;port=$sql_port", $username, $password,{'RaiseError' => 1}); }; return "some value", "Error: $@" if $@; }
thanks a lotsub function1 { my $db=&connect(pass in connection parameters...) if typeof $db = string return "some value", "Error: $db"; } sub connect{ eval { my $db = DBI->connect("DBI:mysql:database=$sql_database;ho +st=$sql_host;port=$sql_port", $username, $password,{'RaiseError' => 1 +}); }; if $@ return $@; else return $db }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to put this db connection code into a function
by ELISHEVA (Prior) on Jan 05, 2011 at 08:22 UTC | |
|
Re: how to put this db connection code into a function
by ikegami (Patriarch) on Jan 04, 2011 at 23:12 UTC | |
by ikegami (Patriarch) on Jan 04, 2011 at 23:28 UTC | |
by Anonymous Monk on Jan 05, 2011 at 00:19 UTC | |
by ikegami (Patriarch) on Jan 05, 2011 at 01:11 UTC | |
|
Re: how to put this db connection code into a function
by Anonyrnous Monk (Hermit) on Jan 05, 2011 at 00:30 UTC |