in reply to
Accessing a scalar value in another subroutine?
Here the solution from "Perl Hacks".
It's freely downloadable from O'Reilly
package Attribute::Docstring;
use strict;
use warnings;
use Scalar::Util 'blessed';
use Attribute::Handlers;
my %doc;
sub UNIVERSAL::Doc :ATTR
{
my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
return if $symbol eq 'LEXICAL';
my $name = *{$symbol}{NAME};
$doc{ $package }{$name} = $data;
}
sub UNIVERSAL::doc
{
my ($self, $name) = @_;
my $package = blessed( $self ) || $self;
return unless exists $doc{ $package }{ $name };
return $doc{ $package }{ $name };
}
1;
application
package Counter;
use strict;
use warnings;
use Attribute::Docstring;
our $counter :Doc( 'a count of all new Foo objects' );
sub new :Doc( 'the constructor for Foo' )
{
$counter++;
bless {}, shift;
}
sub get_count :Doc( 'returns the count for all foo objects' )
{
return $counter;
}
1;