Perl - Pipe command into another

Quick question,

Is there a way to pipe a command into another command via perl like you can in the *Nix command line?

For example: free -m | grep Mem

How would I be able to do the "piping" in Perl?

Answers


You can invoke the command exactly like that:

system("free -m | grep Mem");

From the documentation:

If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to execvp , which is more efficient.

You can do the same with other methods for invoking external commands, like open:

open my $fh, "-|", "free -m | grep Mem" or croak "failed to run pipeline";
# and now read from $fh as usual

Need Your Help

Postgresql CITEXT Datatype Issue JPA Hibernate

hibernate postgresql jpa postgresql-9.1

I'm having difficulty using the CITEXT datatype in PostgreSQL using JPA and Hibernate. CITEXT is supposed to provide a case insensitive text datatype but when used with JPA/Hibernate it doesn't be...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.