This is what the side_effect() functionality does... allows you to do stuff (eg: modify an object) when there's no need for a return. Here's an example:
use warnings;
use strict;
package Thing; {
sub new {
return bless {}, shift;
}
sub modify {
my ($self) = @_;
$self->{modified} = 'modified by original sub';
}
}
package main;
use Data::Dumper;
use Mock::Sub;
use Test::More;
my $m = Mock::Sub->new;
my $thing = Thing->new;
my $modify_sub = $m->mock('Thing::modify');
$modify_sub->side_effect(
sub {
my $obj = shift;
$obj->{modified} = 'modified by mocked sub';
}
);
print "before mocked sub called...\n\n";
print Dumper $thing;
$thing->modify;
print "\n\nafter mocked sub called...\n\n";
print Dumper $thing;
print "\n\n";
is defined $thing->{modified}, 1, "obj was modified ok";
like $thing->{modified}, qr/mocked sub/, "obj was changed by mock";
is $modify_sub->called, 1, "mocked sub called ok";
done_testing();
Output:
before mocked sub called...
$VAR1 = bless( {}, 'Thing' );
after mocked sub called...
$VAR1 = bless( {
'modified' => 'modified by mocked sub'
}, 'Thing' );
ok 1 - obj was modified ok
ok 2 - obj was changed by mock
ok 3 - mocked sub called ok
1..3
So, there's no return anymore. The side_effect() code reference will get all parameters passed in as they were sent in to the real sub call (in this case, $self, as it's the only param on the method call. We then have the side effect add a new hash key to itself and assign it a value. After side effect is complete, the main object is updated just like the original function would have done, without having to call the real function.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|