How does safe shelling out (system, open |- , IPC::Run) interact with Unicode and encodings?

When performing shell operations in Perl using safe shelling out techniques like `system`, `open |-`, or `IPC::Run`, it's essential to handle Unicode and encodings carefully. Perl's internal string representation can accommodate Unicode, but when interfacing with the shell, the encoding scheme must be compatible.

When using `system` and similar functions, it's crucial to ensure that input arguments are properly encoded to avoid issues with non-ASCII characters. Additionally, the output from commands needs to be decoded correctly back into Perl’s internal format.

The following example demonstrates how to safely execute a shell command while handling Unicode with `IPC::Run`:

use strict; use warnings; use Encode qw(decode encode); use IPC::Run 'run'; my $cmd = ['echo', 'こんにちは']; # 'Hello' in Japanese my $output; # Run the command and capture its output, handling Unicode run $cmd, '>', \$output; # Decode the output to Perl's internal format $output = decode('UTF-8', $output); print "Command output: $output\n"; # Output: Command output: こんにちは

safe shelling out IPC::Run Unicode Perl system encoding