in reply to Re^6: Upgrade-proofing overridden subroutines
in thread Upgrade-proofing overridden subroutines

Update: Added output of sample code.

I apologize--I feel like I took your question too concretely, that I missed a modest mentoring tone in your first post, and that I provoked you to the defense of your module. You tossed out Module::Build as if it were germane when we were talking about neglected modules. That seemed red-herringish to me; did I miss a point?

If you fork privately, you have to maintain it in-house.

The wisdom I was trying to share:

Adrianh's solution, if usable, has the advantage of clarity.

For readability and lack of any downside of which I can think, my first choice for an outside patch would be to redefine the problem.

use Foo; package Foo; { no warnings "redefine"; sub foo { 'different foo' } } package resume_former_namespace; ...

I like the idea of aliasing away the problem but I can see how that could be very confusing to those year-later-and-you're-gone maintainers. It is not really possible to predict what semantics those folk will expect. Should foo or Foo::foo be overridden in below? I'd avoid that issue unless I need the flexibility.

Be well,
rir

#!/usr/bin/perl use warnings; use strict; use Foo; use Sub::Override; sub fixed_foo { "I'm overridden foo" } my $override = Sub::Override->new; $override->replace( 'Foo::foo' => \&fixed_foo ); # maybe 'foo' print "Foo::Ridden: ", Foo::foo(), $/; print "Foo::Ridden: ", Foo::bar, $/; print " ::Ridden: ", foo(), $/; $override->restore; print " ::Orig: ", foo(), $/;
File Foo.pm
package Foo; use Exporter; our @ISA = 'Exporter'; our @EXPORT = qw/ foo bar /; sub foo { "I'm foo" } sub bar { foo() . 'bar' } 1;
Output of above code overriding Foo::foo.
Foo::Ridden: I'm overridden foo Foo::Ridden: I'm overridden foobar ::Ridden: I'm foo ::Orig: I'm foo
Results of same code overriding foo.
Foo::Ridden: I'm foo Foo::Ridden: I'm foobar ::Ridden: I'm overridden foo ::Orig: I'm foo
~

Replies are listed 'Best First'.
Re^8: Upgrade-proofing overridden subroutines
by Ovid (Cardinal) on Aug 17, 2006 at 20:42 UTC

    Whoa! That's something I hadn't thought about. Thanks for bringing it to my attention. Yes, it will likely confuse people and that's probably something I should add to the docs.

    Cheers,
    Ovid

    New address of my CGI Course.

      If you find a good way to explain this behaviour to folks who don't really understand aliasing and just want to "get it to work", let me know.

      I think your solution is too cute for the task. By putting such code in your codebase you endorse it for such problems; I feel this responsibility myself and I have no visibility nor reputation as a Perl expert. Since it is an approved solution, it will get used to fix the five other problem subs in Foo that featuritis may uncover. That is the tangle of interactions I would not induce.

      The way I see to make the semantics predictable is to override both Foo::foo and foo, but I haven't thought out the timing and interactions of multiple usages.

      My redefinition solution has the same weakness and should be used thusly:

      # File: My_Foo.pm use Foo; package Foo; no warnings 'redefine'; sub Foo { "redefined Foo" } 1;
      Then make any other use of Foo illegal. I'm a fan of linting code for such things during the build process.

      Be well,
      rir