Prev: Intro

Running external command from Perl 6 (shell, QX)

Perl 6 and it ecosystem has a wealth of commands that you could use instead of shell commands, but there might be cases when you want to run an external command.

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.


The shell function receives a string that is exactly the same as you'd type on the command line and executes the external command on your behalf. It connects the standard output, standard error and standard input to whatever your Perl 6 code had. This can be useful for interactive programs that you want to wrap with your code.


use v6;

my $res = shell("ssh uptime");
say "Command finished";
say $res;

Here we run the ssh command, pass to it the name of the server and the command (uptime) to be executed on the remote server. When Perl 6 executes this command, the output of the command will be printed on the screen and when it asks for input whatever we type will be sent to the command. Specifically becasue this was the first time I ran ssh to that server, it first aske me if I recognize the RSA fingerprint of the server. I could type in 'yes' and the process continued.

$ perl6 run_ssh.p6 
The authenticity of host ' (' can't be established.
RSA key fingerprint is fd:18:2c:05:81:b8:59:54:30.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '' (RSA) to the list of known hosts.

13:55:57 up 27 days,  2:20,  0 users,  load average: 0.07, 0.09, 0.06
Command finished => 0, pid => Any, signal => 0)

The text 'Command finished' is printed only after the external command has finished.

The value returned by the shell command is a Proc::Status object.



use v6;

my $out = QX("ssh uptime");
say "Command finished";
say $out;

The QX function also receives the string that you'd type on the command line, but instead of printing the output to the screen it will return it to the caller and we can assign it to a scalar variable.

In this example we used the same ssh command and it will work now, because the previous time we have already accepted the RSA fingerprint. If we ran this script the first time, it would seem to be "stuck" as it was waiting for input from the user.

Command finished
 14:04:11 up 27 days,  2:28,  0 users,  load average: 0.10, 0.07, 0.05

In this case too perl will wait till the external command finished and qx returns ithe output. The big difference is that this output has not been printed to the screen yet, it can be found in the $out variable.

We can print it out - so we can see it - using the say function, or we could analyze the output in the script. For example we could split up the output and put each value in a variable.

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-01-03

Prev: Intro


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 2.4106836

Support my Perl 6 book!

I am writing a book on Web Application Development in Perl 6 and raising money via crowdfunding. Please support my effort!

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.