Perlito
Perlito copied to clipboard
Modifying substr expressions evaluate incorrectly
I'm very fascinated by all the work that went into this project and I have written quite a bit of Perl code with few dependencies that I could use Perlito on. One issue that I think might be easy to fix with much benefit, is that I often like using modifying substr expressions as it's often faster than regexes, but in the generated JS code, the string doesn't get modified. Here are three ways to modify substrings.
my $s = 'hello';
# 0
substr $s, 0, 1, 'H';
# 1
substr($s, 0, 1) = 'G';
# 2
my $ref = \substr $s, 0, 1;
$$ref = 'F';
Running any of these followed by say $s;
should result in a modified string. Running with Perlito5 gives a compiler error in case[1] and results in output of unmodified hello
in the other cases.
Note that in JavaScript strings are constants - unlike in Perl, the function returns a new string and the original string doesn't get modified.
Can it be done? In some cases yes, but it takes some work.
Some of these cases can be implemented with a Macro - a rule that specifies how a certain code pattern should be mapped to a replacement output.
This will not fix the case for $$ref
above, or when substr()
is used as a subroutine parameter. It is also slow, because it involves executing more code.
For reference, the current implementation of substr
for JavaScript is located at:
-- compile-time (this code executes during translation to JS) https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/Apply.pm#L851
-- run-time (this becomes a JS function) https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/CORE.pm#L364
How macros would work:
substr $s, 0, 1, 'H'
can be transformed into do { $s = 'H' + substr($s, 2) }
Which code to modify:
-
to recognize the special case: add an extra
if
at https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/Apply.pm#L854 -
to create a new syntax tree with the modified pattern and emit it: here is a short example to start - https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/Apply.pm#L876
Another example of using a macro to implement mutators in JS is the ++
operator - https://github.com/fglock/Perlito/blob/master/src5/lib/Perlito5/JavaScript2/Apply.pm#L442
(note the emit_wrap_javascript2()
that works like a "do BLOCK").