blib/lib/Class/MakeMethods/Template/StructBuiltin.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 90 CC 6 SCAR 32.6
CC
6
Cov
0%
builtin_isa
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 26.0
1 package Class::MakeMethods::Template::StructBuiltin;
2  
3 use Class::MakeMethods::Template::Generic '-isasubclass';
4  
5 $VERSION = 1.008;
6 use strict;
7 require 5.00;
8 use Carp;
9  
10 =head1 NAME
11  
12 Class::MakeMethods::Template::StructBuiltin
13  
14 =head1 SYNOPSIS
15  
16 use Class::MakeMethods::Template::StructBuiltin (
17 -TargetClass => 'MyStat',
18 builtin_isa => [
19 '-{new_function}'=>'stat',
20 qw/ dev ino mode nlink /
21 ]
22 );
23  
24  
25 =head1 DESCRIPTION
26  
27 This class generates a wrapper around some builtin function,
28 storing the results in the object and providing a by-name interface.
29  
30 Takes a (core) function name, and a arrayref of return position names
31 (we will call it pos_list). Creates:
32  
33 =over 4
34  
35 =item new
36  
37 Calls the core func with any given arguments, stores the result in the
38 instance.
39  
40 =item x
41  
42 For each member of pos_list, creates a method of the same name which
43 gets/sets the nth member of the returned list, where n is the position
44 of x in pos_list.
45  
46 =item fields
47  
48 Returns pos_list, in the given order.
49  
50 =item dump
51  
52 Returns a list item name, item value, in order.
53  
54 =back
55  
56 Example Usage:
57  
58 package Stat;
59  
60 use Class::MakeMethods::Template::StructBuiltin
61 builtin_isa => [ '-{new_function}'=>'stat', qw/ dev ino mode nlink / ],
62  
63 package main;
64  
65 my $file = "$ENV{HOME}/.template";
66 my $s = Stat->new($file);
67 print "File $file has ", $s->nlink, " links\n";
68  
69 Note that (a) the new method does not check the return value of the
70 function called (in the above example, if $file does not exist, you will
71 silently get an empty object), and (b) if you really want the above
72 example, see the core File::stat module. But you get the idea, I hope.
73  
74 =cut
75  
76 sub builtin_isa {
77 ( {
78 'template' => {
79 default => {
80 '*'=>'get_set', 'dump'=>'dump', 'fields'=>'fields', 'new'=>'new_builtin'
81 },
82 },
83 'behavior' => {
84 '-init' => sub {
85 my $m_info = $_[0];
86
87 $m_info->{class} ||= $m_info->{target_class};
88
89 my $class_info =
90 ( $Class::MakeMethods::Struct::builtin{$m_info->{class}} ||= [] );
91 if ( ! defined $m_info->{array_index} ) {
92 foreach ( 0..$#$class_info ) {
93 if ( $class_info->[$_] eq $m_info->{'name'} ) {
94 $m_info->{array_index} = $_; last }
95 }
96 if ( ! defined $m_info->{array_index} ) {
97 push @ $class_info, $m_info->{'name'};
98 $m_info->{array_index} = $#$class_info;
99 }
100 }
101
102 if (defined $m_info->{new_function} and ! ref $m_info->{new_function}) {
103 # NOTE Below comments found in original version of MethodMaker. -Simon
104 # Cuz neither \&{"CORE::$func"} or $CORE::{$func} work ... N.B. this
105 # only works for core functions that take only one arg. But I can't
106 # quite figure out how to pass in the list without it getting
107 # evaluated in a scalar context. Hmmm.
108 $m_info->{new_function} = eval "sub {
109 scalar \@_ ? CORE::$m_info->{new_function}(shift)
110 : CORE::$m_info->{new_function}
111 }";
112 }
113
114 return;
115 },
116
117 'new_builtin' => sub { my $m_info = $_[0]; sub {
118 my $class = shift;
119 my $function = $m_info->{new_function};
120 my $self = [ &$function(@_) ];
121 bless $self, $class;
122 }},
123
124 'fields' => sub { my $m_info = $_[0]; sub {
125 my $class_info =
126 ( $Class::MakeMethods::Struct::builtin{$m_info->{class}} ||= [] );
127 @$class_info;
128 }},
129 'dump' => sub { my $m_info = $_[0]; sub {
130 my $self = shift;
131 my $class_info =
132 ( $Class::MakeMethods::Struct::builtin{$m_info->{class}} ||= [] );
133 my @keys = @$class_info;
134 map ($keys[$_], $self->[$_]), 0 .. $#keys;
135 }},
136
137 'get_set' => sub { my $m_info = $_[0]; sub {
138 my $self = shift;
139 if ( @_ ) {
140 $self->[ $m_info->{array_index} ] = shift;
141 }
142 $self->[ $m_info->{array_index} ];
143 }},
144 },
145 } )
146 }
147  
148 1;