Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have 2 modules, say A & B. Each of these modules have a function "just_print" . I have another perl program "my_prog" which calls this function depending on the user input, meaning - if user executes my_prog A , "just_print" function of A should be executed, if user executes my_prog B , "just_print" function of module B should be executed. How can I do this ? It seems to me a easy question but actually it is not. I have been struggling with this the whole day. Now this 'just_print' function has a call to 'main_print' function which is present in 'my_prog'. How can I execute this ? main::main_print gives me errors. Below is the pseudocode :
my_prog.pl ---------- use A use B if <arg1> { A.just_print("hi") }else if <arg2> { B.just_print("hello") } sub main_print(){ print @_; } A.pm ---- package A sub just_print(){ print @_; main::main_print(); <-- error } B.pm ---- package B sub just_print(){ print @_; main::main_print(); <-- error }
thanks in advance for your answers novice
  • Comment on calling functions with same names but in different modules depending on user input
  • Download Code

Replies are listed 'Best First'.
•Re: calling functions with same names but in different modules depending on user input
by merlyn (Sage) on May 06, 2004 at 15:05 UTC
    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.

      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
Re: calling functions with same names but in different modules depending on user input
by blue_cowdawg (Monsignor) on May 06, 2004 at 15:29 UTC

        thanks in advance for your answers novice

    ROTFLMAO!
    Sorry... not laughing at you, I was laughing at myself. I dowloaded your code and replaced the psuedocode with real code and boggled at why I kept getting errors to the effect of:

    Undefined subroutine &B::just_print called at index.pl line 8.
    and then it dawned on me what was going on.

    When you are doing a use B; you are pulling in the B perl compiler and not getting the expected results. Anyway....

    I modified your example and came up with the following:

    # The main code: use A; use Blooper; foreach (0..1){ A::just_print("hi") if $_; B::just_print("hello") if not $_; } sub main_print(){ print @_; }
    and
    #A.pm package A; sub just_print{ print @_; main::main_print(); } 1;
    and
    #Blooper.pm package B; sub just_print { print @_; main::main_print(); } 1;
    and when the code is run I get:
    hellohi
    as I would expect.

    With all that in mind... what did you get for errors?

Re: calling functions with same names but in different modules depending on user input
by Fletch (Bishop) on May 06, 2004 at 15:11 UTC

    Dittos on merlyn's OOP comments. If you're not ready to go whole hog OO you could go half hog using a hash as a dispatch table.

    my %dispatch = ( foo => \&A::just_print, bar => \&B::just_print ); if( exists $dispatch{ $user_input } ) { $dispatch{$user_input }->( "hello" ); } else { warn "Unknown user input "$user_input"\n"; }
Re: calling functions with same names but in different modules depending on user input
by itub (Priest) on May 06, 2004 at 15:10 UTC

    I tried the code and it worked (after translating it to real code). What error message did you get? Are you using strict and warnings? That could also help catch simple errors.

    Of course, it's better to objects anyway.

Re: calling functions with same names but in different modules depending on user input
by Anonymous Monk on May 07, 2004 at 07:23 UTC
    thanks guys for your inputs..I will go with the OO aproach then. it seems to be the easiest.