Devel::Sub::Which - Name information about sub calls à la L<UNIVERSAL/can> and


Devel-Sub-Which documentation Contained in the Devel-Sub-Which distribution.

Index


Code Index:

NAME

Top

Devel::Sub::Which - Name information about sub calls à la can in UNIVERSAL and <which(1)>.

SYNOPSIS

Top

	{
		# inject 'which' into a class:
		package Foo;
		use Devel::Sub::Which qw(which);
	}

	# or into UNIVERSAL (best avoided except as a temporary measure)
	use Devel::Sub::Which qw(:universal);




	# introspect like this:
	$obj->which("foo"); # returns the name of the sub that
	                    # will implement the "foo" method

	# or like this:
	Devel::Sub::Which::which( $obj, "foo" );




	# which is equivalent to:
	my $code_ref = $obj->can("foo");
	Devel::Sub::Which::which($code_ref);

DESCRIPTION

Top

I don't like the perl debugger. I'd rather print debug statements as I go along, mostly saying "i'm going to do so and so", so I know what to look for when stuff breaks.

I also like to make extensive use of polymorphism. Due to the richness of Perl's OO, we have multiple inheritence, delegations, runtime generated classes, method calls on non predeterminate values, etc, and it often makes sense to do:

	my $method = "foo";;

	debug("i'm going to call $method on $obj. FYI, it's going to be "
		. $obj->which($method));

	$obj->$method()

In order to figure out exactly which definition of $method is going to be invoked. This helps the above debugging style by providing more deterministic reporting.

METHODS

Top

OBJ->which( METHOD )

This method determines which subroutine reference will be executed for METHOD, using UNIVERSAL::can (or any overriding implementation),

You can get this method by importing it as a function into a class, or using the :universal export.

FUNCTIONS

Top

which OBJ METHOD
which CODEREF

The first form has the same effect as OBJ->which(METHOD), and the second form just delegates to Sub::Identify

EXPORTS

Top

Nothing is exported by default.

This module uses Sub::Exporter, so exports can be renamed, etc.

:universal

This causes which to become a method in UNIVERSAL, so that you can call it on any object.

which

You can import this into a class and then use it as a method.

ref_to_name

Provided for compatibility. Just use Sub::Identify.

ACKNOWLEGEMENTS

Top

Yitzchak Scott-Thoennes provided the know-how needed to get the name of a sub reference. I've since switched to using Sub::Identify, which abstract those details away.

VERSION CONTROL

Top

This module is maintained using Darcs. You can get the latest version from http://nothingmuch.woobling.org/Devel-Sub-Which/, and use darcs send to commit changes.

AUTHOR

Top

Yuval Kogman <nothingmuch@woobling.org>

COPYRIGHT & LICENSE

Top

SEE ALSO

Top

Sub::Identify, DB, perldebug, UNIVERSAL, B


Devel-Sub-Which documentation Contained in the Devel-Sub-Which distribution.

#!/usr/bin/perl

use 5.006;

package Devel::Sub::Which;

use strict;
use warnings;

our $VERSION = "0.05";

use Sub::Identify qw(sub_fullname);
use Scalar::Util qw/reftype/;
use Carp qw/croak/;

use Sub::Exporter -setup => {
	exports => [qw(which ref_to_name)],
	collectors => {
		':universal' => sub { *UNIVERSAL::which = \&which; return 1 },
	}
};

sub which ($;$) {
	my $obj = shift;
	my $sub = shift;

	return sub_fullname($obj) if not defined $sub; # just a sub, no object

	if (ref($sub) and reftype($sub) eq 'CODE'){
		return sub_fullname($sub);
	} else {
		my $ref = $obj->can($sub);
		croak("$obj\->can($sub) did not return a code reference")
			unless ref($ref) and reftype($ref) eq 'CODE';
		return sub_fullname($ref);
	}
}

sub ref_to_name ($) {
	my $sub = shift;

	unless (ref($sub) and reftype($sub) eq 'CODE'){
		croak "$sub is not a code reference";
	}

	sub_fullname($sub);
}

__PACKAGE__

__END__