in reply to calling functions with same names but in different modules depending on user input

Sounds like you're reinventing Object Oriented Programming the hard way. "Same subroutine in different packages based on a string" is another way of saying "same method in different classes based on type". Consider this code:
{ package A; sub just_print { my $class = shift; # extra parameter must be accounted for print "In A: @_\n"; } } { package B; sub just_print { my $class = shift; # extra parameter must be accounted for print "In B: @_\n"; } } # back to package main: print "A or B: "; chomp(my $selector = <STDIN>); $selector =~ /^[AB]$/ or die "I said A, or B!"; $selector->just_print(1..10); # prints 1..10
For more info, see perlboot.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re: calling functions with same names but in different modules depending on user input
  • Download Code

Replies are listed 'Best First'.
Re: •Re: calling functions with same names but in different modules depending on user input
by Anonymous Monk on May 06, 2004 at 18:18 UTC
    Thanks for the comments ,OO solution was great ! why didnt I think of this before ? studpid me ! ok - while we are on the topic, consider this code :
    #!/usr/local/bin/perl use standard; use nonstandard; my $option = "standard"; my_print("i m in main\n"); $option::just_print(); <-- error :syntax error at main.pl near "$opt +ion::just_print(" sub my_print(){ print @_; } package nonstandard; sub just_print{ print "in just_print nonstandard \n"; main::my_print("calling my_print from nonstandard\n"); } 1; package standard; sub just_print{ print "in just_print standard \n"; main::my_print("calling my_print from standard\n"); } 1;
    $option::just_print( ) gives errors, so I did this : my $a = "&"."$adaption"."::just_print()"; eval $a; Is their any better way to do this(apart from our great OO) ?(consider that the two packages have 10-15 functions having similar names,so executing it in the above way is not a good way) I guess, you guys have already answered this..but just for clarification sake