Practical mod_perl / HTML Book /



previous page: 10.2.6. Starting a Long-Running External Program
  
page up: HTML Version of the book
  
next page: 10.2.8. Executing system( ) or exec( ) in the Right Way

10.2.7. Starting a Short-Running External Program


Sometimes you need to call an external program and you cannot continue before this program completes its run (e.g., if you need it to return some result). In this case, the fork solution doesn't help. There are a few ways to execute such a program. First, you could use system( ):

system "perl -e 'print 5+5'"

You would never call the Perl interperter for doing a simple calculation like this, but for the sake of a simple example it's good enough.

The problem with this approach is that we cannot get the results printed to STDOUT. That's where backticks or qx( ) can help. If you use either:

my $result = `perl -e 'print 5+5'`;

or:

my $result = qx{perl -e 'print 5+5'};

the whole output of the external program will be stored in the $result variable.

Of course, you can use other solutions, such as opening a pipe (|) to the program if you need to submit many arguments. And there are more evolved solutions provided by other Perl modules, such as IPC::Open2 and IPC::Open3, that allow you to open a process for reading, writing, and error handling.

 

Continue to:

  • prev: 10.2.6. Starting a Long-Running External Program
  • Table of Contents
  • next: 10.2.8. Executing system( ) or exec( ) in the Right Way







TOP
previous page: 10.2.6. Starting a Long-Running External Program
  
page up: HTML Version of the book
  
next page: 10.2.8. Executing system( ) or exec( ) in the Right Way


Menu

  • HTML Book
  • PDF Book
  • Download Code
  • Table of Contents
  • Errata
  • mod_perl2 User's Guide
  • Sitemap

Search


Add to Google




Creative Commons License


Written by
Eric Cholet (Logilune) and
Stas Bekman (StasoSphere & Free Books).


[ Privacy Policy | Terms of Use | About Us | Search ]

© 2007 StasoSphere.com