Skip to content

Instantly share code, notes, and snippets.

@motemen
Created January 30, 2012 09:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/1703474 to your computer and use it in GitHub Desktop.
Save motemen/1703474 to your computer and use it in GitHub Desktop.
UNIVERSAL::the
use strict;
use warnings;
use PadWalker qw(peek_my);
use Scalar::Util qw(blessed);
use Class::Load qw(is_class_loaded);
sub UNIVERSAL::the {
my $class = shift;
my $vars = peek_my(1);
my @the = grep { blessed $_ && is_class_loaded($class) ? $_->isa($class) : ref $_ eq $class } map { $$_ } values %$vars;
wantarray ? @the : $the[0];
}
{
package Foo;
use Mouse;
}
use Test::More tests => 8;
for my $a (Foo->new, Foo->new) {
for my $b ({ i => 0 }, { i => 1 }) {
is the Foo, $a;
is the HASH, $b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment