Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling one Perl program from another

Status
Not open for further replies.

jpasquini

Programmer
Apr 20, 2006
44
US
Hi All,

Does anyone know a quick and dirty way to have one Perl program call another as a module.

I have two complete and separate Perl programs, one gives a list of log files and then when you select one, and click "View", it runs the other program which displays the file nice and orderly for the user to see.

The way I have it set up now is using a 'system' call, which takes the name of the second program in a string $syscommand and runs

$my_output = system ($syscommand)

This runs the program just fine, passing the selected record as an argument, and after closing goes back where you started.

However...........the local Powers that Be want me to change this by instead making the second program a module, being referenced as a subroutine in the first program.
So (I assume) there would be a line in the first program:

use 2nd_program;

and then a call in the first program:

2nd_program_subroutine();

I have tried examples, articles, etc, and the farthest I've gotten is a memory fault/core dump.
Is there a way to convert a freestanding 2nd program quickly into a module, and run it from the 1st program?
Would appreciate advice....thanks

jpasquini

 
.....Sorry my brain is fried.

What was going under

# do something
?


As far as I understand, 'require' allows you to run a module on the fly, whereas 'use' will compile the program ahead of time and show you any errors before it starts.

The 'textbook example' is to add these lines to the top of the 2nd program:


package script2;
use Exporter;
@ISA = ('Exporter');
@EXPORT = ('script1');

Then have a subroutine called subscript2 { EVERYTHING INSIDE 2nd SCRIPT } 1; in script 2.

Then in script 1, call it with:

subscript2()


Not a chance the examples would work. That would be someone else's luck.........not mine.

If I don't reply you're too late, a faster, smarter H1 worker has already replaced me at half salary.......

 
If you just want to use a subroutine from another script then you do:
Code:
require "[i]ScriptName.pl[/i]";

[i]script_sub_routine();[/i]
If you want to use a subroutine from a module then you do:
Code:
use [i]PackageName[/i];

[i]package_sub_routine();[/i]
Note: Within your package all routines must be exported if you want to use them within your script.

M. Brooks
 
mbrooks,

Using a <require "script2"> in script1, and then putting the entire 2nd program in a subroutine, and calling it with sub_script2(); in the first program gives me

Variable XXX will not stay shared at script2
Variable XXY will not stay shared at script2
Variable XXZ........

and then

Memory fault(coredump)

????


It seems to insist on a memory fault and a dump, I've been confronted with this for quite some time this afternoon.
Not sure why.
 
O.K. Package your script like..
Code:
package MyPackage;

use strict;
use warnings;

use vars qw( @ISA @EXPORT_OK );

require Exporter;
@ISA       = qw( Exporter );
@EXPORT_OK = qw( my_sub );

sub my_sub {
    print "Hello World";
}
And call it from your script like..
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

use MyPackage qw( my_sub );
 
my_sub();

M. Brooks
 
No good.

The 1st program has to have a flag and a user ID to run (originally). The actual program name is 'makeiss'. To execute for instance, you would enter:

makeiss -u 372955 (userid)

After putting the above references in, it bombs with:

Unknown option: u
Usage:
makeiss -u *userid*


As you can see, that makes absolutely no sense. Certainly if it requires 'u' as a flag, then it should not refuse to accept 'u' as a flag. I wonder if actual clamming is easier than this language.....

 
Many parts of this script will need to be rewritten in order for this to work as a module. This is not going to be as easy as placing the script in a subroutine and calling it from another script.

The system() call makes a better and easier solution considering the circumstances.

M. Brooks
 
I thought so too. Problem is, the higher ups want it as a module instead of a system call.

To simplify all that:
In theory, the line in Program #1 that calls:

my $command_output = system ($syscommand);

would be replaced with

module_release();



And then you would go into the release program (Program #2) and add the Package, vars and Export lines at the top, and make the whole rest of the program into one huge subroutine with {}'s at each end. Badabing. Done.

It would be the same thing as having it all in one program, except it's two programs, one calling the other as a subroutine.
But they want it in two, because, they do.

.....



 
I'm emailing my plea up the chain to leave it as a system call. There are some security concerns I guess with one program calling another, and whether everything will be exported. And maybe some kind of performance hit because it is a subshell(??) I can't see it being much. I think they can be addressed.

Aside from that, it's just way to easy compared to this module mumbo-jumbo.
Cripes it's taken me hours of fruitless research and the thing already worked fine in the beginning..........


 
In cases like this there is no reason to re-invent the wheel. Especially if it works from the getgo. Since there are so many modules being used within this script it makes it quite difficult to figure what the script is actually doing.

M. Brooks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top