On Thu, Jun 30, 2005 at 09:05:00AM -0600, Wiggins d'Anconia wrote:
> Peter Rabbitson wrote:
> > Hello everyone,
> > Here and there on the web I encounter claims that the do {} operator is
> > depreciated. However I find it convenient to do things like:
> >
> > eval { some stuff } or do { some multiline error handling };
> >
> > is this a bad practice?
> >
> > Thanks
> >
> > Peter
> >
> >
>
> Didn't see anything about 'do' being deprecated in the perldoc -f do or
> perldoc perlsyn docs. Though do SUBROUTINE was stated as deprecated.
> The problem (IMHO) with the above is that you can't return a positive
> value from the eval and you haven't error checked whether there was an
> exception thrown, which is one of the more common reasons to use an eval
> in the first place. So I guess my question is, why are you using the
> eval to begin with?
>
> my $return = eval { some stuff };
> if ($@) {
> rethrow or handle "Eval failed with: $@";
> }
> unless ($return) {
> some multiline error handling;
> }
>
> The problem is that eval returning a false/undefined value shouldn't
> necessarily have to indicate failure. And if the 'eval' isn't catching
> an exception then there is really no reason to use it in the first place.
>
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;
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>