x
Loading
 Loading
Hello, Guest | Login | Register

Launching Processes

Perl has many ways of launching and managing different programs. This is a Good Thing, because Perl’s ability to launch and manage programs — or child processes — is one of the reasons it makes such a great
“duct-tape of the Internet.” The easiest way to launch a child process is with system:

Perl has many ways of launching and managing different programs. This is a Good Thing, because Perl’s ability to launch and manage programs — or child processes — is one of the reasons it makes such a great “duct-tape of the Internet.” The easiest way to launch a child process is with system:

 system “date”; 

The child process here is the date command. Anything that can be invoked from a shell command prompt can be used in this string. The child process inherits Perl’s standard input, output, and error output, so the output of this datecommand will show up wherever Perl’s STDOUT was going.

The command can be arbitrarily complex, including everything that /bin/sh (or its equivalent) can handle:

 system “for i in *; do echo == $i ==; cat $i; done”; 

Here, we’re dumping out the contents of a directory, one file at a time. The $i vars are backslashed here because Perl would have expanded them to their current Perl values and we want the shell to see its own $i instead. A quick solution here is to use single quotes instead of double quotes. Single quotes, unlike double quotes, prevent the evaluation of references to Perl variables.

 system ‘for i in *; do echo == $i ==; cat $i; done’; 

Or, you can just set the value of Perl’s $i to ‘$i’, but that’s pretty twisted, and will probably…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. KDE 4.4: Does It Work Yet?
  2. Writing Custom Nagios Plugins with Python
  3. Power Up Linux GUI Apps
  4. Tweeting from the Command Line with Twyt
  5. When Memory Serves You: Using ramfs and tmpfs
Follow Linux Magazine
Rackspace