.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Sub::Override 3" .TH Sub::Override 3 2023-12-24 "perl v5.38.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Sub::Override \- Perl extension for easily overriding subroutines .SH VERSION .IX Header "VERSION" 0.10 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Sub::Override; \& \& sub foo { \*(Aqoriginal sub\*(Aq }; \& print foo(); # prints \*(Aqoriginal sub\*(Aq \& \& my $override = Sub::Override\->new( foo => sub { \*(Aqoverridden sub\*(Aq } ); \& print foo(); # prints \*(Aqoverridden sub\*(Aq \& $override\->restore; \& print foo(); # prints \*(Aqoriginal sub\*(Aq .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" .SS "The Problem" .IX Subsection "The Problem" Sometimes subroutines need to be overridden. In fact, your author does this frequently for tests. Particularly when testing, using a Mock Object can be overkill when all you want to do is override one tiny, little function. .PP Overriding a subroutine is often done with syntax similar to the following. .PP .Vb 5 \& { \& local *Some::sub = sub {\*(Aqsome behavior\*(Aq}; \& # do something \& } \& # original subroutine behavior restored .Ve .PP This has a few problems. .PP .Vb 4 \& { \& local *Get::some_feild = { \*(Aqsome behavior\*(Aq }; \& # do something \& } .Ve .PP In the above example, not only have we probably misspelled the subroutine name, but even if there had been a subroutine with that name, we haven't overridden it. These two bugs can be subtle to detect. .PP Further, if we're attempting to localize the effect by placing this code in a block, the entire construct is cumbersome. .PP Hook::LexWrap also allows us to override sub behavior, but I can never remember the exact syntax. .SS "An easier way to replace subroutines" .IX Subsection "An easier way to replace subroutines" Instead, \f(CW\*(C`Sub::Override\*(C'\fR allows the programmer to simply name the sub to replace and to supply a sub to replace it with. .PP .Vb 1 \& my $override = Sub::Override\->new(\*(AqSome::sub\*(Aq, sub {\*(Aqnew data\*(Aq}); \& \& # which is equivalent to: \& my $override = Sub::Override\->new; \& $override\->replace(\*(AqSome::sub\*(Aq, sub { \*(Aqnew data\*(Aq }); .Ve .PP You can replace multiple subroutines, if needed: .PP .Vb 3 \& $override\->replace(\*(AqSome::sub1\*(Aq, sub { \*(Aqnew data1\*(Aq }); \& $override\->replace(\*(AqSome::sub2\*(Aq, sub { \*(Aqnew data2\*(Aq }); \& $override\->replace(\*(AqSome::sub3\*(Aq, sub { \*(Aqnew data3\*(Aq }); .Ve .PP If replacing the subroutine succeeds, the object is returned. This allows the programmer to chain the calls, if this style of programming is preferred: .PP .Vb 3 \& $override\->replace(\*(AqSome::sub1\*(Aq, sub { \*(Aqnew data1\*(Aq }) \& \->replace(\*(AqSome::sub2\*(Aq, sub { \*(Aqnew data2\*(Aq }) \& \->replace(\*(AqSome::sub3\*(Aq, sub { \*(Aqnew data3\*(Aq }); .Ve .PP If the subroutine has a prototype, the new subroutine should be declared with same prototype as original one: .PP .Vb 1 \& $override\->replace(\*(AqSome::sub_with_proto\*(Aq, sub ($$) { ($_[0], $_ [1]) }); .Ve .PP A subroutine may be replaced as many times as desired. This is most useful when testing how code behaves with multiple conditions. .PP .Vb 2 \& $override\->replace(\*(AqSome::thing\*(Aq, sub { 0 }); \& is($object\->foo, \*(Aqwibble\*(Aq, \*(Aqwibble is returned if Some::thing is false\*(Aq); \& \& $override\->replace(\*(AqSome::thing\*(Aq, sub { 1 }); \& is($object\->foo, \*(Aqpuppies\*(Aq, \*(Aqpuppies are returned if Some::thing is true\*(Aq); .Ve .SS "Wrapping a subroutine" .IX Subsection "Wrapping a subroutine" There may be times when you want to 'conditionally' replace a subroutine \- for example, to override the original subroutine only if certain args are passed. For this you can specify 'wrap' instead of 'replace'. Wrap is identical to replace, except the original subroutine is passed as the first arg to your new subroutine. You can call the original sub via 'shift\->(@_)': .PP .Vb 7 \& $override\->wrap(\*(AqSome::sub\*(Aq, \& sub { \& my ($old_sub, @args) = @_; \& return 1 if $args[0]; \& return $old_sub\->(@args); \& } \& ); .Ve .SS "Restoring subroutines" .IX Subsection "Restoring subroutines" If the object falls out of scope, the original subs are restored. However, if you need to restore a subroutine early, just use the \f(CWrestore()\fR method: .PP .Vb 3 \& my $override = Sub::Override\->new(\*(AqSome::sub\*(Aq, sub {\*(Aqnew data\*(Aq}); \& # do stuff \& $override\->restore; .Ve .PP Which is somewhat equivalent to: .PP .Vb 4 \& { \& my $override = Sub::Override\->new(\*(AqSome::sub\*(Aq, sub {\*(Aqnew data\*(Aq}); \& # do stuff \& } .Ve .PP If you have overridden more than one subroutine with an override object, you will have to explicitly name the subroutine you wish to restore: .PP .Vb 1 \& $override\->restore(\*(AqThis::sub\*(Aq); .Ve .PP Note \f(CWrestore()\fR will always restore the original behavior of the subroutine no matter how many times you have overridden it. .SS "Which package is the subroutine in?" .IX Subsection "Which package is the subroutine in?" Ordinarily, you want to fully qualify the subroutine by including the package name. However, failure to fully qualify the subroutine name will assume the current package. .PP .Vb 7 \& package Foo; \& use Sub::Override; \& sub foo { 23 }; \& my $override = Sub::Override\->new( foo => sub { 42 } ); # assumes Foo::foo \& print foo(); # prints 42 \& $override\->restore; \& print foo(); # prints 23 .Ve .SH METHODS .IX Header "METHODS" .SS new .IX Subsection "new" .Vb 2 \& my $sub = Sub::Override\->new; \& my $sub = Sub::Override\->new($sub_name, $sub_ref); .Ve .PP Creates a new \f(CW\*(C`Sub::Override\*(C'\fR instance. Optionally, you may override a subroutine while creating a new object. .SS replace .IX Subsection "replace" .Vb 1 \& $sub\->replace($sub_name, $sub_body); .Ve .PP Temporarily replaces a subroutine with another subroutine. Returns the instance, so chaining the method is allowed: .PP .Vb 2 \& $sub\->replace($sub_name, $sub_body) \& \->replace($another_sub, $another_body); .Ve .PP This method will \f(CW\*(C`croak\*(C'\fR if the subroutine to be replaced does not exist. .SS override .IX Subsection "override" .Vb 2 \& my $sub = Sub::Override\->new; \& $sub\->override($sub_name, $sub_body); .Ve .PP \&\f(CW\*(C`override\*(C'\fR is an alternate name for \f(CW\*(C`replace\*(C'\fR. They are the same method. .SS restore .IX Subsection "restore" .Vb 1 \& $sub\->restore($sub_name); .Ve .PP Restores the previous behavior of the subroutine. This will happen automatically if the \f(CW\*(C`Sub::Override\*(C'\fR object falls out of scope. .SH EXPORT .IX Header "EXPORT" None by default. .SH CAVEATS .IX Header "CAVEATS" If you need to override the same sub several times do not create a new \&\f(CW\*(C`Sub::Override\*(C'\fR object, but instead always reuse the existing one and call \&\f(CW\*(C`replace\*(C'\fR on it. Creating a new object to override the same sub will result in weird behavior. .PP .Vb 3 \& # Do not do this! \& my $sub_first = Sub::Override\->new( \*(AqFoo:bar\*(Aq => sub { \*(Aqfirst\*(Aq } ); \& my $sub_second = Sub::Override\->new( \*(AqFoo::bar\*(Aq => sub { \*(Aqsecond\*(Aq } ); \& \& # Do not do this either! \& my $sub = Sub::Override\->new( \*(AqFoo::bar\*(Aq => sub { \*(Aqfirst\*(Aq } ); \& $sub = Sub::Override\->new( \*(AqFoo::bar\*(Aq => sub { \*(Aqsecond\*(Aq } ); .Ve .PP Both of those usages could result in of your subs being lost, depending on the order in which you restore them. .PP Instead, call \f(CW\*(C`replace\*(C'\fR on the existing \f(CW$sub\fR. .PP .Vb 2 \& my $sub = Sub::Override\->new( \*(AqFoo::bar\*(Aq => sub { \*(Aqfirst\*(Aq } ); \& $sub\->replace( \*(AqFoo::bar\*(Aq => sub { \*(Aqsecond\*(Aq } ); .Ve .SH BUGS .IX Header "BUGS" Probably. Tell me about 'em. .SH "SEE ALSO" .IX Header "SEE ALSO" .IP \(bu 4 Hook::LexWrap \-\- can also override subs, but with different capabilities .IP \(bu 4 Test::MockObject \-\- use this if you need to alter an entire class .SH AUTHOR .IX Header "AUTHOR" Curtis "Ovid" Poe, \f(CW\*(C`\*(C'\fR .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2004\-2013 by Curtis "Ovid" Poe .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.2 or, at your option, any later version of Perl 5 you may have available.