473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to redefine subroutine using Require

13
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 3589
KevinADC
4,059 Expert 2GB
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
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 Expert 256MB
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 Expert 2GB
Thats a good suggestion.
Oct 29 '08 #5
ahgan
13
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
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...
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...
3
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. ...
7
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...
2
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! ...
10
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...
1
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...
2
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...
3
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 ...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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
1
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...
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

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.