| 1 | =head1 NAME | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | Scalar::Footnote - Attach hidden scalars to references | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | use Data::Dumper; | ||||||||||||||||||||
| 8 | use Scalar::Footnote; | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | my $obj = Foo->new; | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # attach invisible footnote to $obj: | ||||||||||||||||||||
| 13 | $obj->Scalar::Footnote::set( my_key => 'my footnote' ); | ||||||||||||||||||||
| 14 | print Dumper( $obj ); | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # get it back: | ||||||||||||||||||||
| 17 | my $note = $obj->Scalar::Footnote::get( 'my_key' ); | ||||||||||||||||||||
| 18 | print "footnote: $note\n"; | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # remove it: | ||||||||||||||||||||
| 21 | my $note = $obj->Scalar::Footnote::remove( 'my_key' ); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | package Scalar::Footnote; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 1 | ▶ | use Carp qw( carp croak confess ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 1 | ▶ | use Scalar::Footnote::Functions qw( get_footnote set_footnote remove_footnote ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | our $VERSION = '0.99_02'; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 36 | 3 | my $class = shift; | |||||||||||||||||||
| 37 | 3 | my $self = bless {}, $class; | |||||||||||||||||||
| 38 | 3 | $self->init( @_ ); | |||||||||||||||||||
| 39 | 3 | return $self; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 3 | ▶ | sub init { return $_[0]; } | ||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 4 | ▶ | sub set_value_for { | ||||||||||||||||||
|
Subroutine
set_value_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 45 | 4 | my $self = shift; | |||||||||||||||||||
| 46 | 4 | my $key = shift; | |||||||||||||||||||
| 47 | 4 | $self->{"$key"} = shift; | |||||||||||||||||||
| 48 | 4 | return $self; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 5 | ▶ | sub get_value_for { | ||||||||||||||||||
|
Subroutine
get_value_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 52 | 5 | my $self = shift; | |||||||||||||||||||
| 53 | 5 | my $key = shift; | |||||||||||||||||||
| 54 | 5 | $self->{"$key"}; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 2 | ▶ | sub remove_value_for { | ||||||||||||||||||
|
Subroutine
remove_value_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 58 | 2 | my $self = shift; | |||||||||||||||||||
| 59 | 2 | my $key = shift; | |||||||||||||||||||
| 60 | 2 | delete $self->{"$key"}; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 2 | ▶ | sub attach_to { | ||||||||||||||||||
|
Subroutine
attach_to: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 64 | 2 | my $self = shift; | |||||||||||||||||||
| 65 | 2 | my $thing = shift; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2 | ▶ | croak( "can't set footnote object: '$thing' is not a ref!" ) unless ref( $thing ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | set_footnote( $thing => $self ); | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 2 | return $self; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 75 | # Class Methods | ||||||||||||||||||||
| 76 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 2 | ▶ | sub attach_new_footnote_object_to { | ||||||||||||||||||
|
Subroutine
attach_new_footnote_object_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 79 | 2 | my $class = shift; | |||||||||||||||||||
| 80 | 2 | my $thing = shift; | |||||||||||||||||||
| 81 | 2 | return Scalar::Footnote->new->attach_to( $thing ); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 6 | ▶ | sub get_footnote_object_for { | ||||||||||||||||||
|
Subroutine
get_footnote_object_for: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 85 | 6 | my $class = shift; | |||||||||||||||||||
| 86 | 6 | my $thing = shift; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 6 | ▶ | confess( "can't get footnote object: '$thing' is not a ref!" ) unless ref( $thing ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 6 | my $note = get_footnote( $thing ); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 6 | ▶ | return unless defined( $note ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 4 | ▶ | croak( "footnote object for '$thing' is not a Scalar::Footnote object (it's '$note')" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | unless UNIVERSAL::isa( $note, 'Scalar::Footnote' ); | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 4 | return $note; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 3 | ▶ | sub get_or_create_footnote_object_for { | ||||||||||||||||||
|
Subroutine
get_or_create_footnote_object_for: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 101 | 3 | my $class = shift; | |||||||||||||||||||
| 102 | 3 | my $thing = shift; | |||||||||||||||||||
| 103 | 3 | ▶ | return $class->get_footnote_object_for( $thing ) || | ||||||||||||||||||
|
Condition66%2/3
$class->get_footnote_object_for($thing) || $class->attach_new_footnote_object_to($thing)
MC/DC0%0/2
$class->get_footnote_object_for($thing) || $class->attach_new_footnote_object_to($thing)
$class->get_footnote_object_for($thing)
$class->attach_new_footnote_object_to($thing)
Truth table66%2/3
A || B
A= $class->get_footnote_object_for($thing)B= $class->attach_new_footnote_object_to($thing)
| |||||||||||||||||||||
| 104 | $class->attach_new_footnote_object_to( $thing ); | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 108 | # Pseudo Class Methods | ||||||||||||||||||||
| 109 | #------------------------------------------------------------------------------- | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 3 | ▶ | sub set { | ||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 112 | 3 | my $thing = shift; | |||||||||||||||||||
| 113 | 3 | my $key = shift; | |||||||||||||||||||
| 114 | 3 | my $value = shift; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 3 | ▶ | croak( "can't set footnote for '$thing' - it's not a ref!" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | unless ref( $thing ); | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 3 | my $note = Scalar::Footnote->get_or_create_footnote_object_for( $thing ); | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 3 | $note->set_value_for( $key => $value ); | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 3 | return $thing; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 2 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 127 | 2 | my $thing = shift; | |||||||||||||||||||
| 128 | 2 | my $key = shift; | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 2 | ▶ | croak( "can't get footnote for '$thing' - it's not a ref!" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | unless ref( $thing ); | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 2 | ▶ | my $note = Scalar::Footnote->get_footnote_object_for( $thing ) || return; | ||||||||||||||||||
|
Condition50%1/2
'Scalar::Footnote'->get_footnote_object_for($thing) || (return)
MC/DC0%0/1
'Scalar::Footnote'->get_footnote_object_for($thing) || (return)
'Scalar::Footnote'->get_footnote_object_for($thing)
Truth table50%1/2
A
A= 'Scalar::Footnote'->get_footnote_object_for($thing)
| |||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 2 | return $note->get_value_for( $key ); | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 1 | ▶ | sub remove { | ||||||||||||||||||
|
Subroutine
remove: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 139 | 1 | my $thing = shift; | |||||||||||||||||||
| 140 | 1 | my $key = shift; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 1 | ▶ | croak( "can't remove footnote for '$thing' - it's not a ref!" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | unless ref( $thing ); | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 1 | ▶ | my $note = Scalar::Footnote->get_footnote_object_for( $thing ) || return; | ||||||||||||||||||
|
Condition50%1/2
'Scalar::Footnote'->get_footnote_object_for($thing) || (return)
MC/DC0%0/1
'Scalar::Footnote'->get_footnote_object_for($thing) || (return)
'Scalar::Footnote'->get_footnote_object_for($thing)
Truth table50%1/2
A
A= 'Scalar::Footnote'->get_footnote_object_for($thing)
| |||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 1 | return $note->remove_value_for( $key ); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 1 | ▶ | sub remove_all { | ||||||||||||||||||
|
Subroutine
remove_all: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||
| 151 | 1 | my $thing = shift; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 1 | ▶ | croak( "can't remove all footnotes for '$thing' - it's not a ref!" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 154 | unless ref( $thing ); | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 1 | my $note = remove_footnote( $thing ); | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 1 | ▶ | return unless defined( $note ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 1 | ▶ | carp( "footnote for '$thing' is not a Scalar::Footnote object (it's '$note')" ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | unless UNIVERSAL::isa( $note, 'Scalar::Footnote' ); | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 1 | return $note; | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 1 | 1; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||