Skip to content

Commit 18da27a

Browse files
committed
Revise the qqx section.
Thanks to @AlexDaniel 's comment on the pull-req.
1 parent e0c27d6 commit 18da27a

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

doc/Language/5to6-nutshell.pod

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,16 +1112,33 @@ e.g.:
11121112
Whereas in Perl 5 you would do:
11131113
11141114
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);
11171117
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:
11191125
11201126
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;
11221129
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.
11251142
11261143
=head1 Environment variables
11271144

0 commit comments

Comments
 (0)