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

I have a function. Let's call it 'my' for instance. I want to have define another function that sits of top of the original and acts in the same way, let call it 'our' for instance.
Readers of my previod post will know where I'm going with this.

I don't want to open a debate about when 'our' should or should not be used. I just want to create a fake 'our' function that can be used in older versions perl that is actually my.

I despirately need code that looks kinda like:-
unless (exists ${our}) { ${our} = \${my}; } ## End unless
I know that code is wrong, but you get the idea. Please help. I need to patch this very large and widely used script ASAP.

Lyle

Replies are listed 'Best First'.
Re: Having one sub routine point to another
by fishbot_v2 (Chaplain) on Jul 04, 2005 at 04:13 UTC

    In the general case, core functions can be overwritten by setting a reference in their slot in *CORE::GLOBAL at compile time. Then at runtime, when functions are called, the sub ref in that slot is used in place of the CORE:: function. (You almost never really want to do this, though.)

    But, the problem here is of course that my and our aren't runtime functions. They are compile-time directives. If you are using a perl that is pre-our, and you try to map our->my (ignoring that they are very different things) you have a big problem: your fake our is called at runtime, after compile-time strict warnings are issued.

    You can create an 'our()' subroutine, but it won't solve your problem: by the time it is called, it's too late. You need to intercept the source code before compile time (with a source filter, ick) or just suck it up and replace all the ours with use vars. You want that anyway, since replacing our with my is bound to break things badly, assuming the original use of our wasn't frivolous.

      To get things straight in my head.

      At the moment my code declares some 'our' variables, some of which are in a BEGIN {} tag. It then uses VARS for each of those variables (I had to do this to stop strict from complaining) Later variables are defined with 'our' alone and strict doesn't complain (I guess it has something to do with the begin tag).

      Your telling me that if I go through my whole program and change every 'our' to 'my' and add the variable to the VARS list it'll be global even though it was declared with my? Or if it's in the VARS list do I not put a my in front?

      Sorry if I sound dense but it's 5:30am where I am and I've been working through the night (again).

      Thanks
      Lyle
      (Suspecting he may have to give up and update all his scripts)

        If it is in the use vars list, then do not put a my in front. You only need one or the other. You don't need to declare it other than in the use vars... just use it.

        When it is no longer 5:30 am, read up on perldoc -f my and perldoc -f our and pick which one you really want in each case.

Re: Having one sub routine point to another
by Anonymous Monk on Jul 04, 2005 at 03:18 UTC
    it looks like you need the vars pragma:
    use vars '$foo', '@bar', '%baz'; $foo = "hate globals";
Re: Having one sub routine point to another
by ysth (Canon) on Jul 04, 2005 at 05:17 UTC
    I don't want to open a debate about when 'our' should or should not be used.
    If wishes were horses, beggars would ride...

    I guess I'm pretty baffled by this request...our does not do what my does in general, and for pre-5.6.0, the appropriate replacement for our is use vars. Why do you not want to just use vars for every variable you are using our for now? Possibly combined with a dummy our subroutine so you don't have to remove them (untested):

    BEGIN { *our = sub { wantarray ? @_ : $_[$#_] } if $] < 5.006 }
    (May not parse exactly as our() does in every circumstance.)