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 }
In reply to how to put this db connection code into a function by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |