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

Hi monks
I have a bit of a strange problem:
I have written an OOP program (hc_url.pm) that uses a function from another program (ml_lwp_wrapper), called call_httpd_exec. It gets the other program with "require". This works fine. My problem is that I now need to make this program create a new object, that also wants to use the same function from ml_lwp_wrapper (also retrieved with require). However, when I run the program, I get this message:
Undefined subroutine &hc_url::call_httpd_exec called at /exlibris/meta +lib/m4_b/dat01/vir_ext/hc_url.pm line 166.
I know that this is a bit complicated, but the basic outcome is that the original program doesn't recognize the function anymore!! Does anyone know why this is the case, and what I can do to solve this problem?
Thanks in advance
Guy (mrguy123) Update: What the programs does isn't very important. The main thing is that both programs have this code in them:
require "ml_lwp_wrapper"; &call_httpd_exec($hash);
Problem is, when the first program creates an instance of the second program
my $authen = ml_authen->new(\%params);
the first program can't use the function anymore

Update 2: I added 4 snippet programs in the reply section. You can download them and get the same error message (just change the Perl path)

Replies are listed 'Best First'.
Re: problem with repeating function
by davorg (Chancellor) on Sep 04, 2006 at 08:44 UTC

    Can you demonstrate the problem with a short piece of code. It's easier to follow code than narrative descriptions. Your description is a little unclear.

    My first guess is that you need to also "require" the second program in the class that creates the object that you are talking about. But my second thought is that if this function is as useful as you think it is, then you should consider putting it into a real module which will make it easier to use elsewhere.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: problem with repeating function
by Corion (Patriarch) on Sep 04, 2006 at 08:45 UTC

    Maybe if you show us the relevant code, we can help you better.

    I suggest that you reduce both, your main program and your module to the minimal code that still reproduces the problem and then post these two.

Re: problem with repeating function
by shmem (Chancellor) on Sep 04, 2006 at 11:19 UTC
    In perl OOP, a constructor generally blesses a newly created object, so the object "knows" in which package it lives. Read bless

    The error message you got

    Undefined subroutine &hc_url::call_httpd_exec called at /exlibris/meta +lib/m4_b/d +at01/vir_ext/hc_url.pm line 166.

    tells you that the routine call_httpd_exec was looked up in package hc_url, which is not what you expected - not in package demo nor in package demo2.

    Read perltoot.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      The error message:
      Undefined subroutine &hc_url::call_httpd_exec called at /exlibris/meta +lib/m4_b/d +at01/vir_ext/hc_url.pm line 166.
      is for my real program. The error code for my demo programs is :
      Undefined subroutine &wrapper::call_httpd_exec called at demo.pm line +8.
      hc_url isn't used. Does this change your answer?
        Yes, the part w/regard to the error message changes.

        You do a require "wrapper" in package demo and in package demo2. demo2 is used by demo. The use means, that demo2 is compiled and executed before demo is even compiled to end.

        The docs for require state, that require doesn't include a file twice.

        So, wrapper is already loaded via demo2, so it lives in demo2. The require in demo just returns 1, no file is loaded. You'll see that your code works if you change call_httpd_exec in demo to demo2::call_httpd_exec.

        <update>
        OTOH, if you wrap require "wrapper" in demo into a BEGIN block and place it before use demo2, then the call to call_httpd_exec will fail in demo2, since it now lives in demo.

        Read require, use, again perltoot, perlmod, there BEGIN , CHECK, INIT and END... there are also modules which implement automatic exportation of functions, e.g. Spiffy.
        </update>

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: problem with repeating function
by mrguy123 (Hermit) on Sep 04, 2006 at 09:35 UTC
    OK, since reducing my programs from over a thousand lines of code to a short snippet was not so simple, I wrote a few short snippets that show you the same problem: Program 1 (the executable):
    #!/exlibris/metalib/m4_b/product/bin/perl use demo; { my $externalObject = new demo; }
    Program 2: demo.pm - an instance is created by the executable:
    package demo; require "wrapper"; use demo2; sub new { my $class = shift; my $params = shift; my $self = {}; &call_httpd_exec(); my $externalObject2 = new demo2; }
    Program 3: demo2.pm - an istance is created by demo.pm
    package demo2; require "wrapper"; sub new { my $class = shift; my $params = shift; my $self = {}; &call_httpd_exec(); }
    Program 4: wrapper - the required program:
    sub call_httpd_exec{ print "hello world!!\n" } 1;
    When I run the executable program, I get this message:
    Undefined subroutine &demo::call_httpd_exec called at demo.pm line 8.
    If I don't make demo.pm create an instance of demo2.pm there is no problem. Why do you think adding demo2.pm creates a problem?
Re: problem with repeating function
by cdarke (Prior) on Sep 04, 2006 at 11:23 UTC
    You are mixing 'require' with 'use', is there a reason for that? 'require' is executed at run-time and 'use' at compile time.
    You do not appear to be exporting the subroutine name. That is not necessary if called as a method (using the -> convention) or direct, using wrapper::call_httpd_exec. Otherwise ther is no way for Perl to know which namespace (package) the subroutine is in.
    (BTW I don't recommend using the & prefix for a straight subroutine call unless you have a good reason).
      ---You are mixing 'require' with 'use', is there a reason for that? I am using both "require" and "use" programs in this case. I'm not sure what you mean about mixing them up. ---That is not necessary if called as a method (using the -> convention) or direct, using wrapper::call_httpd_exec I used wrapper::call_httpd_exec in my program but still recieved the same error message. Any ideas?
Re: problem with repeating function
by Fletch (Bishop) on Sep 04, 2006 at 15:51 UTC

    Not directly germane to the problem (I don't think at least; not that it's very clear what the problem is . . .), but Perl convention is to use Capitalized names for modules; lower cased names are reserved for language use.