Is there a filehandle/handle for the output of a system command I execute in Perl?
The system() Function in Perl #!/usr/bin/perl $PATH = "I am Perl Variable"; system('echo $PATH'); # Treats $PATH as shell variable system("echo $PATH"); # Treats $PATH as Perl variable system("echo \$PATH"); # Escaping $ works.
Perl's system() function executes a system shell command. Here the parent process forks a child process, and then waits for the child process to terminate. The command will either succeed or fail returning a value for each situation.
Here's an example of establishing pipes between your script and other commands, using the 3-argument form of open
:
open(my $incoming_pipe, '-|', 'ls -l') or die $!;
open(my $outgoing_pipe, '|-', "grep -v '[02468]'") or die $!;
my @listing = <$incoming_pipe>; # Lines from output of ls -l
print $outgoing_pipe "$_\n" for 1 .. 50; # 1 3 5 7 9 11 ...
Yes, you can use a pipe like this:
open(my $pipe, "ls|") or die "Cannot open process: $!";
while (<$pipe>) {
print;
}
See the documentation for open
for more information, and perlipc
for a complete description of pipe operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With