473,657 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to redefine subroutine using Require

13 New Member
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 #####
Expand|Select|Wrap|Line Numbers
  1. sub Process {
  2.     my $var1 = shift;
  3.     my $var2 = shift;
  4.     return ($var1 * $var2);
  5. }
  6. 1;
  7.  
##### script2.pl #####
Expand|Select|Wrap|Line Numbers
  1. sub Process {
  2.     my $var1 = shift;
  3.     my $var2 = shift;
  4.     my $ans = "";
  5.     if ($var2 != 0) {
  6.         $ans = $var1 / $var2 
  7.     }
  8.     else {
  9.         $ans = "Error: Divide by 0";
  10.     }
  11.     return $ans;
  12. }
  13. 1;
  14.  
##### main.pl #####
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my @num = (1,3,5,7,9);
  5. my @methods = ("multiplication","division","multiplication");
  6. my $runscript = "";
  7. my $result = "";
  8.  
  9. for my $method (@methods) {
  10.     for (0 .. $#num) {
  11.         if ($method eq "multiplication") {
  12.             $runscript = "script1.pl";
  13.         }
  14.         elsif ($method eq "division") {
  15.             $runscript = "script2.pl";
  16.         }
  17.         else {
  18.             print STDERR "Unknown Method\n";
  19.         }
  20.  
  21.         require "$runscript";
  22.         $result = &Process($num[$_],$num[$_]*$num[$_]);
  23.         print "Result for $method of $num[$_] to " . $num[$_]*$num[$_] . " : $result\n";
  24.     }
  25. }
  26.  
Oct 28 '08 #1
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.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my @num = (1,3,5,7,9);
  5. my @methods = ("multiplication","division","multiplication");
  6. my $runscript = "";
  7. my $result = "";
  8.  
  9. for my $method (@methods) {
  10.     for (0 .. $#num) {
  11.         if ($method eq "multiplication") {
  12.             $runscript = "script1.pl";
  13.         }
  14.         elsif ($method eq "division") {
  15.             $runscript = "script2.pl";
  16.         }
  17.         else {
  18.             print STDERR "Unknown Method\n";
  19.         }
  20.  
  21.         require "$runscript";
  22.         $result = &Process($num[$_],$num[$_]*$num[$_]);
  23.         undef &Process;#<---- here undef the subroutine
  24.         print "Result for $method of $num[$_] to " . $num[$_]*$num[$_] . " : $result\n";
  25.     }
  26. }
  27.  
Oct 28 '08 #2
ahgan
13 New Member
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.
Oct 28 '08 #3
nithinpes
410 Recognized Expert Contributor
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
Expand|Select|Wrap|Line Numbers
  1. package script1;
  2.  
and

Expand|Select|Wrap|Line Numbers
  1. package script2;
  2.  
at the top of script1.pl & script2.pl respectively. And modify your main.pl as below:

Expand|Select|Wrap|Line Numbers
  1. for my $method (@methods) { 
  2.     for (0 .. $#num) { 
  3.         if ($method eq "multiplication") { 
  4.                  $runscript = "script1.pl"; 
  5.            } 
  6.         elsif ($method eq "division") { 
  7.           $runscript = "script2.pl"; 
  8.                   } 
  9.               else { 
  10.             print STDERR "Unknown Method\n"; 
  11.         } 
  12.         require "$runscript"; 
  13.         my $result;
  14.         $result = &script1::Process($num[$_],$num[$_]*$num[$_])  if($runscript eq "script1.pl"); 
  15.         $result = &script2::Process($num[$_],$num[$_]*$num[$_])  if($runscript eq "script2.pl");
  16.         print "Result for $method of $num[$_] to " . $num[$_]*$num[$_] . " : $result\n"; 
  17.  
  18.  } 
  19.  
  20.  
Oct 29 '08 #4
KevinADC
4,059 Recognized Expert Specialist
Thats a good suggestion.
Oct 29 '08 #5
ahgan
13 New Member
Thanks for all the pointers!
Nov 18 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3349
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"/>
1
2217
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">
3
3605
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"/>
7
9558
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
2
1434
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
10
1878
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...
1
1903
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...
2
5847
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.
3
18718
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
0
8420
marktang
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...
0
8324
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,...
0
8617
tracyyun
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...
0
7353
agi2029
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...
0
4173
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...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.