When I run this without using the extra-escape for the "\n", hexdump doesn’t print the 0a for the embedded newline.
Why does the "\n" need here an extra-treatment?
(While searching for an answer I found String::ShellQuote which does the escaping.)
#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;
binmode STDOUT, ':utf8';
use charnames qw(:full);
use IPC::System::Simple qw(system);
for my $i ( 0x08 .. 0x0d ) {
printf "0x%02x - %s\n", $i, '\N{' . charnames::viacode( $i ) . '}';
my $string = "It" . chr( $i ) . "s";
$string =~ s/\n/\\n/g;
system( "echo -e \Q$string\E | hexdump -C" );
say "";
}
When you don’t convert the newline to the two characters
\n, you’re executing the commandTo
sh, that’s equivalent toWhen you convert the newline to the two characters
\n, you’re executing the commandThat passes the two characters
\ntoecho, for which it outputs a newline under-e.You don’t need to use
-eand to create escapes for-e. You could create a proper shell command. That command would be:You can do that a number of ways. You could roll out your own solution.
There is String::ShellQuote.
Finally, you could avoid the shell entirely.