Peter Rabbitson wrote: > On Thu, Jun 30, 2005 at 09:05:00AM -0600, Wiggins d'Anconia wrote: >
[snip previous round] > > I am sorry, I wasn't clear enough. I am aware of eval returning the last > statements return value, thus the possibility of having an undef result. > I am using it exclusively for control of "do or die" tasks, as DBI > operations for example, where even if it returns 0 it still evalutes > true. The reason I do () an entire block is because I specify additional > error messages. Like this: > > my $dsn = join (':', 'DBI', > $cfg->{engine}, > join (';', "database=$cfg->{database_name}", > "host=$cfg->{host}", > "port=$cfg->{port}", > ), > ); > > my $dbh; > eval { $dbh = DBI->connect ($dsn, $auth->{user}, $auth->{pass}, { > AutoCommit => 0, > RaiseError => 1, > PrintError => 0, > } > ) > } > or do { > $ERROR = "Unable to connect to '$dsn':\n" . ($DBI::errstr || 'Unknown > error'); > return undef; > }; > > return $dbh; > But again, you have defeated the purpose of using 'RaiseError' because you are catching the exception, not doing anything with it, and then doing your error handling manually. In this case why not just, my $dbh = DBI->connect($dsn, $auth->{user}, $auth->{pass}, { AutoCommit => 0, RaiseError => 0, PrintError => 0, } ); unless ($dbh) { # error handling.... } return $dbh; Why use the eval idiom when you can just shut off DBI's exception throwing? Or use the eval and raise errors but catch them with the normal $@ variable. aka... my $dbh = eval { connection stuff, with raise error on }; if ($@) { # error handling.... error is in $@ return undef; } return $dbh; Either way it seems like an odd idiom to use an eval just to get to a do, I think you can do that with just a regular blocks (though I didn't test so maybe not).... { my $dbh = .... # with RaiseError off } or { # error handling }; or at least { my $dbh = .... # with RaiseError off } or do { # error handling }; Either way there shouldn't be a need for eval unless you have raise error on, but in that case you should catch the exception with [EMAIL PROTECTED] http://danconia.org > Thanks for the feedback > > Peter > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>