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.
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 perl6maven.com 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 'perl6maven.com (22.214.171.124)' 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 'perl6maven.com' (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 Proc::Status.new(exit => 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 perl6maven.com 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.
Published on 2015-01-03