File: Template.pm

package info (click to toggle)
libclass-makemethods-perl 1.01-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,864 kB
  • ctags: 516
  • sloc: perl: 10,495; makefile: 2
file content (1255 lines) | stat: -rw-r--r-- 39,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
package Class::MakeMethods::Template;

use strict;
use Carp;

use Class::MakeMethods '-isasubclass';

use vars qw( $VERSION );
$VERSION = 1.008;

sub _diagnostic { &Class::MakeMethods::_diagnostic }

########################################################################
### TEMPLATE LOOKUP AND CACHING: named_method(), _definition()
########################################################################

use vars qw( %TemplateCache );

# @results = $class->named_method( $name, @arguments );
sub named_method {
  my $class = shift;
  my $name = shift;
  
  # Support direct access to cached Template information
  if (exists $TemplateCache{"$class\::$name"}) {
    return $TemplateCache{"$class\::$name"};
  }
  
  my @results = $class->$name( @_ );
  
  if ( scalar @results == 1 and ref $results[0] eq 'HASH' ) {
    # If this is a hash-definition format, cache the results for speed.
    my $def = $results[0];
    $TemplateCache{"$class\::$name"} = $def;
    _expand_definition($class, $name, $def);
    return $def;
  }
  
  return wantarray ? @results : $results[0];
}

# $mm_def = _definition( $class, $target );
sub _definition {
  my ($class, $target) = @_;
  
  while ( ! ref $target ) {
    $target =~ s/\s.*//;
    
    # If method name contains a colon or double colon, call the method on the
    # indicated class.
    my $call_class = ( ( $target =~ s/^(.*)\:{1,2}// ) 
      ? Class::MakeMethods::_find_subclass($class, $1) : $class );
    $target = $call_class->named_method( $target );
  }
  _diagnostic('mmdef_not_interpretable', $target) 
	unless ( ref($target) eq 'HASH' or ref($target) eq __PACKAGE__ );
  
  return $target;
}

########################################################################
### TEMPLATE INTERNALS: _expand_definition()
########################################################################

sub _expand_definition {
  my ($class, $name, $mm_def) = @_;
  
  return $mm_def if $mm_def->{'-parsed'};
  
  $mm_def->{'template_class'} = $class;
  $mm_def->{'template_name'} = $name;
  
  # Allow definitions to import values from each other.
  my $importer;
  foreach $importer ( qw( interface params behavior code_expr modifier ) ) {
    my $rules = $mm_def->{$importer}->{'-import'} || $mm_def->{'-import'};
    my @rules = ( ref $rules eq 'HASH' ? %$rules : ref $rules eq 'ARRAY' ? @$rules : () );
    unshift @rules, '::' . $class . ':generic' => '*' if $class->can('generic');
    while ( 
      my ($source, $names) = splice @rules, 0, 2
    ) {
      my $mmi = _definition($class, $source);
      foreach ( ( $names eq '*' ) ? keys %{ $mmi->{$importer} } 
			: ( ref $names ) ? @{ $names } : ( $names ) ) {
	my $current = $mm_def->{$importer}{$_};
	my $import = $mmi->{$importer}{$_};
	if ( ! $current ) {
	  $mm_def->{$importer}{$_} = $import;
	} elsif ( ref($current) eq 'ARRAY' ) {
	  my @imports = ref($import) ? @$import : $import;
	  foreach my $imp ( @imports ) {
	    push @$current, $imp unless ( grep { $_ eq $imp } @$current );
	  }
	}
      }
    }
    delete $mm_def->{$importer}->{'-import'};
  }
  delete $mm_def->{'-import'};
  
  _describe_definition( $mm_def ) if $Class::MakeMethods::CONTEXT{Debug};

  
  $mm_def->{'-parsed'} = "$_[1]";
  
  bless $mm_def, __PACKAGE__;
}

sub _describe_definition {
  my $mm_def = shift;
  
  my $def_type = "$mm_def->{template_class}:$mm_def->{template_name}";
  warn "----\nMethods info for $def_type:\n";
  if ( $mm_def->{interface} ) {
    warn join '', "Templates: \n", map {
	"  $_: " . _describe_value($mm_def->{interface}{$_}) . "\n"
      } keys %{$mm_def->{interface}};
  }
  if ( $mm_def->{modifier} ) {
    warn join '', "Modifiers: \n", map {
	"  $_: " . _describe_value($mm_def->{modifier}{$_}) . "\n"
      } keys %{$mm_def->{modifier}};
  }
}

sub _describe_value {
  my $value = $_[0];
  ref($value) eq 'ARRAY' ? join(', ', @$value) :
  ref($value) eq 'HASH'  ? join(', ', %$value) : 
				      "$value";
}

########################################################################
### METHOD GENERATION: make_methods()
########################################################################

sub make_methods {
  my $mm_def = shift;
  
  return unless ( scalar @_ );
  
  # Select default interface and initial method parameters
  my $defaults = { %{ ( $mm_def->{'params'} ||= {} ) } };
  $defaults->{'interface'} ||= $mm_def->{'interface'}{'-default'} || 'default';
  $defaults->{'target_class'} = $mm_def->_context('TargetClass');
  $defaults->{'template_class'} = $mm_def->{'template_class'};
  $defaults->{'template_name'} = $mm_def->{'template_name'};
  
  my %interface_cache;
  
  # Our return value is the accumulated list of method-name => method-sub pairs
  my @methods; 

  while (scalar @_) {

    ### PARSING ### Requires: $mm_def, $defaults, @_
    
    my $m_name = shift @_;
    _diagnostic('make_empty') unless ( defined $m_name and length $m_name );
    
    # Normalize: If we've got an array of names, replace it with those names 
    if ( ref $m_name eq 'ARRAY' ) {
      my @items = @{ $m_name };
      # If array is followed by a params hash, each one gets the same params
      if ( scalar @_ and ref $_[0] eq 'HASH' and ! exists $_[0]->{'name'} ) {
	my $params = shift;
	@items = map { $_, $params } @items
      }
      unshift @_, @items;
      next;
    }
    
    # Parse interfaces, modifiers and parameters
    if ( $m_name =~ s/^-// ) {
      if (  $m_name !~ s/^-// ) {
	# -param => value
	$defaults->{$m_name} = shift @_; 
      } else {
	if ( $m_name eq '' ) {
	  # '--' => { param => value ... }
	  %$defaults = ( %$defaults, %{ shift @_ } );
		
	} elsif ( exists $mm_def->{'interface'}{$m_name} ) {
	  # --interface
	  $defaults->{'interface'} = $m_name;
	
	} elsif ( exists $mm_def->{'modifier'}{$m_name} ) {
	  # --modifier
	  $defaults->{'modifier'} .= 
			    ( $defaults->{'modifier'} ? ' ' : '' ) . "-$m_name";
	
	} elsif ( exists $mm_def->{'behavior'}{$m_name} ) {
	  # --behavior as shortcut for single-method interface
	  $defaults->{'interface'} = $m_name;
	
	} else {
	  _diagnostic('make_bad_modifier', $mm_def->{'name'}, "--$m_name");
	}
      }
      next;
    }
    
    # Make a new meta-method hash
    my $m_info;
    
    # Parse string, string-then-hash, and hash-only meta-method parameters
    if ( ! ref $m_name ) {
      if ( scalar @_ and ref $_[0] eq 'HASH' and ! exists $_[0]->{'name'} ) {
	%$m_info = ( 'name' => $m_name, %{ shift @_ } );
      } else {
	$m_info = { 'name' => $m_name };
      }
    
    } elsif ( ref $m_name eq 'HASH' ) {
      unless ( exists $m_name->{'name'} and length $m_name->{'name'} ) {
	_diagnostic('make_noname');
      }
      $m_info = { %$m_name };
    
    } else {
      _diagnostic('make_unsupported', $m_name);
    }
    _diagnostic('debug_declaration', join(', ', map { defined $_ ? $_ : '(undef)' } %$m_info) );

    ### INITIALIZATION ### Requires: $mm_def, $defaults, $m_info
    
    my $interface = (
      $interface_cache{ $m_info->{'interface'} || $defaults->{'interface'} } 
	||= _interpret_interface( $mm_def, $m_info->{'interface'} || $defaults->{'interface'} )
    );
    %$m_info = ( 
      %$defaults, 
      ( $interface->{-params} ? %{$interface->{-params}} : () ),
      %$m_info 
    );

    
    # warn "Actual: " . Dumper( $m_info );


    # Expand * and *{...} strings.
    foreach (grep defined $m_info->{$_}, keys %$m_info) {
      $m_info->{$_} =~ s/\*(?:\{([^\}]+)?\})?/ $m_info->{ $1 || 'name' } /ge
    }
    if ( $m_info->{'modifier'} and $mm_def->{modifier}{-folding} ) {
      $m_info->{'modifier'} = _fold_modifiers( $m_info->{'modifier'}, 
			$mm_def->{modifier}{-folding} )
    }
    
    ### METHOD GENERATION ### Requires: $mm_def, $interface, $m_info
    
    # If the MM def provides an initialization "-init" call, run it.
    if ( local $_ = $mm_def->{'behavior'}->{'-init'} ) {
      push @methods, map $_->( $m_info ), (ref($_) eq 'ARRAY') ? @$_ : $_;
    }
    # Build Methods
    for ( grep { /^[^-]/ } keys %$interface ) { 
      my $function_name = $_;
      $function_name =~ s/\*/$m_info->{'name'}/g;
      
      my $behavior = $interface->{$_};
      
      # Fold in additional modifiers
      if ( $m_info->{'modifier'} ) { 
	if ( $behavior =~ /^\-/ and $mm_def->{modifier}{-folding} ) {
	  $behavior = $m_info->{'modifier'} = 
			_fold_modifiers( "$m_info->{'modifier'} $behavior", 
			    $mm_def->{modifier}{-folding} )
	} else {
	  $behavior = "$m_info->{'modifier'} $behavior";
	}
      }

      my $builder = 
	( $mm_def->{'-behavior_cache'}{$behavior} ) ? 
	$mm_def->{'-behavior_cache'}{$behavior} : 
	( ref($mm_def->{'behavior'}{$behavior}) eq 'CODE' ) ? 
	$mm_def->{'behavior'}{$behavior} : 
_behavior_builder( $mm_def, $behavior, $m_info );
      
      my $method = &$builder( $m_info );
      
      _diagnostic('debug_make_behave', $behavior, $function_name, $method);
      push @methods, ($function_name => $method) if ($method);
    }
    
    # If the MM def provides a "-subs" call, for forwarding and other
    # miscelaneous "subsidiary" or "contained" methods, run it.
    if ( my $subs = $mm_def->{'behavior'}->{'-subs'} ) {
      my @subs = (ref($subs) eq 'ARRAY') ? @$subs : $subs;
      foreach my $sub ( @subs ) {
	my @results = $sub->($m_info);
	if ( scalar @results == 1 and ref($results[0]) eq 'HASH' ) {
	  # If it returns a hash of helper method types, check the method info
	  # for any matching names and call the corresponding method generator.
	  my $types = shift @results;
	  foreach my $type ( keys %$types ) {
	    my $names = $m_info->{$type} or next; 
	    my @names = ref($names) eq 'ARRAY' ? @$names : split(' ', $names);
	    my $generator = $types->{$type};
	    push @results, map { $_ => &$generator($m_info, $_) } @names;
	  }	
	}
	push @methods, @results;
      }
    }
    
    # If the MM def provides a "-register" call, for registering meta-method
    # information for run-time access, run it.
    if ( local $_ = $mm_def->{'behavior'}->{'-register'} ) {
      push @methods, map $_->( $m_info ), (ref($_) eq 'ARRAY') ? @$_ : $_;
    }
  }
  
  return @methods;
}

# I'd like for the make_methods() sub to be simpler, and to take advantage
# of the standard _get_declarations parsing provided by the superclass.
# Sadly the below doesn't work, due to a few order-of-operations peculiarities 
# of parsing interfaces and modifiers, and their associated default paramters.
# Perhaps it might work if the processing of --options could be overridden with
# a callback sub, so that interfaces and their params can be parsed in order.
sub _x_get_declarations {	
  my $mm_def = shift;

  my @declarations = $mm_def::SUPER->_get_declarations( @_ );

  # use Data::Dumper;
  # warn "In: " . Dumper( \@_ );
  # warn "Auto: " . Dumper( \@declarations );

  my %interface_cache;

  while (scalar @declarations) {
    
    my $m_info = shift @declarations;

    # Parse interfaces and modifiers
    my @specials = grep $_, split '--', ( delete $m_info->{'--'} || '' );
    foreach my $special ( @specials ) {
      if ( exists $mm_def->{'interface'}{$special} ) {
	# --interface
	$m_info->{'interface'} = $special;
      
      } elsif ( exists $mm_def->{'modifier'}{$special} ) {
	# --modifier
	$m_info->{'modifier'} .= 
			  ( $m_info->{'modifier'} ? ' ' : '' ) . "-$special";
      
      } elsif ( exists $mm_def->{'behavior'}{$special} ) {
	# --behavior as shortcut for single-method interface
	$m_info->{'interface'} = $special;
      
      } else {
	_diagnostic('make_bad_modifier', $mm_def->{'name'}, "--$special");
      }
    }

    my $interface = (
	$interface_cache{ $m_info->{'interface'} } 
	  ||= _interpret_interface( $mm_def, $m_info->{'interface'} )
    );
    $m_info = { %$m_info, %{$interface->{-params}} } if $interface->{-params};

    _diagnostic('debug_declaration', join(', ', map { defined $_ ? $_ : '(undef)' } %$m_info) );
    
    # warn "Updated: " . Dumper( $m_info );
  }
}

########################################################################
### TEMPLATES: _interpret_interface()
########################################################################

sub _interpret_interface {
  my ($mm_def, $interface) = @_;
  
  if ( ref $interface eq 'HASH' ) { 
    return $interface if exists $interface->{'-parsed'};
  } 
  elsif ( ! defined $interface or ! length $interface ) { 
    _diagnostic('tmpl_empty');

  } 
  elsif ( ! ref $interface ) {
    if ( exists $mm_def->{'interface'}{ $interface } ) {
      if ( ! ref $mm_def->{'interface'}{ $interface } ) { 
	$mm_def->{'interface'}{ $interface } = 
				{ '*' => $mm_def->{'interface'}{ $interface } };
      }
    } elsif ( exists $mm_def->{'behavior'}{ $interface } ) {
      $mm_def->{'interface'}{ $interface } = { '*' => $interface };
    } else {
      _diagnostic('tmpl_unkown', $interface);
    }
    $interface = $mm_def->{'interface'}{ $interface };
    
    return $interface if exists $interface->{'-parsed'};
  }
  elsif ( ref $interface ne 'HASH' ) {
    _diagnostic('tmpl_unsupported', $interface);
  } 
  
  $interface->{'-parsed'} = "$_[1]";
  
  # Allow interface inheritance via -base specification
  if ( $interface->{'-base'} ) {
    for ( split ' ', $interface->{'-base'} ) {
      my $base = _interpret_interface( $mm_def, $_ );
      %$interface = ( %$base, %$interface );
    }
    delete $interface->{'-base'};
  }
  
  for (keys %$interface) {
    # Remove empty/undefined items.
    unless ( defined $interface->{$_} and length $interface->{$_} ) {
      delete $interface->{$_};
      next;
    }
  }
  # _diagnostic('debug_interface', $_[1], join(', ', %$interface ));
  
  return $interface;
}

########################################################################
### BEHAVIORS AND MODIFIERS: _fold_modifiers(), _behavior_builder()
########################################################################

sub _fold_modifiers {
  my $spec = shift;
  my $rules = shift;
  my %rules = @$rules;
  
  # Longest first, to prevent over-eager matching.
  my $rule = join '|', map "\Q$_\E", 
	sort { length($b) <=> length($a) } keys %rules;
  # Match repeatedly from the front.
  1 while ( $spec =~ s/($rule)/$rules{$1}/ );
  $spec =~ s/(^|\s)\s/$1/g;
  return $spec;
}

sub _behavior_builder {
  my ( $mm_def, $behavior, $m_info ) = @_;
  
  # We're going to have to do some extra work here, so we'll cache the result
  my $builder;
  
  # Separate the modifiers
  my $core_behavior = $behavior;
  my @modifiers;
  while ( $core_behavior =~ s/\-(\w+)\s// ) { push @modifiers, $1 }
  
  # Find either the built-in or universal behavior template
  if ( $mm_def->{'behavior'}{$core_behavior} ) {
    $builder = $mm_def->{'behavior'}{$core_behavior};
  } else {
    my $universal = _definition('Class::MakeMethods::Template::Universal','generic');
    $builder = $universal->{'behavior'}{$core_behavior} 
  }
  
  # Otherwise we're hosed.
  $builder or _diagnostic('make_bad_behavior', $m_info->{'name'}, $behavior);
  
  if ( ! ref $builder ) {
    # If we've got a text template, pass it off for interpretation.
    my $code = ( ! $Class::MakeMethods::Utility::DiskCache::DiskCacheDir ) ?
      _interpret_text_builder($mm_def, $core_behavior, $builder, @modifiers) 
    : _disk_cache_builder($mm_def, $core_behavior, $builder, @modifiers);
    
    # _diagnostic('debug_eval_builder', $name, $code);
    local $^W unless $Class::MakeMethods::CONTEXT{Debug};
    $builder = eval $code;
    if ( $@ ) { _diagnostic('behavior_eval', $@, $code) }
    unless (ref $builder eq 'CODE') { _diagnostic('behavior_eval', $@, $code) }
  
  } elsif ( scalar @modifiers ) {
    # Can't modify code subs
    _diagnostic('make_behavior_mod', join(', ', @modifiers), $core_behavior);
  }
  
  $mm_def->{'-behavior_cache'}{$behavior} = $builder;

  return $builder;
}

########################################################################
### CODE EXPRESSIONS: _interpret_text_builder(), _disk_cache_builder()
########################################################################

sub _interpret_text_builder {
  require Class::MakeMethods::Utility::TextBuilder;
  
  my ( $mm_def, $name, $code, @modifiers ) = @_;
  
  foreach ( @modifiers ) {
    exists $mm_def->{'modifier'}{$_} 
      or _diagnostic('behavior_mod_unknown', $name, $_);
  }
  
  my @exprs = grep { $_ } map { 
	$mm_def->{'modifier'}{ $_ }, 
	$mm_def->{'modifier'}{ "$_ $name" } || $mm_def->{'modifier'}{ "$_ *" }
      } ( '-all', ( scalar(@modifiers) ? @modifiers : '-default' ) );
  
  # Generic method template
  push @exprs, "return sub _SUB_ATTRIBS_ { \n  my \$self = shift;\n  * }";
  
  # Closure-generator
  push @exprs, "sub { my \$m_info = \$_[0]; * }";
  
  my $exprs = $mm_def->{code_expr};
  unshift @exprs, { 
	( map { $_=>$exprs->{$_} } grep /^[^-]/, keys %$exprs ),
	'_BEHAVIOR_{}' => $mm_def->{'behavior'},
	'_SUB_ATTRIBS_' => '',
  };
  
  my $result = Class::MakeMethods::Utility::TextBuilder::text_builder($code,
								       @exprs);
  
  my $modifier_string = join(' ', map "-$_", @modifiers);
  my $full_name = "$name ($mm_def->{template_class} $mm_def->{template_name}" .
		    ( $modifier_string ? " $modifier_string" : '' ) . ")";
  
  _diagnostic('debug_template_builder', $full_name, $code, $result);
  
  return $result;
}

sub _disk_cache_builder { 
  require Class::MakeMethods::Utility::DiskCache;
  my ( $mm_def, $core_behavior, $builder, @modifiers ) = @_;
  
  Class::MakeMethods::Utility::DiskCache::disk_cache( 
    "$mm_def->{template_class}::$mm_def->{template_name}", 
    join('.', $core_behavior, @modifiers),
    \&_interpret_text_builder, ($mm_def, $core_behavior, $builder, @modifiers)
  );
}

1;

__END__


=head1 NAME

Class::MakeMethods::Template - Extensible code templates 


=head1 SYNOPSIS

  package MyObject;
  use Class::MakeMethods::Template::Hash (
    'new'       => 'new',
    'string'    => 'foo',
    'number'    => 'bar',
  );
   
  my $obj = MyObject->new( foo => "Foozle", bar => 23 );
  print $obj->foo();
  $obj->bar(42);


=head1 MOTIVATION

If you compare the source code of some of the closure-generating
methods provided by other subclasses of Class::MakeMethods,
such as the C<hash> accessors provided by the various Standard::*
subclasses, you will notice a fair amount of duplication. This
module provides a way of assembling common pieces of code to
facilitate support the maintenance of much larger libraries of
generated methods.


=head1 DESCRIPTION

This module extends the Class::MakeMethods framework by providing
an abstract superclass for extensible code-templating method
generators.

Common types of methods are generalized into B<template definitions>.
For example, C<Template::Generic>'s C<new> provides a template for
methods that create object instances, while C<Template::Generic>'s
C<scalar> is a template for methods that allow you to get and set
individual scalar values.

Thse definitions are then re-used and modified by various B<template
subclasses>. For example, the C<Template::Hash> subclass supports
blessed-hash objects, while the C<Template::Global> subclass supports
shared data; each of them includes an appropriate version of the
C<scalar> accessor template for those object types.

Each template defines one or more B<behaviors>, individual methods
which can be installed in a calling package, and B<interfaces>,
which select from those behaviours and indicate the names to install
the methods under.

Each individual meta-method defined by a calling package requires
a B<method name>, and may optionally include other key-value
B<parameters>, which can control the operation of some meta-methods.


=head1 USAGE

=head2 Class::MakeMethods Calling Conventions

When you C<use> this package, the method declarations you provide
as arguments cause subroutines to be generated and installed in
your module.

You can also omit the arguments to C<use> and instead make methods
at runtime by passing the declarations to a subsequent call to
C<make()>.

You may include any number of declarations in each call to C<use>
or C<make()>. If methods with the same name already exist, earlier
calls to C<use> or C<make()> win over later ones, but within each
call, later declarations superceed earlier ones.

You can install methods in a different package by passing C<-TargetClass =E<gt> I<package>> as your first arguments to C<use> or C<make>. 

See L<Class::MakeMethods> for more details.

=head2 Passing Parameters

The following types of Basic declarations are supported:

=over 4

=item *

I<generator_type> => "I<method_name>"

=item *

I<generator_type> => "I<name_1> I<name_2>..."

=item *

I<generator_type> => [ "I<name_1>", "I<name_2>", ...]

=back

See L<Class::MakeMethods::Docs::Catalog/"TEMPLATE CLASSES"> for a list of the supported values of I<generator_type>.

For each method name you provide, a subroutine of the indicated
type will be generated and installed under that name in your module.

Method names should start with a letter, followed by zero or more
letters, numbers, or underscores.

=head2 Standard Declaration Syntax

The Standard syntax provides several ways to optionally associate
a hash of additional parameters with a given method name.

=over 4

=item *

I<generator_type> => [ "I<name_1>" => { I<param>=>I<value>... }, ... ]

A hash of parameters to use just for this method name. 

(Note: to prevent confusion with self-contained definition hashes,
described below, parameter hashes following a method name must not
contain the key 'name'.)

=item *

I<generator_type> => [ [ "I<name_1>", "I<name_2>", ... ] => { I<param>=>I<value>... } ]

Each of these method names gets a copy of the same set of parameters.

=item *

I<generator_type> => [ { "name"=>"I<name_1>", I<param>=>I<value>... }, ... ]

By including the reserved parameter C<name>, you create a self
contained declaration with that name and any associated hash values.

=back

Basic declarations, as described above, are treated as having an empty parameter hash.

=head2 Default Parameters

A set of default parameters to be used for several declarations
may be specified using any of the following types of arguments to
a Template method generator call:

=over 4

=item * 

'-I<param>' => 'I<value>'

Set a default value for the specified parameter.

=item * 

'--' => { 'I<param>' => 'I<value>', ... }

Set default values for one or more parameters. Equivalent to a series of '-I<param>' => 'I<value>' pairs for each pair in the referenced hash.

=item * 

'--I<special_param_value>' 

Specifies a value for special parameter; the two supported parameter types are: 

=over 4

=item -

'--I<interface_name>' 

Select a predefined interface; equivalent to '-interface'=> 'I<interface_name>'.

For more information about interfaces, see L<"Selecting Interfaces"> below.

=item -

'--I<modifier_name>' 

Select a global behavior modifier, such as '--private' or '--protected'.

For more information about modifiers, see L<"Selecting Modifiers"> below.

=back

=back

Parameters set in these ways are passed to each declaration that
follows it until the end of the method-generator argument array,
or until overridden by another declaration. Parameters specified
in a hash for a specific method name, as discussed above, will
override the defaults of the same name for that particular method.


=head1 PARAMETER REFERENCE

Each meta-method is allocated a hash in which to store its parameters
and optional information.

(Note that you can not override parameters on a per-object level.)

=head2 Special Parameters

The following parameters are pre-defined or have a special meaning:

=over 4

=item *

name

The primary name of the meta-method. Note that the subroutines
installed into the calling package may be given different names,
depending on the rules specified by the interface.

=item *

interface

The name of a predefined interface, or a reference to a custom
interface, to use for this meta-method. See L</Selecting Interfaces>, below.

=item *

modifier

The names of one or more predefined modifier flags. See L</Selecting Modifiers>, below.

=back

=head2 Informative Parameters

The following parameters are set automatically when your meta-method is declared:

=over 4

=item *

target_class

The class that requested the meta-method, into which its subroutines
will be installed.

=item *

template_name

The Class::MakeMethods::Template method used for this declaration.

=item *

template_class

The Class::MakeMethods::Template subclass used for this declaration.

=back

=head2 Other Parameters

Specific subclasses and template types provide support for additional
parameters.

Note that you generally should not arbitrarily assign additional
parameters to a meta-method unless you know that they do not conflict
with any parameters already defined or used by that meta-method.


=head2 Parameter Expansion

If a parameter specification contains '*', it is replaced with
the primary method name.

Example: The following defines counter (*, *_incr, *_reset)
meta-methods j and k, which use the hash keys j_index and k_index
to fetch and store their values.

  use Class::MakeMethods::Template::Hash
    counter => [ '-hash_key' => '*_index', qw/ j k / ];

(See L<Class::MakeMethods::Template::Hash> for information about the C<hash_key> parameter.)

If a parameter specification contains '*{I<param>}', it is replaced
with the value of that parameter.

Example: The following defines a Hash scalar meta-method which will
store its value in a hash key composed of the defining package's
name and individual method name, such as
C<$self-E<gt>{I<MyObject>-I<foo>}>:

  use Class::MakeMethods::Template::Hash
    'scalar' => [ '-hash_key' => '*{target_class}-*{name}', qw/ l / ];


=head2 Selecting Interfaces

Each template provides one or more predefined interfaces, each of which specifies one or more methods to be installed in your package, and the method names to use. Check the documentation for specific templates for a list of
which interfaces they define.

An interface may be specified for a single method by providing an
'interface' parameter:

=over 4

=item * 

'I<interface_name>'

Select a predefined interface.

Example: Instead of the normal Hash scalar method named x, the
following creates methods with "Java-style" names and behaviors,
getx and setx.

  use Class::MakeMethods::Template::Hash
    'scalar' => [ 'x' => { interface=>'java' } ];

(See L<Class::MakeMethods::Template::Generic/"scalar"> for a
description of the C<java> interface.)

=item * 

'I<behavior_name>'

A simple interface consisting only of the named behavior.

For example, the below declaration creates a read-only methods named q. (There
are no set or clear methods, so any value would have to be placed
in the hash by other means.)

  use Class::MakeMethods::Template::Hash (
    'scalar' => [ 'q' => { interface=>'get' } ] 
  );

=item * 

{  'I<subroutine_name_pattern>' => 'I<behavior_name>', ... }

A custom interface consists of a hash-ref that maps subroutine names to the associated behaviors. Any C<*> characters in I<subroutine_name_pattern> are replaced with the declared method name.

For example, the below delcaration creates paired get_w and set_w methods:

  use Class::MakeMethods::Template::Hash (
    'scalar' => [ 'w' => { interface=> { 'get_*'=>'get', 'set_*'=>'set' } } ] 
  );

=back

Some interfaces provide very different behaviors than the default
interface.

Example: The following defines a method g, which if called with an
argument appends to, rather than overwriting, the current value:

  use Class::MakeMethods::Template::Hash
    'string' => [ '--get_concat', 'g' ];

A named interface may also be specified as a default in the argument
list with a leading '--' followed by the interface's name.

Example: Instead of the normal Hash scalar methods (named x and
clear_x), the following creates methods with "Java-style" names
and behaviors (getx, setx).

  use Class::MakeMethods::Template::Hash
    'scalar' => [ '--java', 'x'  ];

An interface set in this way affects all meta-methods that follow it
until another interface is selected or the end of the array is
reached; to return to the original names request the 'default'
interface.

Example: The below creates "Java-style" methods for e and f, "normal
scalar" methods for g, and "Eiffel-style" methods for h.

  use Class::MakeMethods::Template::Hash
    'scalar' => [
      '--java'=> 'e', 'f', 
      '--default'=> 'g', 
      '--eiffel'=> 'h',
    ];


=head2 Selecting Modifiers

You may select modifiers, which will affect all behaviors.

  use Class::MakeMethods::Template::Hash
      'scalar' => [ 'a', '--protected' => 'b', --private' => 'c' ];

Method b croaks if it's called from outside of the current package
or its subclasses.

Method c croaks if it's called from outside of the current package.

See the documentation for each template to learn which modifiers it supports.


=head2 Runtime Parameter Access

If the meta-method is defined using an interface which includes the
attributes method, run-time access to meta-method parameters is
available.

Example: The following defines a counter meta-method named y, and
then later changes the 'join' parameter for that method at runtime.

  use Class::MakeMethods ( get_concat => 'y' );
  
  y_attributes(undef, 'join', "\t" )
  print y_attributes(undef, 'join')


=head1 EXTENDING

You can create your own method-generator templates by following the below outline.


=head2 Mechanisms

Dynamic generation of methods in Perl generally depends on one of two approaches: string evals, which can be as flexible as your string-manipulation functions allow, but are run-time resource intensive; or closures, which are limited by the number of subroutine constructors you write ahead of time but which are faster and smaller than evals. 

Class::MakeMethods::Template uses both of these approaches: To generate different types of subroutines, a simple text-substitution mechanism combines bits of Perl to produce the source code for a subroutine, and then evals those to produce code refs. Any differences which can be handled with only data changes are managed at the closure layer; once the subroutines are built, they are repeatedly bound as closures to hashes of parameter data.

=head2 Code Generation

A substitution-based "macro language" is used to assemble code strings. This happens only once per specific subclass/template/behavior combination used in your program. (If you have disk-caching enabled, the template interpretation is only done once, and then saved; see below.)

There are numerous examples of this within the Generic interface and its subclasses; for examples, look at the following methods: Universal:generic, Generic:scalar, Hash:generic, and Hash:scalar.

See L<Class::MakeMethods::Utility::TextBuilder> for more information.


=head2 Template Definitions

Template method generators are declared by creating a subroutine that returns a hash-ref of information about the template. When these subroutines are first called, the template information is filled in with imported and derived values, blessed as a Class::MakeMethods::Template object, and cached. 

Each C<use> of your subclass, or call to its C<make>, causes these objects to assemble the requested methods and return them to Class::MakeMethods for installation in the calling package.

Method generators defined this way will have support for parameters, custom interfaces, and the other features discussed above.

(Your module may also use the "Aliasing" and "Rewriting" functionality described in L<Class::MakeMethods/EXTENDING>.)

Definition hashes contain several types of named resources in a second level of hash-refs under the following keys:

=over 4

=item * 

interface - Naming styles (see L<"Defining Interfaces">, below)

=item *

params - Default parameters for meta-methods declared with this template (see L<"Default Parameters">, below)

=item *

behavior - Method recipes (see L<"Defining Behaviors">, below)

=item *

code_expr - Bits of code used by the behaviors

=back

=head2 Minimum Template Definition

You must at least specify one behavior; all other information is optional.

Class::MakeMethods will automatically fill in the template name and class
as 'template_name' and 'template_class' entries in the version of your
template definition hash that it caches and uses for future execution.

For example a simple sub-class that defines a method type
upper_case_get_set might look like this:

  package Class::MakeMethods::UpperCase;
  use Class::MakeMethods '-isasubclass';
  
  sub uc_scalar {
    return { 
      'behavior' => {
	'default' => sub { 
	  my $m_info = $_[0]; 
	  return sub {
	    my $self = shift;
	    if ( scalar @_ ) { 
	      $self->{ $m_info->{'name'} } = uc( shift ) 
	    } else {
	      $self->{ $m_info->{'name'} };
	    }
	  }
	},
      }
    }
  }

And a caller could then use it to generate methods in their package by invoking:

  Class::MakeMethods::UpperCase->make( 'uc_scalar' => [ 'foo' ] );

=head2 Default Parameters

Each template may include a set of default parameters for all declarations as C<params =E<gt> I<hash_ref>>.

Template-default parameters can be overridden by interface '-params', described below, and and method-specific parameters, described above.

=head2 Defining Interfaces

Template definitions may have one or more interfaces, including
the default one, named 'default', which is automatically selected
if another interface is not requested. (If no default interface is
provided, one is constructed, which simply calls for a behavior
named default.)

Most commonly, an interface is specified as a hash which maps one or
more subroutine names to the behavior to use for each. The interface
subroutine names generally contain an asterisk character, '*', which
will be replaced by the name of each meta-method.

Example: The below defines methods e_get, e_set, and e_clear.

  use Class::MakeMethods::Template::Hash
    'scalar' => [
      -interface=>{ '*_clear'=>clear, '*_get'=>'get', '*_set'=>'set' }, 'e' 
    ];

If the provided name does not contain an asterisk, it will not be
modified for individual meta-methods; for examples, see the bit_fields
method generated by Generic bits, and the DESTROY method generated
by InsideOut meta-methods.

In addition to the name-to-behavior correspondences described above,
interfaces may also contain additional entries with keys beginning
with the '-' character which are interpreted as follows:

=over 4

=item *

C<-params =E<gt> I<hash_ref>>

Interfaces may include a '-params' key and associated reference
to a hash of default parameters for that interface.

=item *

C<-base =E<gt> I<interface_name>>

Interfaces can be based on previously existing ones by including
a -base specification in the the hash. The base value should contain
one or more space-separated names of the interfaces to be included.

Example: The below defines methods getG, setG, and clearG.

  use Class::MakeMethods::Template::Hash
    'scalar' => [
      -interface => { -base=>'java', 'clear*'=>'clear' }, qw/ G / 
    ];

If multiple interfaces are included in the -base specification and
specify different behaviors for the same subroutine name, the later
ones will override the earlier. Names which appear in the base
interface can be overridden by providing a new value, or a name
can be removed by mapping it to undef or the empty string.

Example: The following defines a get-set meta-method h, but suppresses
the clear_h method:

  use Class::MakeMethods::Template::Hash
    'scalar' => [
      -interface => { -base=>'with_clear', 'clear_*'=>'' }, qw/ h / 
    ];

=back


=head2 Defining Behaviors

Behaviors can be provided as text which is eval'd to form a
closure-generating subroutine when it's first used; C<$self> is
automatically defined and assigned the value of the first argument.

      'behavior' => {
	'default' => q{
	    if ( scalar @_ ) { $self->{ $m_info->{'name'} } = uc shift }
	    $self->{ $m_info->{'name'} };
	},
      }

A simple substitution syntax provides for macro interpretation with
definition strings. This functionality is currently undocumented;
for additional details see the _interpret_text_builder function in
Class::MakeMethods, and review the code_expr hashes defined in
Class::MakeMethods::Generic.


=head2 Importing

You can copy values out of other template definitions by specifying
an '-import' key and corresponding hash reference. You can specify
an -import for inside any of the template definition sub-hashes.
If no -import is specified for a subhash, and there is a top-level
-import value, it is used instead.

Inside an -import hash, provide C<I<TemplateClass>:I<type>> names
for each source you wish to copy from, and the values to import,
which can be a string, a reference to an array of strings, or '*'
to import everything available. (The order of copying is not
defined.)

Example: The below definition creates a new template
which is identical to an existing one.

  package Class::MakeMethods::MyMethods;
  sub scalarama {
    { -import => { 'Template::Hash:scalar' => '*' } }
  }

Values that are already set are not modified, unless they're an
array ref, in which case they're added to.

Example:

  package Class::MakeMethods::MyMethods;
  sub foo_method {
    { 'behavior' => {
      '-init' => [ sub {  warn "Defining foo_method $_[0]->{'name'}" } ],
      'default' => q{ warn "Calling foo_method behavior" }.
    } }
  }
  sub bar_method {
    { 'behavior' => {
      -import => { 'MyMethods:foo_method' => '*' },
      '-init' => [ sub {  warn "Defining bar_method $_[0]->{'name'}" } ],
      'default' => q{ warn "Calling bar_method behavior" }.
    } }
  }

In this case, the bar_method ends up with an array of two '-init'
subroutines, its own and the imported one, but only its own default
behavior.



=head2 Modifying Existing Templates

You can over-write information contained in template definitions
to alter their subsequent behavior. 

Example: The following extends the Hash:scalar template definition
by adding a new interface, and then uses it to create scalar accessor
methods named access_p and access_q that get and set values for
the hash keys 'p' and 'q':

  Class::MakeMethods::Template::Hash->named_method('scalar')->
	  {'interface'}{'frozzle'} = { 'access_*'=>'get_set' };

  package My::Object;
  Class::MakeMethods::Template::Hash->make( 'scalar' => [ --frozzle => qw( p q ) ] );

  $object->access_p('Potato');    # $object->{p} = 'Potato'
  print $object->access_q();      # print $object->{q}
  

Note that this constitutes "action at a distance" and will affect subsequent use by other packages; unless you are "fixing" the current behavior, you are urged to create your own template definition which imports the base behavior of the existing template and overrides the information in question.

Example: The following safely declares a new version of Hash:scalar with the desired additional interface:

  package My::Methods;
  
  sub scalar {
    { 
      -import => { 'Template::Hash:scalar' => '*' } ,
      interface => { 'frozzle' => { 'access_*'=>'get_set' } },
    }
  }

  package My::Object;
  My::Methods->make( 'scalar' => [ --frozzle => qw( p q ) ] );


=cut

=head2 Disk Caching

To enable disk caching of generated code, create an empty directory and pass it to the DiskCache package:

  use Class::MakeMethods::Utility::DiskCache qw( /my/code/dir );

This has a mixed effect on performance, but has the notable advantage of letting you view the subroutines that are being generated by your templates.

See L<Class::MakeMethods::Utility::DiskCache> for more information.


=head1 SEE ALSO

See L<Class::MakeMethods> for general information about this distribution. 

See L<Class::MakeMethods::Examples> for some illustrations of what you can do with this package.

For distribution, installation, support, copyright and license 
information, see L<Class::MakeMethods::Docs::ReadMe>.

=cut