http://qs321.pair.com?node_id=11120163


in reply to Re: Use cases for 'sub Pckg::func { }' ?
in thread Use cases for 'sub Pckg::func { }' ?

jo37 you are guilty of monkey patching eueueueueueueu, sirens howling, you find yourself in a Cave. It's the cellar where the King keeps his barrels of wine. To the northeast there is the red door. Below there is a trap door. Oooopppss sorry, that was the punishment for acting over a distance. Let's go again monkey-patchers. Resume... You are in the trolls cave. To the north there is the heavy rock door. Thorin enters. He is laughing...

bw, bliako

Replies are listed 'Best First'.
Re^3: Use cases for 'sub Pckg::func { }' ?
by Tux (Canon) on Aug 01, 2020 at 09:14 UTC

    I find a few reasons to not oppose to this (considered evil) path:

    1. Using a module that is not maintained and nobody took over, several patches were proposed but nobody uploaded a fixed version.
      Of course you can apply the patches locally before you install it, but that is no guarantee it is also fixed on other systems.
      It is way more reliable to overrule the faulty sub or make a new one foced into the faulty package thereby making sure it works.
    2. The package owner/maint has explicitely said *not* to add the requested/required functionality (they might even have documented so), so adding the desired method by forcing it into the package is relatively safe. (problematic would only be a new maintainer who disagrees with the refusal and putting it in later anyway)
    3. When working in supporting a module that is evolving, adding methods that are not yet implemented or have been removed, all guarded by version checks is a very fine way to make your module work for the end-users regardless of the changes in the underlying module(s). I do this very regularly. (example)
    4. Overruling CORE::GLOBAL functions in test files to provide stubs.

    Enjoy, Have FUN! H.Merijn
Re^3: Use cases for 'sub Pckg::func { }' ?
by jo37 (Deacon) on Aug 01, 2020 at 07:02 UTC

    I confess.

    But seriously, why would I be punished for such:

    #!/usr/bin/perl use strict; use warnings; use LinkedList::Single; sub format_node_data { my ($id, $name) = @{$_[0]}; "[id:$id, name:$name]"; } sub LinkedList::Single::print { my $list = (shift)->clone; for ($list->head; $list->has_next; $list->next) { print format_node_data($list->node_data), " -> "; } print format_node_data($list->node_data), "\n"; } my $list = LinkedList::Single->new([1, 'foo'], [2, 'bar'], [3, 'baz']) +; $list->print; __DATA__ [id:1, name:foo] -> [id:2, name:bar] -> [id:3, name:baz]

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$