Hi,
For some reasons, I have to keep my sub-programs in 2 different .pl files. Both sub-programs are having the same subroutine name. In my main program, I need to determine which sub-program to be called when it meets certain criteria. This is not an issue until I have some mixture of conditions which will switch between the 2 sub-program in different looping. This causes the subroutine to be re-defined and I get undesired results. Is there a better way of doing this, considering I need to keep the sub-programs with the same subroutine names into 2 separate files and switch in between them in main program? Below is the simulation code to explain the issue that I have. Thanks.
~ahgan
##### script1.pl ##### -
sub Process {
-
my $var1 = shift;
-
my $var2 = shift;
-
return ($var1 * $var2);
-
}
-
1;
-
##### script2.pl ##### -
sub Process {
-
my $var1 = shift;
-
my $var2 = shift;
-
my $ans = "";
-
if ($var2 != 0) {
-
$ans = $var1 / $var2
-
}
-
else {
-
$ans = "Error: Divide by 0";
-
}
-
return $ans;
-
}
-
1;
-
##### main.pl ##### -
use strict;
-
use warnings;
-
-
my @num = (1,3,5,7,9);
-
my @methods = ("multiplication","division","multiplication");
-
my $runscript = "";
-
my $result = "";
-
-
for my $method (@methods) {
-
for (0 .. $#num) {
-
if ($method eq "multiplication") {
-
$runscript = "script1.pl";
-
}
-
elsif ($method eq "division") {
-
$runscript = "script2.pl";
-
}
-
else {
-
print STDERR "Unknown Method\n";
-
}
-
-
require "$runscript";
-
$result = &Process($num[$_],$num[$_]*$num[$_]);
-
print "Result for $method of $num[$_] to " . $num[$_]*$num[$_] . " : $result\n";
-
}
-
}
-
5 3589
Unless this is some sort of learning excersize, there is no reason to use seperate files or use the same subroutine name. Put it all in one script. But if for some reason you wanted to continue with this strange setup, try using undef() on the subroutine. -
use strict;
-
use warnings;
-
-
my @num = (1,3,5,7,9);
-
my @methods = ("multiplication","division","multiplication");
-
my $runscript = "";
-
my $result = "";
-
-
for my $method (@methods) {
-
for (0 .. $#num) {
-
if ($method eq "multiplication") {
-
$runscript = "script1.pl";
-
}
-
elsif ($method eq "division") {
-
$runscript = "script2.pl";
-
}
-
else {
-
print STDERR "Unknown Method\n";
-
}
-
-
require "$runscript";
-
$result = &Process($num[$_],$num[$_]*$num[$_]);
-
undef &Process;#<---- here undef the subroutine
-
print "Result for $method of $num[$_] to " . $num[$_]*$num[$_] . " : $result\n";
-
}
-
}
-
Unfortunately, I need to stick with the setup as I need to load different simulation codes under different circumstances. Anyway, the code undef &Process;
does not seem to be working. I will have to find other resolution. Thanks for the suggestion.
Unfortunately, I need to stick with the setup as I need to load different simulation codes under different circumstances. Anyway, the code undef &Process;
does not seem to be working. I will have to find other resolution. Thanks for the suggestion.
One way would be to include
and
at the top of script1.pl & script2.pl respectively. And modify your main.pl as below: -
for my $method (@methods) {
-
for (0 .. $#num) {
-
if ($method eq "multiplication") {
-
$runscript = "script1.pl";
-
}
-
elsif ($method eq "division") {
-
$runscript = "script2.pl";
-
}
-
else {
-
print STDERR "Unknown Method\n";
-
}
-
require "$runscript";
-
my $result;
-
$result = &script1::Process($num[$_],$num[$_]*$num[$_]) if($runscript eq "script1.pl");
-
$result = &script2::Process($num[$_],$num[$_]*$num[$_]) if($runscript eq "script2.pl");
-
print "Result for $method of $num[$_] to " . $num[$_]*$num[$_] . " : $result\n";
-
-
}
-
}
-
-
Thanks for all the pointers!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: BillD |
last post by:
I'm trying to derive a schema from a base schema. I want to redefine
a "group" from the base schema in my derived schema in order to add
more options to the "choice" aggregate (see schema1.xsd...
|
by: Cat |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I'm getting a validation error when I try to restrict the content of nested
groups with xs:redefine whereas the same restriction on xs:element's...
|
by: junlia |
last post by:
We are using ACORD xml schema standard, and we need to add to it, so we
choose to redefine ACORD xml schema. One of the problems that I ran into is
how to add some values to an emumerated list.
...
|
by: Richard Grant |
last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter
How do I do that in VB .NET
What should be the syntax for a parameter to receive the address of a...
|
by: singlal |
last post by:
Hi, my question was not getting any attention because it moved to 2nd
page; so posting it again. Sorry for any inconvenience but I need to
get it resolved fast. Need your help!
...
|
by: nasau |
last post by:
Perl, I have a main program which calls two subroutines (depending upon the report names).In the subroutine I am printing the data from CSV file using the format variable, Format_top prints the...
|
by: peterv6 |
last post by:
I'm using a "package" type subroutine, called test_package.pl. I'm calling it from a script called split0.pl. I want to pass the $0 variable, use the subroutine to split out just the filename, and...
|
by: ERingmae |
last post by:
Hi,
The environment is .NET 2.0, the language is C# and the problem is
reading XSD file with xs:redefine section correctly to a
XMLDataDocument.DataSet.
What I am trying to do:
I am trying...
|
by: Ernie.Pasveer |
last post by:
Hi All,
Is there a way to create a macro called #redefine that will allow
redefinition without having to use #ifdef/#undef
For example, I'd like to do something like this :
#define THING ...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |