# calling a perl program from within another perl program



## csross (Sep 2, 2004)

I have a perl script that reads through a directory and manipulates every mp3. I would like to call another script, passing the name of the mp3 file and returning values obtained in the second program. Then I would like to reference those returned values in the original program. 

How do I call the second program, sending it the file to process and leaving place-holders for the return values? This is on a linux server.

Any ideas?


----------



## lotuseclat79 (Sep 12, 2003)

csross said:


> I have a perl script that reads through a directory and manipulates every mp3. I would like to call another script, passing the name of the mp3 file and returning values obtained in the second program. Then I would like to reference those returned values in the original program.
> 
> How do I call the second program, sending it the file to process and leaving place-holders for the return values? This is on a linux server.
> 
> Any ideas?


Hi csross,

You need to make what you call the second program a subroutine and pass it the parameters declared in the first program or main routine. After the subroutine call returns the parameter values, use the names you referred the parameters by in the first routine to continue your computation.

-- Tom


----------



## csross (Sep 2, 2004)

Hi 

Thank you. Unfortunatly I am not a programmer so I do not know the syntax of that. Have you got an example that I could see.

Thanks


----------



## lotuseclat79 (Sep 12, 2003)

csross said:


> Hi
> 
> Thank you. Unfortunatly I am not a programmer so I do not know the syntax of that. Have you got an example that I could see.
> 
> Thanks


Hi csross,

Try this tutorial on Perl: http://archive.ncsa.uiuc.edu/General/Training/PerlIntro/

-- Tom


----------



## AGCurry (Jun 15, 2005)

lotuseclat79 said:


> Hi csross,
> 
> You need to make what you call the second program a subroutine and pass it the parameters declared in the first program or main routine. After the subroutine call returns the parameter values, use the names you referred the parameters by in the first routine to continue your computation.
> 
> -- Tom


Excuse me, this is not correct. The system() function can be used.

Create a string (we can call it "command_string") consisting of the program to be called and its arguments. Then, just:

system( $command_string ) ;


----------



## cristobal03 (Aug 5, 2005)

It's been forever since I used perl, but I'm pretty sure that system returns an integer based on its success. The original poster wants to capture returns, so unless he/she instantiated containers that were passed as references rather than values in the calling procedure, *system* wouldn't be an effective method for this application.

Nevermind this post if the information is inaccurate.

chris.


----------



## AGCurry (Jun 15, 2005)

If you want to reference an exit status from the called program, system() will return that. If the called program writes the desired value(s) to standard output, you can call the program with back ticks to capture that output. If it's something more complex than this, then making the called program into a subprogram is probably the way to go.


----------

