Skip to content

[PATCH] allow non-variable as lhs of non-updating tr/// #2279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
p5pRT opened this issue Jul 30, 2000 · 1 comment
Closed

[PATCH] allow non-variable as lhs of non-updating tr/// #2279

p5pRT opened this issue Jul 30, 2000 · 1 comment

Comments

@p5pRT
Copy link

p5pRT commented Jul 30, 2000

Migrated from rt.perl.org#3597 (status was 'resolved')

Searchable as RT3597$

@p5pRT
Copy link
Author

p5pRT commented Jul 30, 2000

From [email protected]

As noted by Bart Lateur / Larry Rosler on comp.lang.perl.misc,
while the tr/// operator no longer complains if it is just counting and
the lhs is read-only, it still insists that the lhs is syntactically
a variable.

The attached patch for bleadperl fixes this.

Mike Guy

Inline Patch
--- ./op.c.orig	Mon Jul 24 18:46:48 2000
+++ ./op.c	Sun Jul 30 14:30:22 2000
@@ -1988,7 +1988,9 @@
 	right->op_type == OP_SUBST ||
 	right->op_type == OP_TRANS)) {
 	right->op_flags |= OPf_STACKED;
-	if (right->op_type != OP_MATCH)
+	if (right->op_type != OP_MATCH &&
+            ! (right->op_type == OP_TRANS &&
+               right->op_private & OPpTRANS_IDENTICAL))
 	    left = mod(left, right->op_type);
 	if (right->op_type == OP_TRANS)
 	    o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
--- ./t/op/tr.t.orig	Tue Jul 11 22:19:23 2000
+++ ./t/op/tr.t	Sun Jul 30 14:33:40 2000
@@ -5,7 +5,7 @@
     unshift @INC, "../lib";
 }
 
-print "1..23\n";
+print "1..27\n";
 
 $_ = "abcdefghijklmnopqrstuvwxyz";
 
@@ -145,4 +145,21 @@
 eval "tr/m-d/ /";
 print (($@ =~ /^Invalid \[\] range "m-d" in transliteration operator/) 
        ? '' : 'not ', "ok 23\n");
+
+# 24: test cannot update if read-only
+eval '$1 =~ tr/x/y/';
+print (($@ =~ /^Modification of a read-only value attempted/) ? '' : 'not ',
+       "ok 24\n");
+
+# 25: test can count read-only
+'abcdef' =~ /(bcd)/;
+print (( eval '$1 =~ tr/abcd//' == 3) ? '' : 'not ', "ok 25\n");
+
+# 26: test lhs OK if not updating
+print ((eval '"123" =~ tr/12//' == 2) ? '' : 'not ', "ok 26\n");
+
+# 27: test lhs bad if updating
+eval '"123" =~ tr/1/1/';
+print (($@ =~ m|^Can't modify constant item in transliteration \(tr///\)|)
+       ? '' : 'not ', "ok 27\n");
 

End of patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant