public inbox for [email protected]  
help / color / mirror / Atom feed
From: Peter Eisentraut <[email protected]>
To: pgsql-hackers <[email protected]>
Subject: What about Perl autodie?
Date: Wed, 7 Feb 2024 15:05:16 +0100
Message-ID: <[email protected]> (raw)

I came across the Perl autodie pragma 
(https://perldoc.perl.org/autodie).  This seems pretty useful; is this 
something we can use?  Any drawbacks?  Any minimum Perl version?

Attached is a sample patch of the kind of thing I'd be interested in. 
The existing error handling of file operations in Perl is pretty 
cumbersome, and this would simplify that.

Btw., here is a sample error message from autodie:

Can't open '../src/include/mb/pg_wchar.h' for reading: 'No such file or 
directory' at ../src/include/catalog/../../backend/catalog/genbki.pl 
line 391

which seems as good or better than the stuff we produce manually.
From 4ada07b13ecde8b3c0d120583202a38de062239f Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Wed, 7 Feb 2024 14:59:36 +0100
Subject: [PATCH] WIP: Make some use of Perl autodie pragma

---
 src/backend/catalog/Catalog.pm | 11 ++++++-----
 src/backend/catalog/genbki.pl  | 24 +++++++++---------------
 2 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm
index 55a8877aede..fa42d472df1 100644
--- a/src/backend/catalog/Catalog.pm
+++ b/src/backend/catalog/Catalog.pm
@@ -15,6 +15,7 @@ package Catalog;
 
 use strict;
 use warnings FATAL => 'all';
+use autodie;
 
 use File::Compare;
 
@@ -48,7 +49,7 @@ sub ParseHeader
 	$catalog{foreign_keys} = [];
 	$catalog{client_code} = [];
 
-	open(my $ifh, '<', $input_file) || die "$input_file: $!";
+	open(my $ifh, '<', $input_file);
 
 	# Scan the input file.
 	while (<$ifh>)
@@ -307,7 +308,7 @@ sub ParseData
 {
 	my ($input_file, $schema, $preserve_formatting) = @_;
 
-	open(my $ifd, '<', $input_file) || die "$input_file: $!";
+	open(my $ifd, '<', $input_file);
 	$input_file =~ /(\w+)\.dat$/
 	  or die "Input file $input_file needs to be a .dat file.\n";
 	my $catname = $1;
@@ -531,11 +532,11 @@ sub RenameTempFile
 	if (-f $final_name
 		&& compare($temp_name, $final_name) == 0)
 	{
-		unlink($temp_name) || die "unlink: $temp_name: $!";
+		unlink($temp_name);
 	}
 	else
 	{
-		rename($temp_name, $final_name) || die "rename: $temp_name: $!";
+		rename($temp_name, $final_name);
 	}
 	return;
 }
@@ -553,7 +554,7 @@ sub FindDefinedSymbol
 		$include_path .= '/';
 	}
 	my $file = $include_path . $catalog_header;
-	open(my $find_defined_symbol, '<', $file) || die "$file: $!";
+	open(my $find_defined_symbol, '<', $file);
 	while (<$find_defined_symbol>)
 	{
 		if (/^#define\s+\Q$symbol\E\s+(\S+)/)
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 94afdc5491d..dc8cba037fd 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -15,6 +15,7 @@
 
 use strict;
 use warnings FATAL => 'all';
+use autodie;
 use Getopt::Long;
 
 use FindBin;
@@ -386,7 +387,7 @@
 my %encids;
 
 my $encfile = $include_path . 'mb/pg_wchar.h';
-open(my $ef, '<', $encfile) || die "$encfile: $!";
+open(my $ef, '<', $encfile);
 
 # We're parsing an enum, so start with 0 and increment
 # every time we find an enum member.
@@ -435,23 +436,17 @@
 # Open temp files
 my $tmpext = ".tmp$$";
 my $bkifile = $output_path . 'postgres.bki';
-open my $bki, '>', $bkifile . $tmpext
-  or die "can't open $bkifile$tmpext: $!";
+open my $bki, '>', $bkifile . $tmpext;
 my $schemafile = $output_path . 'schemapg.h';
-open my $schemapg, '>', $schemafile . $tmpext
-  or die "can't open $schemafile$tmpext: $!";
+open my $schemapg, '>', $schemafile . $tmpext;
 my $fk_info_file = $output_path . 'system_fk_info.h';
-open my $fk_info, '>', $fk_info_file . $tmpext
-  or die "can't open $fk_info_file$tmpext: $!";
+open my $fk_info, '>', $fk_info_file . $tmpext;
 my $constraints_file = $output_path . 'system_constraints.sql';
-open my $constraints, '>', $constraints_file . $tmpext
-  or die "can't open $constraints_file$tmpext: $!";
+open my $constraints, '>', $constraints_file . $tmpext;
 my $syscache_ids_file = $output_path . 'syscache_ids.h';
-open my $syscache_ids_fh, '>', $syscache_ids_file . $tmpext
-  or die "can't open $syscache_ids_file$tmpext: $!";
+open my $syscache_ids_fh, '>', $syscache_ids_file . $tmpext;
 my $syscache_info_file = $output_path . 'syscache_info.h';
-open my $syscache_info_fh, '>', $syscache_info_file . $tmpext
-  or die "can't open $syscache_info_file$tmpext: $!";
+open my $syscache_info_fh, '>', $syscache_info_file . $tmpext;
 
 # Generate postgres.bki and pg_*_d.h headers.
 
@@ -469,8 +464,7 @@
 
 	# Create one definition header with macro definitions for each catalog.
 	my $def_file = $output_path . $catname . '_d.h';
-	open my $def, '>', $def_file . $tmpext
-	  or die "can't open $def_file$tmpext: $!";
+	open my $def, '>', $def_file . $tmpext;
 
 	print_boilerplate($def, "${catname}_d.h",
 		"Macro definitions for $catname");
-- 
2.43.0



Attachments:

  [text/plain] 0001-WIP-Make-some-use-of-Perl-autodie-pragma.patch (4.1K, ../[email protected]/2-0001-WIP-Make-some-use-of-Perl-autodie-pragma.patch)
  download | inline diff:
From 4ada07b13ecde8b3c0d120583202a38de062239f Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Wed, 7 Feb 2024 14:59:36 +0100
Subject: [PATCH] WIP: Make some use of Perl autodie pragma

---
 src/backend/catalog/Catalog.pm | 11 ++++++-----
 src/backend/catalog/genbki.pl  | 24 +++++++++---------------
 2 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm
index 55a8877aede..fa42d472df1 100644
--- a/src/backend/catalog/Catalog.pm
+++ b/src/backend/catalog/Catalog.pm
@@ -15,6 +15,7 @@ package Catalog;
 
 use strict;
 use warnings FATAL => 'all';
+use autodie;
 
 use File::Compare;
 
@@ -48,7 +49,7 @@ sub ParseHeader
 	$catalog{foreign_keys} = [];
 	$catalog{client_code} = [];
 
-	open(my $ifh, '<', $input_file) || die "$input_file: $!";
+	open(my $ifh, '<', $input_file);
 
 	# Scan the input file.
 	while (<$ifh>)
@@ -307,7 +308,7 @@ sub ParseData
 {
 	my ($input_file, $schema, $preserve_formatting) = @_;
 
-	open(my $ifd, '<', $input_file) || die "$input_file: $!";
+	open(my $ifd, '<', $input_file);
 	$input_file =~ /(\w+)\.dat$/
 	  or die "Input file $input_file needs to be a .dat file.\n";
 	my $catname = $1;
@@ -531,11 +532,11 @@ sub RenameTempFile
 	if (-f $final_name
 		&& compare($temp_name, $final_name) == 0)
 	{
-		unlink($temp_name) || die "unlink: $temp_name: $!";
+		unlink($temp_name);
 	}
 	else
 	{
-		rename($temp_name, $final_name) || die "rename: $temp_name: $!";
+		rename($temp_name, $final_name);
 	}
 	return;
 }
@@ -553,7 +554,7 @@ sub FindDefinedSymbol
 		$include_path .= '/';
 	}
 	my $file = $include_path . $catalog_header;
-	open(my $find_defined_symbol, '<', $file) || die "$file: $!";
+	open(my $find_defined_symbol, '<', $file);
 	while (<$find_defined_symbol>)
 	{
 		if (/^#define\s+\Q$symbol\E\s+(\S+)/)
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 94afdc5491d..dc8cba037fd 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -15,6 +15,7 @@
 
 use strict;
 use warnings FATAL => 'all';
+use autodie;
 use Getopt::Long;
 
 use FindBin;
@@ -386,7 +387,7 @@
 my %encids;
 
 my $encfile = $include_path . 'mb/pg_wchar.h';
-open(my $ef, '<', $encfile) || die "$encfile: $!";
+open(my $ef, '<', $encfile);
 
 # We're parsing an enum, so start with 0 and increment
 # every time we find an enum member.
@@ -435,23 +436,17 @@
 # Open temp files
 my $tmpext = ".tmp$$";
 my $bkifile = $output_path . 'postgres.bki';
-open my $bki, '>', $bkifile . $tmpext
-  or die "can't open $bkifile$tmpext: $!";
+open my $bki, '>', $bkifile . $tmpext;
 my $schemafile = $output_path . 'schemapg.h';
-open my $schemapg, '>', $schemafile . $tmpext
-  or die "can't open $schemafile$tmpext: $!";
+open my $schemapg, '>', $schemafile . $tmpext;
 my $fk_info_file = $output_path . 'system_fk_info.h';
-open my $fk_info, '>', $fk_info_file . $tmpext
-  or die "can't open $fk_info_file$tmpext: $!";
+open my $fk_info, '>', $fk_info_file . $tmpext;
 my $constraints_file = $output_path . 'system_constraints.sql';
-open my $constraints, '>', $constraints_file . $tmpext
-  or die "can't open $constraints_file$tmpext: $!";
+open my $constraints, '>', $constraints_file . $tmpext;
 my $syscache_ids_file = $output_path . 'syscache_ids.h';
-open my $syscache_ids_fh, '>', $syscache_ids_file . $tmpext
-  or die "can't open $syscache_ids_file$tmpext: $!";
+open my $syscache_ids_fh, '>', $syscache_ids_file . $tmpext;
 my $syscache_info_file = $output_path . 'syscache_info.h';
-open my $syscache_info_fh, '>', $syscache_info_file . $tmpext
-  or die "can't open $syscache_info_file$tmpext: $!";
+open my $syscache_info_fh, '>', $syscache_info_file . $tmpext;
 
 # Generate postgres.bki and pg_*_d.h headers.
 
@@ -469,8 +464,7 @@
 
 	# Create one definition header with macro definitions for each catalog.
 	my $def_file = $output_path . $catname . '_d.h';
-	open my $def, '>', $def_file . $tmpext
-	  or die "can't open $def_file$tmpext: $!";
+	open my $def, '>', $def_file . $tmpext;
 
 	print_boilerplate($def, "${catname}_d.h",
 		"Macro definitions for $catname");
-- 
2.43.0



view thread (3+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: What about Perl autodie?
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox