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 3631 KevinADC 4,059
Recognized Expert Specialist
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";
-
-
}
-
}
-
-
KevinADC 4,059
Recognized Expert Specialist
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 sample).
schema1.xsd sample:
<!-- Here is a clipped down version of the group I want to redefine.
-->
<xsd:group name="INSURANCESVCRQMSGS">
<xsd:choice>
<xsd:element ref="HomePolicyAddRq" minOccurs="1" maxOccurs="1"/>
|
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 validates.
============== BASE XMLSCHEMA =================
<?xml version="1.0"?>
<xs:schema targetNamespace="test" xmlns="test"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
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.
For an emumerated list like this:
<xsd:complexType name="AttachmentType">
<xsd:simpleContent>
<xsd:restriction base="OpenEnum">
<xsd:enumeration value="040"/>
<xsd:enumeration value="041"/>
|
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 subroutine
Let's say theres a sub that creates buttons and I want it to receive as a parameter the address of the sub that handles the OnClick event for the button being created
How do I pass such a parameter
Thanks in advance
Richar
|
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!
****************************************************************************************************
Original Question:
--------------------
Has anyone called a COBOL subroutine using COBOL CALL from a COBOL/DB2
| |
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 report header and STDOUT prints the report data. Both these subroutine runs fine individually, independent of each other. Now I have included both these subroutine in my main program and I call these subroutine depending upon the name of the report. Here...
|
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 pass that filename value back to the calling script. The subroutine does the processing correctly (I've verified with print statements), but I'm having trouble getting the value passed back to the calling script.
Here's the subroutine:
package...
|
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 to create a DataSet object from an XSD file by using
XMLDataDocument class.
|
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 "apple"
... code
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |