@@ -1112,16 +1112,33 @@ e.g.:
1112
1112
Whereas in Perl 5 you would do:
1113
1113
1114
1114
my $arg = 'Hello';
1115
- my $captured = `echo " $arg" `;
1116
- my $captured = qx(echo " $arg" );
1115
+ my $captured = `echo \Q $arg\E `;
1116
+ my $captured = qx(echo \Q $arg\E );
1117
1117
1118
- In Perl 6, you can do:
1118
+ Or using String::ShellQuote (because C < \Q…\E > is not completely right):
1119
+
1120
+ my $arg = shell_quote 'Hello';
1121
+ my $captured = `echo $arg`;
1122
+ my $captured = qx(echo $arg);
1123
+
1124
+ In Perl 6, you will probably want to run commands without using the shell:
1119
1125
1120
1126
my $arg = 'Hello';
1121
- my $captured = qq:x(echo "{$arg}");
1127
+ my $captured = run('echo', $arg, :out).out.slurp-rest;
1128
+ my $captured = run(«echo "$arg"», :out).out.slurp-rest;
1122
1129
1123
- Beware of interpolating strings with special shell characters! (Which is a
1124
- problem in Perl 5 as well).
1130
+ You can also use the shell if you really want to:
1131
+
1132
+ my $arg = 'Hello';
1133
+ my $captured = shell("echo $arg", :out).out.slurp-rest;
1134
+ my $captured = qqx{echo $arg};
1135
+
1136
+ But beware that in this case there is B < no protection at all > ! C < run > does
1137
+ not use the shell, so there is no need to escape the arguments (arguments
1138
+ are passed directly). If you are using C < shell > or C < qqx > , then everything
1139
+ ends up being one long string which is then passed to the shell. Unless you
1140
+ validate your arguments very carefully, there is a high chance to introduce
1141
+ shell injection vulnerabilities with such code.
1125
1142
1126
1143
= head1 Environment variables
1127
1144
0 commit comments