Attribute::SubName - Naming anonymous subroutines via attributes


Attribute-SubName documentation Contained in the Attribute-SubName distribution.

Index


Code Index:

NAME

Top

Attribute::SubName - Naming anonymous subroutines via attributes

VERSION

Top

version 1.101420

SYNOPSIS

Top

    use Attribute::SubName;
    my $coderef = sub :Name(foo) { print "got: $_\n"; };
    print foo("hi");

DESCRIPTION

Top

This module provides an attribute :Name that you can use on anonymous subroutines to give them a name. This is useful as they will then show up with that name in stack traces (cf. Carp). The naming is done with Sub::Name. Additionally, the attribute also installs the newly named subroutine in the proper glob slot so you can refer to it by name.

INSTALLATION

Top

See perlmodinstall for information and options on installing Perl modules.

BUGS AND LIMITATIONS

Top

No bugs have been reported.

Please report any bugs or feature requests through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=Attribute-SubName.

AVAILABILITY

Top

The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see http://search.cpan.org/dist/Attribute-SubName/.

The development version lives at http://github.com/hanekomu/Attribute-SubName/. Instead of sending patches, please fork this project using the standard git and github infrastructure.

AUTHOR

Top

  Marcel Gruenauer <marcel@cpan.org>

COPYRIGHT AND LICENSE

Top


Attribute-SubName documentation Contained in the Attribute-SubName distribution.

use 5.006;
use strict;
use warnings;

package Attribute::SubName;
BEGIN {
  $Attribute::SubName::VERSION = '1.101420';
}
# ABSTRACT: Naming anonymous subroutines via attributes

use Sub::Name;
use parent 'Attribute::Handlers';

sub UNIVERSAL::Name : ATTR(CODE) {
    my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
    $data = [$data] unless ref $data eq 'ARRAY';
    for my $item (@$data) {
        my $name = "${package}::${item}";
        subname $name => $referent;
        no strict 'refs';
        *{$name} = $referent;
    }
}
1;


__END__