File: README.md

package info (click to toggle)
libsub-override-perl 0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 116 kB
  • sloc: perl: 164; makefile: 2
file content (299 lines) | stat: -rw-r--r-- 9,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# NAME

Sub::Override - Perl extension for easily overriding subroutines

# VERSION

0.12

# SYNOPSIS

    use Sub::Override;

    sub foo { 'original sub' };
    print foo(); # prints 'original sub'

    my $override = Sub::Override->new( foo => sub { 'overridden sub' } );
    print foo(); # prints 'overridden sub'
    $override->restore;
    print foo(); # prints 'original sub'

# DESCRIPTION

## 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.

Overriding a subroutine is often done with syntax similar to the following.

    {
      local *Some::sub = sub {'some behavior'};
      # do something
    }
    # original subroutine behavior restored

This has a few problems.

    {
      local *Get::some_feild = { 'some behavior' };
      # do something
    }

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.

Further, if we're attempting to localize the effect by placing this code in a
block, the entire construct is cumbersome.

Hook::LexWrap also allows us to override sub behavior, but I can never remember
the exact syntax.

## An easier way to replace subroutines

Instead, `Sub::Override` allows the programmer to simply name the sub to
replace and to supply a sub to replace it with.

    my $override = Sub::Override->new('Some::sub', sub {'new data'});

    # which is equivalent to:
    my $override = Sub::Override->new;
    $override->replace('Some::sub', sub { 'new data' });

You can replace multiple subroutines, if needed:

    $override->replace('Some::sub1', sub { 'new data1' });
    $override->replace('Some::sub2', sub { 'new data2' });
    $override->replace('Some::sub3', sub { 'new data3' });

If replacing the subroutine succeeds, the object is returned.  This allows the
programmer to chain the calls, if this style of programming is preferred:

    $override->replace('Some::sub1', sub { 'new data1' })
             ->replace('Some::sub2', sub { 'new data2' })
             ->replace('Some::sub3', sub { 'new data3' });

If the subroutine has a prototype, the new subroutine should be declared with
same prototype as original one:

    $override->replace('Some::sub_with_proto', sub ($$) { ($_[0], $_ [1]) });

A subroutine may be replaced as many times as desired.  This is most useful
when testing how code behaves with multiple conditions.

    $override->replace('Some::thing', sub { 0 });
    is($object->foo, 'wibble', 'wibble is returned if Some::thing is false');

    $override->replace('Some::thing', sub { 1 });
    is($object->foo, 'puppies', 'puppies are returned if Some::thing is true');

## Injecting a subroutine

If you want to inject a subroutine into a package, you can use the `inject()`
method. This is identical to `replace()`, except that it requires that the
subroutine does not exist:

    $override->inject('Some::sub', sub {'new data'});

This is useful if you want to add a subroutine to a package that doesn't
already have it.

If you attempt to inject a subroutine that already exists, an exception will be
thrown.

    $override->inject('Some::sub', sub {'new data'}); # works
    $override->inject('Some::sub', sub {'new data'}); # throws an exception

Calling `restore()` or allowing the `$override` to go out of scope will
remove the injected subroutine.

    $override->inject('Some::sub', sub {'new data'});
    $override->restore('Some::sub'); # removes the injected subroutine

## Inheriting a subroutine

Similar to 'inject', 'inherit' will only allow you to create a new subroutine
on a child object that inherits the routine from the parent, and doesn't
exist in the child:

    package Parent;
    sub foo {}
    sub bar {}

    package Child;
    use parent 'Parent';
    sub foo {}

'Inherit' will allow you to set up a new 'Child::bar' subroutine since it is
inherited from Parent. Attempting to 'inherit' 'Child::foo' will result in an
exception being thrown since 'foo' already exists in Child. Similarly,
attempting to 'inherit' new subroutine 'something' in Child will also result
in an exception since it doesn't exist in Parent and won't be inherited by Child.

## 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->(@\_)':

    $override->wrap('Some::sub',
      sub {
        my ($old_sub, @args) = @_;
        return 1 if $args[0];
        return $old_sub->(@args);
      }
    );

## 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 `restore()` method:

    my $override = Sub::Override->new('Some::sub', sub {'new data'});
    # do stuff
    $override->restore;

Which is somewhat equivalent to:

    {
      my $override = Sub::Override->new('Some::sub', sub {'new data'});
      # do stuff
    }

If you have overridden more than one subroutine with an override object, you
will have to explicitly name the subroutine you wish to restore:

    $override->restore('This::sub');

Note `restore()` will always restore the original behavior of the subroutine
no matter how many times you have overridden it.

## 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.

    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

# METHODS

## new

    my $sub = Sub::Override->new;
    my $sub = Sub::Override->new($sub_name, $sub_ref);

Creates a new `Sub::Override` instance.  Optionally, you may override a
subroutine while creating a new object.

## replace

    $sub->replace($sub_name, $sub_body);

Temporarily replaces a subroutine with another subroutine.  Returns the
instance, so chaining the method is allowed:

    $sub->replace($sub_name, $sub_body)
        ->replace($another_sub, $another_body);

This method will `croak` if the subroutine to be replaced does not exist.

## override

    my $sub = Sub::Override->new;
    $sub->override($sub_name, $sub_body);

`override` is an alternate name for `replace`.  They are the same method.

## inject

    $sub->inject($sub_name, $sub_body);

Temporarily injects a subroutine into a package.  Returns the instance, so
chaining the method is allowed:

    $sub->inject($sub_name, $sub_body)
        ->inject($another_sub, $another_body);

## inherit

    $sub->inherit($sub_name, $sub_body);

Checks that the subroutine exists in a parent class, but not in the current
class, and injects it into the current class to inherit the parent's version.

## restore

    $sub->restore($sub_name);

Restores the previous behavior of the subroutine.  This will happen
automatically if the `Sub::Override` object falls out of scope.

## wrap

    $sub->wrap($sub_name, $sub_body);

Temporarily wraps a subroutine with another subroutine. The original subroutine
is passed as the first arg to the new subroutine.

# EXPORT

None by default.

# CAVEATS

If you need to override the same sub several times do not create a new
`Sub::Override` object, but instead always reuse the existing one and call
`replace` on it. Creating a new object to override the same sub will result
in weird behavior.

    # Do not do this!
    my $sub_first = Sub::Override->new( 'Foo:bar' => sub { 'first' } );
    my $sub_second = Sub::Override->new( 'Foo::bar' => sub { 'second' } );

    # Do not do this either!
    my $sub = Sub::Override->new( 'Foo::bar' => sub { 'first' } );
    $sub = Sub::Override->new( 'Foo::bar' => sub { 'second' } );

Both of those usages could result in of your subs being lost, depending
on the order in which you restore them.

Instead, call `replace` on the existing `$sub`.

    my $sub = Sub::Override->new( 'Foo::bar' => sub { 'first' } );
    $sub->replace( 'Foo::bar' => sub { 'second' } );

# BUGS

Probably.  Tell me about 'em.

# SEE ALSO

- [Hook::LexWrap](https://metacpan.org/pod/Hook%3A%3ALexWrap) -- can also override subs, but with different capabilities
- [Test::MockObject](https://metacpan.org/pod/Test%3A%3AMockObject) -- use this if you need to alter an entire class

# MAINTAINER

Robin Murray (mvsjes2 on github)

# AUTHOR

Curtis "Ovid" Poe, `<ovid [at] cpan [dot] org>`

# COPYRIGHT AND LICENSE

Copyright (C) 2004-2013 by Curtis "Ovid" Poe

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.