From 9bc23d14895b20a6b5d63ebf760a9537b9c86745 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 7 Sep 2021 14:44:22 -0700 Subject: [PATCH] Keep needed space after $^, issue c068 --- lib/Perl/Tidy/Formatter.pm | 15 +++++++++++++++ local-docs/BugLog.pod | 24 +++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index f49e683c..77fc7fba 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -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, diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 39a0660f..8f67aaee 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,7 +2,29 @@ =over 4 -=item B +=item B + +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 This issue is illustrated with the following line (rt80058): -- 2.39.5