Skip to content

Commit 72686fc

Browse files
committed
Document shell quoting constructs (qx and qqx)
1 parent b8c13ad commit 72686fc

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

lib/Language/quoting.pod

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,53 @@ or equivalently:
159159
my @list = «$a b c»;
160160
say @list; # 42 b c
161161
162-
=comment also document qqx<>
162+
=head2 X<Shell quoting: qx|quote,qx>
163+
164+
To run a string as an external program, not only is it possible to pass the
165+
string to the C<shell> or C<run> functions but one can also perform shell
166+
quoting in a similar manner to the backticks a.k.a. C<qx> in Perl 5. There
167+
are some subtleties to consider, however. The backticks are no longer used
168+
for shell quoting in Perl 6 and the C<qx> quotes I<don't> interpolate Perl
169+
variables. Thus
170+
171+
my $world = "there";
172+
say qx{echo "hello $world"}
173+
174+
prints simply C<hello>. Nevertheless, if you have declared an environment
175+
variable before calling C<perl6>, this will be available within C<qx>, for
176+
instance
177+
178+
WORLD="there" perl6
179+
> say qx{echo "hello $WORLD"}
180+
181+
will now print C<hello there>.
182+
183+
The result of calling C<qx> is returned, so this information can be assigned
184+
to a variable for later use:
185+
186+
my $output = qx{echo "hello!"};
187+
say $output; # hello!
188+
189+
See also L<shell|/routine/shell> and L<run|/routine/run> for other ways to
190+
execute external commands.
191+
192+
=head2 X<Shell quoting with interpolation: qqx|quote,qqx>
193+
194+
If one wishes to use the content of a Perl variable within an external
195+
command, then the C<qqx> shell quoting construct should be used (this
196+
corresponds to Perl 5's C<qx>):
197+
198+
my $world = "there";
199+
say qqx{echo "hello $world"}; # hello there
200+
201+
Again, the output of the external command can be kept in a variable:
202+
203+
my $word = "cool";
204+
my $option = "-i";
205+
my $file = "/usr/share/dict/words";
206+
my $output = qqx{grep $option $word $file};
207+
# runs the command: grep -i cool /usr/share/dict/words
208+
say $output; # Cooley␤Cooley's␤Coolidge␤Coolidge's␤cool␤ ...
163209
164210
=head2 Heredocs: :to
165211

0 commit comments

Comments
 (0)