By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

How to redefine subroutine using Require

P: 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
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
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

P: 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
Expert 100+
P: 410
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
Expert 2.5K+
P: 4,059
Thats a good suggestion.
Oct 29 '08 #5

P: 13
Thanks for all the pointers!
Nov 18 '08 #6

Post your reply

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