| 1 | package Test::Run::Sprintf::Named::FromAccessors; | ||||||||||
| 2 | |||||||||||
| 3 | 24 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 24 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Test::Run::Sprintf::Named::FromAccessors - named sprintf according to the | ||||||||||
| 9 | values of accessors. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||
| 12 | |||||||||||
| 13 | my $formatter = | ||||||||||
| 14 | Test::Run::Sprintf::Named::FromAccessors->new( | ||||||||||
| 15 | { | ||||||||||
| 16 | fmt => "Hello %(name)s! Today you are %(age)d years old.", | ||||||||||
| 17 | ) | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | my $person1 = Person->new(name => "Larry", age => 24); | ||||||||||
| 21 | |||||||||||
| 22 | my $msg1 = $formatter->format({args => { obj => $person1 }}); | ||||||||||
| 23 | |||||||||||
| 24 | my $person2 = Person->new(name => "Rachel", age => 30); | ||||||||||
| 25 | |||||||||||
| 26 | my $msg2 = $formatter->format({args => { obj => $person2 }}); | ||||||||||
| 27 | |||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||
| 29 | |||||||||||
| 30 | This module is a sub-class of L<Text::Sprintf::Named> where the variables | ||||||||||
| 31 | inside the sprintf fields (e.g: C<%(varname)s>) are read from the accessors | ||||||||||
| 32 | (or any other function) of the current object. | ||||||||||
| 33 | |||||||||||
| 34 | =head1 METHODS | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | 24 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | 24 | extends('Text::Sprintf::Named'); | |||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | =head2 $formatter->calc_param() | ||||||||||
| 44 | |||||||||||
| 45 | Over-riding the behavior of the equivalent one in Text::Sprintf::Named. | ||||||||||
| 46 | |||||||||||
| 47 | =cut | ||||||||||
| 48 | |||||||||||
| 49 | sub calc_param | ||||||||||
| 50 | 219 | ▶ | { | ||||||||
|
Subroutine
calc_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 51 | 219 | my ($self, $args) = @_; | |||||||||
| 52 | |||||||||||
| 53 | 219 | my $method = $args->{name}; | |||||||||
| 54 | |||||||||||
| 55 | 219 | return $args->{named_params}->{obj}->$method(); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | =head2 $formatter->obj_format($object, \%args) | ||||||||||
| 59 | |||||||||||
| 60 | Formats based on the accessors of the object $object. I don't think %args | ||||||||||
| 61 | is used in any way. | ||||||||||
| 62 | |||||||||||
| 63 | =cut | ||||||||||
| 64 | |||||||||||
| 65 | sub obj_format | ||||||||||
| 66 | 14 | ▶ | { | ||||||||
|
Subroutine
obj_format: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 67 | 14 | my ($self, $obj, $other_args) = @_; | |||||||||
| 68 | |||||||||||
| 69 | 14 | ▶ | if (!$other_args) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | { | ||||||||||
| 71 | 14 | $other_args = {}; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 14 | return $self->format({args => {obj => $obj, %$other_args}}); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 24 | 1; | |||||||||
| 78 | |||||||||||
| 79 | __END__ | ||||||||||