[WindyCity-pm] || return a value?

Samuel Halicke samh at fnal.gov
Fri Jun 27 14:21:26 PDT 2008


On Jun 27, 2008, at 3:26 PM, Richard Reina wrote:

> I am trying to write a statement like:
>
> #!/usr/bin/perl5 -w
>
> sub display {
>
> my $dbh = DBI->connect(blah, blah, ...) || return 2; # return 2  
> means no connection possible
>
>
> snip...
>
> } # end of sub display
> 1;
>
> However, the value that is returned ( even if the connection fails )  
> is the one at the end of the sub.
>
> Does anyone know what I might be doing wrong?

Use eval. DBI will die on error, so your condition is not being reached.

eval {
     $dbh = DBI->connect(x, y, z);
}

return 2 if $@;

perldoc -f eval for more info. Also look into the $DBI::errstr value.

Cheers,
Sam

--
Samuel Halicke		samh at fnal.gov
FNAL/CD/SF/FEF		FCC/252/U
(T): 630.840.4308	(P): 630.266.0042



More information about the WindyCity-pm mailing list