Should I put the “eval” in the subroutine or the subroutine in the “eval”?
问题 Does one of these two ways have to be preferred or is it only a matter of taste? #!/usr/bin/env perl use warnings; use strict; use DBI; my $db = 'sqlite_db'; #################### A #################### sub get_database_handle { my ( $db ) = @_; my $dbh; eval { $dbh = DBI->connect( "DBI:SQLite:$db", '', '', {...} ) or die DBI->errstr; }; if ( $@ ) { print $@; return; } return $dbh; } DATABASES: while ( 1 ) { # choose a database from a list of databases # ... my $dbh = get_database_handle( $db