From 9a204295325110177ac9f85b092851539f80921d Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Fri, 24 Apr 2026 20:23:23 +0200 Subject: [PATCH] MT#61800 perl: Use native try/catch keywords This makes the code more ergonomic. Change-Id: I2198f0841e616308e13337a2d547b4c04990f413 --- helper/sync-db | 23 +++++++++-------------- helper/tt2-process | 10 ++++------ sbin/ngcp-network-validator | 8 ++++---- sbin/ngcp-sync-db-creds | 15 +++++++-------- sbin/ngcp-sync-db-grants | 7 +++---- 5 files changed, 27 insertions(+), 36 deletions(-) diff --git a/helper/sync-db b/helper/sync-db index 083e12d7..b1144622 100755 --- a/helper/sync-db +++ b/helper/sync-db @@ -332,7 +332,7 @@ sub do_sync_general_timezone { my $ok = 1; my ($sql_log_bin) = $dbh->selectrow_array('SELECT @@sql_log_bin'); - eval { + try { die "Error: general.timezone value is not set\n" unless $tz; $dbh->do("SET sql_log_bin=0"); @@ -353,9 +353,8 @@ SQL if ($DBI::err) { die "Error: Could not insert into ngcp.timezone: $DBI::errstr\n"; } - }; - if ($@) { - print $@; + } catch ($e) { + print $e; $ok = 0; } $dbh->do("SET sql_log_bin=$sql_log_bin"); @@ -378,7 +377,7 @@ sub do_sync_db_timezones { chomp $tzinfo_version; my $sql = ''; - eval { + try { $dbh->begin_work() or die "Cannot start tx: $DBI::errstr\n"; my ($cur_tzinfo_version) = $dbh->selectrow_array(<rollback(); return; } @@ -453,7 +450,7 @@ sub do_sync_timezone_version { } my $sql = ''; - eval { + try { $dbh->begin_work() or die "Cannot start tx: $DBI::errstr\n"; my ($cur_tz_version) = $dbh->selectrow_array(<rollback(); return; } diff --git a/helper/tt2-process b/helper/tt2-process index f94b17f4..1d150994 100755 --- a/helper/tt2-process +++ b/helper/tt2-process @@ -211,14 +211,12 @@ sub process_input { # Assume safe defaults. umask 0077; - eval { + try { process_template($tt, $config, $input, $output); - }; - if ($@) { - warn $@; - error("Generating $output based on $input: FAILED"); - } else { info("Generating $output: OK"); + } catch ($e) { + warn $e; + error("Generating $output based on $input: FAILED"); } # Execute postbuild script. diff --git a/sbin/ngcp-network-validator b/sbin/ngcp-network-validator index 965c4782..42209b4d 100755 --- a/sbin/ngcp-network-validator +++ b/sbin/ngcp-network-validator @@ -261,12 +261,12 @@ if (defined $ENV{NGCP_KWALIFY_VERBOSE}) { my $errors = 0; -eval { +try { Kwalify::validate($schema, $yaml); -} or do { - warn $@; +} catch ($e) { + warn $e; $errors++; -}; +} # Perform out-of-schema checks. $errors += miss_check(\%miss_peer, '[/hosts/] Missing peer'); diff --git a/sbin/ngcp-sync-db-creds b/sbin/ngcp-sync-db-creds index 1facc825..95d73c09 100755 --- a/sbin/ngcp-sync-db-creds +++ b/sbin/ngcp-sync-db-creds @@ -284,10 +284,11 @@ sub flush_privs { } sub main { - eval { + try { $yml = YAML::XS::LoadFile($CONSTANTS_YML); - }; - die "Can't read constants file: $EVAL_ERROR\n" if $EVAL_ERROR; + } catch ($e) { + die "Can't read constants file: $e\n"; + } my ($dbhost, $dbport); if (my $db_cfg = Config::Tiny->read($DB_CFG)) { @@ -315,8 +316,7 @@ sub main { $dbh->begin_work or die "Cannot start transaction: $DBI::errstr\n"; - eval { - + try { my $rc = sync_mysql_data(); if ($rc) { @@ -324,11 +324,10 @@ sub main { } else { log_debug("nothing to update"); } - }; - if ($@) { + } catch ($e) { $dbh->rollback if $dbh; $dbh->disconnect if $dbh; - die "Error: $@"; + die "Error: $e"; } $dbh->commit if $dbh; diff --git a/sbin/ngcp-sync-db-grants b/sbin/ngcp-sync-db-grants index fef9469d..3c446bb4 100755 --- a/sbin/ngcp-sync-db-grants +++ b/sbin/ngcp-sync-db-grants @@ -493,7 +493,7 @@ sub main { $dbh->begin_work or die "Cannot start transaction: $DBI::errstr\n"; - eval { + try { my $rc = 0; create_protected_user($TEMP_GRANT_USER, $TEMP_GRANT_HOST); @@ -520,11 +520,10 @@ sub main { if ($rc) { flush_privs(); } - }; - if ($@) { + } catch ($e) { $dbh->rollback if $dbh; $dbh->disconnect if $dbh; - die "Error: $@"; + die "Error: $e"; } $dbh->commit or die "Cannot commit transaction: $DBI::errstr\n";