]> git.donarmstrong.com Git - perltidy.git/commitdiff
Keep needed space after $^, issue c068
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 7 Sep 2021 21:44:22 +0000 (14:44 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 7 Sep 2021 21:44:22 +0000 (14:44 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index f49e683cd009958cef7aa2bef8a6c4ec081676d9..77fc7fba5d0a657fad48d414081bea0783f49313 100644 (file)
@@ -2714,6 +2714,7 @@ EOM
     my %essential_whitespace_filter_l2;
     my %essential_whitespace_filter_r2;
     my %is_type_with_space_before_bareword;
+    my %is_special_variable_char;
 
     BEGIN {
 
@@ -2768,6 +2769,12 @@ EOM
         @q = qw( Q & );
         @is_type_with_space_before_bareword{@q} = (1) x scalar(@q);
 
+        # These are the only characters which can (currently) form special
+        # variables, like $^W: (issue c066, c068).
+        @q =
+          qw{ ? A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ };
+        @{is_special_variable_char}{@q} = (1) x scalar(@q);
+
     }
 
     sub is_essential_whitespace {
@@ -2979,6 +2986,14 @@ EOM
             && $is_for_foreach{$tokenll}
           )
 
+          # Keep space after like $^ if needed to avoid forming a different
+          # special variable (issue c068). For example:
+          #       my $aa = $^ ? "none" : "ok";
+          || ( $typel eq 'i'
+            && length($tokenl) == 2
+            && substr( $tokenl, 1, 1 ) eq '^'
+            && $is_special_variable_char{ substr( $tokenr, 0, 1 ) } )
+
           # We must be sure that a space between a ? and a quoted string
           # remains if the space before the ? remains.  [Loca.pm, lockarea]
           # ie,
index 39a0660f4042987b90a1c3f177a74a06c923d904..8f67aaeef127b6aa4cae0f492e0c0a18989f4ef0 100644 (file)
@@ -2,7 +2,29 @@
 
 =over 4
 
-=item B<Handle parsing problem issue c066>
+=item B<Fix parsing issue c068>
+
+This issue is illustrated with the following line:
+
+   my $aa = $^ ? "defined" : "not defined";
+
+If we tell perltidy to remove the space before the '?', then the output
+will no longer be a valid script:
+
+   # perltidy -nwls='?':
+   my $aa = $^? "defined" : "not defined";
+
+The problem is that Perl considers '$^?' to be a special variable. So Rerunning
+perltidy on this gives an error, and perl also gives an error.  This update
+fixes the problem by preventing a space after anything like '$^' from being
+removed a new special variable would be created.
+
+This fixes issue c068.
+
+7 Sep 2021.
+
+
+=item B<Fix parsing problem issue c066>
 
 This issue is illustrated with the following line (rt80058):