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

Hi, I have come across a scenario in which i want to override function in another file with the same name. although the functionality willbe different but function name will be same. Is there a way to achieve this with object oriented. Any sort of help or guidance will be of utmost value. Waiting for a prompt reply.
  • Comment on Does perl allow overriding a function in two different files(.pl)

Replies are listed 'Best First'.
Re: Does perl allow overriding a function in two different files(.pl)
by davido (Cardinal) on Jul 14, 2011 at 22:15 UTC

    Usually when the question is "Does Perl allow...", the answer is yes. But specifically demonstrating the implementation requires that both parties have a meeting of the minds on what is being discussed.

    Does Perl allow you to override functions defined in other modules? Yes. Does it allow you to override functions defined as class or object methods? Yes, there are ways.

    But I think the first step is for us all to be on the same page. The Perl way has evolved over the years into a pretty elaborate framework of packages and modules. For us to fully understand what you're asking, you would need to learn the basics of the Perl way first so that you can accurately identify for us what aspect of overriding functions in Perl you're discussing. And you would need those basics in place to understand the responses you get as well.

    Once you've had a look at perlmod and perlobj, you may feel more comfortable with the principles and with the nomenclature of Perl's namespaces, modules, packages, and OO system. At that point we would come closer to a meaningful discussion.

    There certainly are answers to your question, and we would love to help. I believe that spending an hour looking over the two documents I pointed you to is the best and most accurate help I can offer until our languages sync up sufficient to answer more specifically.

    I can say that it really helps if you use Perl packages, and modules. Doing so defines namespaces, relationships of interaction, and dependencies. If you're using, say module A, and you have a function in A called do_it(), but you decide that you no longer want do_it() done the A way, you have many options. Several of those options include that you can literally attach a new function in Perl's symbol table for A with the name do_it(), or you can create a new function in your current symbol table named do_it() that masks A's do_it() unless you use the fully qualified name( A::do_it() ) or a reference to that function. If you're using an OO approach, some of that applies too, but you also have options for inheritance, where a B inherits from class A, and may in so doing keep some of A's functionality while overriding other functionality.

    By learning Perl's object and module system, you can take advantage of the tools, practices, and understanding of the Perl community, which provides wonderful leverage. Clumping together a bunch of .pl files in a folder and somehow evaling them, doing them, or require-ing them in non-standard ways gives you a very short lever to work with, as there won't be a set of established knowledge and tools already in existence to help you through your problem.

    So please do go ahead and take a moment with those documents linked to. And if you really grow an interest, pick up the O'Reilly book, "Intermediate Perl." Then you'll be well on your way.


    Dave

      Hi Dave,

      Thanks for elaborating the point of knowing that both parties are on same page before answering the question. I have looked into my question and it is not clear enough to answer specifically. So Let me explain what exactly i want or what is exactly the question is. Lets suppose we have three files:

      1. Start.pl( main entry point of the program from command prompt )

      2. Process.pl( This file is required in start.pl using require. This file contains functions and start.pl will invoke its function.)

      3. program.pl( This file is required in process.pl and contain some functions too).

      Now scenario is we define a function ABC() in process.pl and program.pl both with different functionality. Is there a way we can refer function ABC() of program.pl from within process.pl on run time. If we can do so what will be the affect on global variables of start.pl that are being used in function ABC() of program.pl.

      I hope i have clarify my self more than previously. Any suggestions will be of utmost importance, since im stuck on this for a week now.

        It certainly makes sense that if you've been stuck for a week you would need answers, and fast, I'm sure. So I'm sure you have by now taken a moment to look over perlmod, as previously recommended. And that being the case, I can now ask the following:

        What are your package names?

        You see, having read perlmod, you would now know that giving package names gives you namespaces. You might, for example, set a package Process; in Process.pl, and a package Program; in program.pl. Then, depending on an object oriented interface or a functional interface, you might export functions from program.pl, importing them into Process.pl's namespace, or you might inherit a class from program.pl into Process.pl. At that point you would have a framework that would be easy to manipulate.

        Having read perlmod as recommended in my previous answer you would also have discovered that there is an advantage to using .pm as the extension for the filenames of your packages. In fact, a package name, a file ending in .pm, some functions defined, and a '1' at the end is the simplest and most basic framework of a Perl5 module.

        But even if you only give your functions a different namespace for each file using package, at least we would be able to use fully qualified names to manipulate our symbol tables so that we're using the version of a function that we want to use.

        If you haven't given packages namespaces, and you've got one function in one file and another in another file both with the same name, you could, I suppose, take a function reference to the first function and save it away, and then after requiring the second file, take a function reference to its function by the same name. Now you've got two function references; one pointing to the original function, and one pointing to the other one. From there you could invoke whichever one you please.

        But that's going to get old fast... devising your own manipulations to avoid having to read the Perl documentation enough to know how to create packages (namespaces), and how to use either Exporter or inheritance, you'll be reinventing the wheel at every corner, and will continue being held up by a week here, and a week there.

        So having followed the previous advice, where are you now hung up? And if you haven't followed the previous advice, what is the motivation for me to provide additional? Of course maybe you have done the required reading. If that's the case I apologize for wasting your time with this explanation, and ask that you post an example, boiled down to a couple dozen lines type of real code showing where you're hung up.


        Dave