]> git.donarmstrong.com Git - perltidy.git/commitdiff
remove a call to streamhandle
authorSteve Hancock <perltidy@users.sourceforge.net>
Fri, 15 Sep 2023 01:43:46 +0000 (18:43 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Fri, 15 Sep 2023 01:43:46 +0000 (18:43 -0700)
Streamhandle can be inefficient to use, so I am minimize its use.

lib/Perl/Tidy.pm

index 8f7d68cf7a4295dbf3dfcc536ccfe4b1e1157603..f9b98ee8acc2eb1f2c8c527ae9d7778e70bbebfe 100644 (file)
@@ -165,10 +165,15 @@ sub streamhandle {
     # Case 2. Not given, or an empty string: unencoded binary data is being
     # transferred, set binary mode for files and for stdin.
 
-    # NOTE: sub slurp_stream is now preferred for reading.
-
     my ( $filename, $mode, $is_encoded_data ) = @_;
 
+    # sub slurp_stream is preferred for reading (for efficiency).
+    if ( $mode ne 'w' && $mode ne 'W' ) {
+        if ( DEVEL_MODE || ( $mode ne 'r' && $mode ne 'R' ) ) {
+            Fault("streamhandle called in unexpected mode '$mode'\n");
+        }
+    }
+
     my $ref = ref($filename);
     my $New;
     my $fh;
@@ -1219,7 +1224,7 @@ sub backup_method_copy {
     # Open the original input file for writing ... opening with ">" will
     # truncate the existing data.
     open( my $fout, ">", $input_file )
-      || Die(
+      or Die(
 "problem re-opening $input_file for write for -b option; check file and directory permissions: $OS_ERROR\n"
       );
 
@@ -1357,13 +1362,13 @@ sub backup_method_move {
     }
 
     # Open a file with the original input file name for writing ...
-    my $is_encoded_data = $self->[_is_encoded_data_];
-    my ( $fout, $iname ) =
-      Perl::Tidy::streamhandle( $input_file, 'w', $is_encoded_data );
-    if ( !$fout ) {
-        Die(
+    open( my $fout, ">", $input_file )
+      or Die(
 "problem re-opening $input_file for write for -b option; check file and directory permissions: $OS_ERROR\n"
-        );
+      );
+
+    if ( $self->[_is_encoded_data_] ) {
+        binmode $fout, ":raw:encoding(UTF-8)";
     }
 
     # Now copy the formatted output to it..