I was working on a keymap script (map keys from one language keyboard layout to another). And after a lot of hard time trying to get everything working I found out that different characters are treated differently in all programs (perl, python). Then I run a simple test script (now simplified) in a terminal (kitty, gnome-terminal — it doesn't matter):
python -c 'import sys;print(len(sys.argv))' テスト
And got an expected result:
But if I run this in a sh/bash (unix&utf-8) file:
#!/usr/bin/env bash # or #!/bin/sh python -c 'import sys;print(len(sys.argv))' テスト
I get (
And that's the reason all this encode/decode/upgrade/downgrade UTF-8 stuff didn't work in Perl (if I would run the command manually from terminal it would probably work without all this additional encoding functions).
Now I have a problem: why the exact same command gives me different results depending on the execution environment (terminal emulator vs shell script)? How can I fix this?
I forgot about my:
So with Python, running
python3 explicitly makes everything work the same in both cases. But with Perl on the other hand:
echo 'print length $ARGV;' | perl -l -- - テスト
This works the same, but in both cases it outputs
9. With Perl there are no different versions and mine is 5.30.0 (which is printed in both cases exactly the same). Do I have to add some code in Perl itself to make it work like Python3 (length of 1 Unicode character is 1 and not 1-3 bytes)?