Home > Archive > PERL Beginners > December 2004 > Running a perl script thru a perl script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Running a perl script thru a perl script
|
|
| Dave Kettmann 2004-12-21, 8:55 pm |
| Hello,
I have built a script that has 2 environment variables passed to it and =
it will take that data and return a value. This script works fine, but =
there are many possibilites that can be passed to it. I want to build a =
test case of sorts to pass to this script. In 'meta-code' this is what I =
would like to do:
@from_numbers =3D qw( "6364421234", "6364421234", "3143221234", =
"3143221234" );
@to_numbers =3D qw( "4225678", "6364425678", "3212222", "3143212222");
while (@from_numbers) {
set environment variable to from_number
set additional variable to to_number
run script
}
My main question: Is there a better way to run the other perl script =
other than running an exec()? I am trying to keep processor usage down =
to a minimum and I know that the exec() function opens up a shell (which =
takes both time, memory, and processor). I dont know if perl has a =
'better' way to handle running another perl script.
A quick explanation of the script that I want to run within this script: =
The script will take a phone 'from phone number' and a 'to phone number' =
and return a value based on the 2 numbers. In trying to fix some things, =
I ended up breaking others. In order to test this manually I have to go =
in, edit the file, then run the script and find the result. I know how =
to go about writing the rest of my test case script, just need to know =
if there is a better way to run the one script within the other, =
hopefully avoiding exec().
Any help is appreciated and if more info is needed, let me know and I =
will supply it.
Thanks,
Dave Kettmann
NetLogic
636-561-0680
| |
| John Moon 2004-12-21, 8:55 pm |
| Subject: Running a perl script thru a perl script
Hello,
I have built a script that has 2 environment variables passed to it and it
will take that data and return a value. This script works fine, but there
are many possibilites that can be passed to it. I want to build a test case
of sorts to pass to this script. In 'meta-code' this is what I would like to
do:
@from_numbers = qw( "6364421234", "6364421234", "3143221234", "3143221234"
);
@to_numbers = qw( "4225678", "6364425678", "3212222", "3143212222");
while (@from_numbers) {
set environment variable to from_number
set additional variable to to_number
run script
}
My main question: Is there a better way to run the other perl script other
than running an exec()? I am trying to keep processor usage down to a
minimum and I know that the exec() function opens up a shell (which takes
both time, memory, and processor). I dont know if perl has a 'better' way to
handle running another perl script.
A quick explanation of the script that I want to run within this script: The
script will take a phone 'from phone number' and a 'to phone number' and
return a value based on the 2 numbers. In trying to fix some things, I ended
up breaking others. In order to test this manually I have to go in, edit the
file, then run the script and find the result. I know how to go about
writing the rest of my test case script, just need to know if there is a
better way to run the one script within the other, hopefully avoiding
exec().
Any help is appreciated and if more info is needed, let me know and I will
supply it.
########
Not sure I understand your complete requirement but I use a "BEGIN" block to
read a file and set the email address I wish to send a message as the email
address can change and it depending on if I am test or production
environment... (I could have done the same thing without the BEGIN but I
already had the block for other things...)
So the script always sends email to $ENV{Admin} ... and BEGIN block always
reads a file at $ENV{HOME}/email.addrs, parses the file, and set various
$ENV values...
You may wish to read about "Package Constructors and Destructors: BEGIN and
END"
Hope this gives you some ideas ...
jwm
| |
| Bob Showalter 2004-12-21, 8:55 pm |
| Dave Kettmann wrote:
> Hello,
>
> I have built a script that has 2 environment variables passed to it
> and it will take that data and return a value. This script works
> fine, but there are many possibilites that can be passed to it. I
> want to build a test case of sorts to pass to this script. In
> 'meta-code' this is what I would like to do:
>
> @from_numbers = qw( "6364421234", "6364421234", "3143221234", "3143221234"
);
> @to_numbers = qw( "4225678", "6364425678", "3212222", "3143212222");
Erm, you're not using qw() properly.
>
> while (@from_numbers) {
>
> set environment variable to from_number
> set additional variable to to_number
> run script
>
> }
>
> My main question: Is there a better way to run the other perl script
> other than running an exec()? I am trying to keep processor usage
> down to a minimum and I know that the exec() function opens up a
> shell (which takes both time, memory, and processor). I dont know if
> perl has a 'better' way to handle running another perl script.
You wouldn't use exec(), since that replaces your currently running process
with another. You would use system() or backticks. None of these use a shell
unless
you include shell meta-characters.
There's no reason to avoid these calls, IMO. This is the most straightfoward
way to approach it.
I would suggest something like this:
while (<DATA> ) {
($ENV{VAR1}, $ENV{VAR2}) = split;
system('/path/to/myscript.pl');
}
__DATA__
6364421234 4225678
6364421234 6364425678
... and so on
HTH
| |
| John W. Krahn 2004-12-21, 8:55 pm |
| Dave Kettmann wrote:
> Hello,
Hello,
> I have built a script that has 2 environment variables passed to it and it
> will take that data and return a value. This script works fine, but there
> are many possibilites that can be passed to it. I want to build a test case
> of sorts to pass to this script.
You should probably search for "test" on http://search.cpan.org.
http://search.cpan.org/search?m=module&q=test&s=1&n=100
> In 'meta-code' this is what I would like to do:
>
> @from_numbers = qw( "6364421234", "6364421234", "3143221234", "3143221234" );
> @to_numbers = qw( "4225678", "6364425678", "3212222", "3143212222");
If you had warnings enabled then perl would have warned you about that:
$ perl -le'use warnings; my @x = qw( "1", "2", "3" ); print for @x'
Possible attempt to separate words with commas at -e line 1.
"1",
"2",
"3"
So what you want is either:
my @from_numbers = qw( 6364421234 6364421234 3143221234 3143221234 );
my @to_numbers = qw( 4225678 6364425678 3212222 3143212222 );
Or:
my @from_numbers = ( 6364421234, 6364421234, 3143221234, 3143221234 );
my @to_numbers = ( 4225678, 6364425678, 3212222, 3143212222 );
Or even:
my @from_numbers = ( '6364421234', '6364421234', '3143221234', '3143221234' );
my @to_numbers = ( '4225678', '6364425678', '3212222', '3143212222' );
And of course, you should probably be using strict as well.
> while (@from_numbers) {
>
> set environment variable to from_number
> set additional variable to to_number
> run script
>
> }
From your pseudo code description, you may want to use an array of arrays or
array of hashes instead, if you want to keep the corresponding from and to
numbers together.
> My main question: Is there a better way to run the other perl script other
> than running an exec()? I am trying to keep processor usage down to a
> minimum and I know that the exec() function opens up a shell (which takes
> both time, memory, and processor).
It only uses a shell in certain circumstances:
perldoc -f exec
[snip]
If there is more than one argument in LIST, or if LIST is an array
with more than one value, calls execvp(3) with the arguments in
LIST. If there is only one scalar argument or an array with one
element in it, 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.
John
--
use Perl;
program
fulfillment
|
|
|
|
|