PrePAN

Sign in to PrePAN

UNIVERSAL::tap tap

Author
cho45@github
Date
URL
http://example.com/
Status
In Review
Good

Synopsis

use UNIVERSAL::tap;

my $base = URI->new('http://example.com/');

$mech->get_ok($base->tap(sub { $_->query_form(page => 2) }), [ 'X-Requested-With' => 'XMLHttpRequest' ]);

Description

UNIVERSAL::tap is similar to Ruby's Object#tap.

Comments

Seems handy. Is there any reason to put it in UNIVERSAL though? Why not...

use Object::tap -package => [qw/URI HTTP::Message HTTP::Headers/];

which would install the tap method into only the specified packages.

{
package MyClass;
use Object::tap;
}

would install tap into only MyClass.
Anybody who really needed it in UNIVERSAL could simply do:

use Object::tap -package => 'UNIVERSAL';
From just this description, I have no idea what it does. I would have guessed it has something to do with TAP (Test Anything Protocol), but that was clearly wrong.
$foo->tap($coderef) would set local $_=$foo then execute $coderef and then return $foo. It's a helper for chaining.
"tap" has a very specific meaning in the world of Perl, which is TAP, the Test Anything Protocol, which Perl uses for doing tests. I think you'll cause a lot of confusion with this name.

Please sign up to post a review.