The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Object::Tap - Tap into a series of method calls to alter an object


Instead of writing -

  my $thing = My::Class->new(...);

you can instead write -

  use Object::Tap;
  my $thing = My::Class->new(...)->$_tap(sub { $_[0]->set_foo(1) });

We also alias $_ to $_[0] within the subroutine so:

  my $thing = My::Class->new(...)->$_tap(sub { $_->set_foo(1) });

also works.

To realise why this might be useful, consider instead -


where a variable is thereby not required at all.

You can also pass extra args -

  $obj->$_tap(sub { warn "Got arg: $_[1]" }, 'arg');

or use a method name instead of a sub ref -

  my $thing = My::Class->new(...)->$_tap(set_foo => 1);

For a 'real' example of how that might be used, one could create and initialize an HTML::TableExtract object in one go using -

  my $te = HTML::TableExtract->new->$_tap(parse => $html);


mst - Matt S. Trout (cpan:MSTROUT) <>


None yet. Well volunteered? :)


Copyright (c) 2014 the Object::Tap "AUTHOR" and "CONTRIBUTORS" as listed above.


This library is free software and may be distributed under the same terms as perl itself.