in reply to Calling functions from different namespaces

If I were to guess, I'd say you were trying to override "s2" in some existing package, without modifying the module. If that's indeed what you are trying to do, then the best you could do would be to replace "s2" entirely:

perl -wle 'use TT; sub TT::s2 { print "NO S2!"; } s1();'

To avoid the warning:

perl -wle 'use TT; BEGIN { undef *TT::s2; } sub TT::s2 { print "NO S2! +"; } s1();'

Replies are listed 'Best First'.
Re^2: Calling functions from different namespaces
by Anonymous Monk on Jan 18, 2006 at 08:04 UTC
    Couldn't I also use a coderef in the arguments to s1? For example:
    s1(\&s2)
    Someone suggested this approach to me, but I don't know how to flesh the rest of it out... I would have to alter s1 to read in the coderef. How could I do that? thanks
      sub s1 { my $sub=shift; # get it! # you may want to add checks => perldoc -f ref. print "s1"; $sub->(); # execute it! }

      Indeed they gave you a good suggestion: this is the best way to do what you say you want to do.

        You could even make it default to the original s2 if the argument isn't supplied:
        sub s1 { my $sub = shift || \&s2; print "s1"; $sub->(); }
        If I do it that way, how do i supply arguments to $sub in s1? For example the s2 subroutine takes in 2 parameters. How do I put those parameters into the $sub once I am in s1?

        I was thinking of something like:

        $sub->($value1, $value2);
        But that doesn't work...