/
Data.pm
697 lines (422 loc) · 12.2 KB
/
Data.pm
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
# Copyright (C) 2001-2005, Parrot Foundation.
=pod
=head1 NAME
Parrot::Configure::Data - Configuration data container
=head1 SYNOPSIS
use Parrot::Configure::Data;
my $data = Parrot::Configure::Data->new;
my @values = $data->get(@keys);
$data->set($key1 => $value1, $key2 => $value2);
$data->add($delimiter, $key1 => $value1, $key2 => $value2);
my @keys = $data->keys;
my $serialized = $data->dump(q{c}, q{*PConfig});
$data->clean;
$data->settrigger($key, $trigger, $cb);
$data->gettriggers($key);
$data->gettrigger($key, $trigger);
$data->deltrigger($key, $trigger);
=head1 DESCRIPTION
This module provides methods by which other Parrot::Configure::* modules
can access configuration data.
The module supplies a constructor for Parrot::Configure::Data objects
and three kinds of accessors:
=over 4
=item 1 Main configuration data
=item 2 Triggers
=item 3 Data read from Perl 5's C<%Config> or Perl 5 special variables.
=back
=head1 USAGE
=cut
package Parrot::Configure::Data;
use strict;
use warnings;
use Data::Dumper ();
=head2 Constructor
=over 4
=item * C<new()>
=over 4
=item * Purpose
Basic object constructor.
=item * Arguments
None.
=item * Return Value
Parrot::Configure::Data object.
=back
=back
=cut
sub new {
my $class = shift;
my $self = {
c => {},
triggers => {},
p5 => {},
};
bless $self, ref $class || $class;
return $self;
}
=head2 Methods for Main Configuration Data
=over 4
=item * C<get($key, ...)>
=over 4
=item * Purpose
Provides access to the values assigned to elements in the
Parrot::Configure object's main data structure.
=item * Arguments
List of elements found in the Parrot::Configure object's main data
structure.
=item * Return Value
List of values associated with corresponding arguments.
=back
=cut
sub get {
my $self = shift;
my $c = $self->{c};
return @$c{@_};
}
=item * C<< set($key => $val, ...) >>
=over 4
=item * Purpose
Modifies or creates new values in the main part of the Parrot::Configure
object's data structure..
=item * Arguments
List of C<< key => value >> pairs.
=item * Return Value
Parrot::Configure::Data object.
=back
=cut
sub set {
my $self = shift;
my $verbose = defined $self->get('verbose') && $self->get('verbose') == 2;
print "\nSetting Configuration Data:\n(\n" if $verbose;
while ( my ( $key, $val ) = splice @_, 0, 2 ) {
print "\t$key => ", defined($val) ? "'$val'" : 'undef', ",\n"
if $verbose;
$self->{c}{$key} = $val;
foreach my $trigger ( $self->gettriggers($key) ) {
print "\tcalling trigger $trigger for $key\n" if $verbose;
my $cb = $self->gettrigger( $key, $trigger );
&$cb( $key, $val );
}
}
print ");\n" if $verbose;
return $self;
}
=item * C<< add($delim, $key => $val, ...) >>
=over 4
=item * Purpose
Either creates a new key or appends to an existing key, with the previous/new
values joined together by C<$delim>.
=item * Arguments
Delimiter value followed by a list of C<< key => value >> pairs.
=item * Return Value
Parrot::Configure::Data object.
=back
=cut
sub add {
my $self = shift;
my $delim = shift;
while ( my ( $key, $val ) = splice @_, 0, 2 ) {
my ($old) = $self->{c}{$key};
if ( defined $old ) {
$self->set( $key, "$old$delim$val" );
}
else {
$self->set( $key, $val );
}
}
return $self;
}
=item * C<keys()>
=over 4
=item * Purpose
Provides a list of names of elements in the Parrot::Configure object's
main data structure.
=item * Arguments
None.
=item * Return Value
List of elements in the Parrot::Configure object's main data structure.
=back
=cut
sub keys {
my $self = shift;
return keys %{ $self->{c} };
}
=item * C<get_PConfig()>
=over 4
=item * Purpose
Slurps in L<Parrot::Config> data from previous run of I<Configure.pl>.
=item * Arguments
None.
=item * Return Value
Reference to hash holding main Parrot::Configure data structure.
=back
=cut
sub get_PConfig {
my $self = shift;
my $res = eval <<EVAL_CONFIG;
no strict;
use Parrot::Config;
\\%PConfig;
EVAL_CONFIG
if ( not defined $res ) {
die "You cannot use --step until you have completed the full configure process\n";
}
$self->{c} = $res;
}
=item * C<get_PConfig_Temp()>
=over 4
=item * Purpose
Slurps in L<Parrot::Config> temporary data from previous run of
Configure.pl. Only to be used when running C<gen::makefiles> plugin.
=item * Arguments
None.
=item * Return Value
Reference to hash holding that part of the main Parrot::Configure data
structure holding temporary data.
=back
=cut
sub get_PConfig_Temp {
my $self = shift;
my $res = eval <<EVAL_CONFIG_TEMP;
no strict;
use Parrot::Config::Generated;
\\%PConfig_Temp;
EVAL_CONFIG_TEMP
if ( not defined $res ) {
die "You cannot use --step until you have completed the full configure process\n";
}
$self->{c}{$_} = $res->{$_} for CORE::keys %$res;
}
=item * C<dump()>
=over 4
=item * Purpose
Provides a L<Data::Dumper> serialized string of the objects key/value pairs
suitable for being C<eval>ed.
=item * Arguments
Two scalar arguments:
=over 4
=item 1
Key in Parrot::Configure object's data structure which is being dumped.
=item 2
Name of the dumped structure.
=back
Example:
$conf->data->dump(q{c}, q{*PConfig});
$conf->data->dump(q{c_temp}, q{*PConfig_Temp});
=item * Return Value
String.
=back
=cut
# Data::Dumper supports Sortkeys since 2.12
# older versions will work but obviously not sorted
{
if ( defined eval { Data::Dumper->can('Sortkeys') } ) {
*dump = sub {
my $self = shift;
my ( $key, $structure ) = @_;
Data::Dumper->new( [ $self->{$key} ], [$structure] )->Sortkeys(1)->Dump();
};
}
else {
*dump = sub {
my $self = shift;
my ( $key, $structure ) = @_;
Data::Dumper->new( [ $self->{$key} ], [$structure] )->Dump();
};
}
}
=item * C<clean()>
=over 4
=item * Purpose
Deletes keys matching C</^TEMP_/> from the internal configuration store,
and copies them to a special store for temporary keys.
Keys using this naming convention are intended to be used only temporarily,
I<e.g.> as file lists for Makefile generation.
Temporary keys are used B<only> to regenerate makefiles after configuration.
=item * Arguments
None.
=item * Return Value
Parrot::Configure::Data object.
=back
=back
=cut
sub clean {
my $self = shift;
$self->{c_temp}{$_} = delete $self->{c}{$_} for grep { /^TEMP_/ } CORE::keys %{ $self->{c} };
return $self;
}
=head2 Triggers
=over 4
=item * C<settrigger($key, $trigger, $cb)>
=over 4
=item * Purpose
Set a callback on C<$key> named C<$trigger>. Multiple triggers can be set on a
given key. When the key is set via C<set> or C<add> then all callbacks that
are defined will be called. Triggers are passed the key and value that was set
after it has been changed.
=item * Arguments
Accepts a key name, a trigger name, & a C<CODE> ref.
=item * Return Value
Parrot::Configure::Data object.
=back
=cut
sub settrigger {
my ( $self, $key, $trigger, $cb ) = @_;
return unless defined $key and defined $trigger and defined $cb;
my $verbose = defined $self->get('verbose') && $self->get('verbose') == 2;
print "Setting trigger $trigger on configuration key $key\n",
if $verbose;
$self->{triggers}{$key}{$trigger} = $cb;
return $self;
}
=item * C<gettriggers($key)>
=over 4
=item * Purpose
Get the names of all triggers set for C<$key>.
=item * Arguments
String holding single key name.
=item * Return Value
List of triggers set for that key.
=back
=cut
sub gettriggers {
my ( $self, $key ) = @_;
return unless defined $self->{triggers}{$key};
my $verbose = defined $self->get('verbose') && $self->get('verbose') == 2;
print "Looking up all triggers on configuration key $key\n"
if $verbose;
return CORE::keys %{ $self->{triggers}{$key} };
}
=item * C<gettrigger($key, $trigger)>
=over 4
=item * Purpose
Get the callback set for C<$key> under the name C<$trigger>
=item * Arguments
Accepts a key name & a trigger name.
=item * Return Value
C<CODE> ref.
=back
=cut
sub gettrigger {
my ( $self, $key, $trigger ) = @_;
return
unless defined $self->{triggers}{$key}
and defined $self->{triggers}{$key}{$trigger};
my $verbose = defined $self->get('verbose') && $self->get('verbose') == 2;
print "Looking up trigger $trigger on configuration key $key\n"
if $verbose;
return $self->{triggers}{$key}{$trigger};
}
=item * C<deltrigger($key, $trigger)>
=over 4
=item * Purpose
Removes the trigger on C<$key> named by C<$trigger>
=item * Arguments
Accepts a key name & a trigger name.
=item * Return Value
Parrot::Configure::Data object.
=back
=cut
sub deltrigger {
my ( $self, $key, $trigger ) = @_;
return
unless defined $self->{triggers}{$key}
and defined $self->{triggers}{$key}{$trigger};
my $verbose = defined $self->get('verbose') && $self->get('verbose') == 2;
print "Removing trigger $trigger on configuration key $key\n"
if $verbose;
delete $self->{triggers}{$key}{$trigger};
return $self;
}
=back
=head2 Methods for Perl 5 Data
=over 4
=item * C<get_p5($key, ...)>
=over 4
=item * Purpose
Retrieve data originally derived from the Perl 5 environment during
configuration step C<init::defaults> and stored in a special part of the
Parrot::Configure::Data object.
=item * Arguments
List of elements found in the Perl 5-related part of the
Parrot::Configure object's data structure.
=item * Return Value
List of values associated with corresponding arguments.
=item * Note
Once data from Perl 5's C<%Config> or special variables has been stored
in configuration step C<init::defaults>, C<%Config> and the special
variables should not be further accessed. Use this method instead.
=back
=cut
sub get_p5 {
my $self = shift;
my $p5 = $self->{p5};
return @$p5{@_};
}
=item * C<< set_p5($key => $val, ...) >>
=over 4
=item * Purpose
Looks up values from either (a) the C<%Config>, located in Config.pm
and imported via C<use Config;>, associated with the instance of Perl
(C<$^X>) used to run I<Configure.pl> and assigns those values to a
special part of the Parrot::Configure::Data object.
=item * Arguments
List of C<< key => value >> pairs. If the key being set is from
C<%Config>, the corresponding value should have the same name. If,
however, the key being set is a Perl 5 special variable (I<e.g.>,
C<%^O>), the corresponding value should be the 'English' name of that
special variable as documented in L<perlvar> (less the initial C<$>, of
course).
=item * Return Value
Parrot::Configure::Data object.
=item * Examples
=item * Note
This method should B<only> be used in configuration step
C<init::defaults>. It is B<not> the method used to assign values to the
main Parrot::Configure data structure; use C<set()> (above) instead.
=back
=cut
sub set_p5 {
my $self = shift;
my $verbose = defined $self->get('verbose') && $self->get('verbose') == 2;
print "\nSetting Configuration Data:\n(\n" if $verbose;
while ( my ( $key, $val ) = splice @_, 0, 2 ) {
print "\t$key => ", defined($val) ? "'$val'" : 'undef', ",\n"
if $verbose;
$self->{p5}{$key} = $val;
}
print ");\n" if $verbose;
return $self;
}
=item * C<keys_p5()>
=over 4
=item * Purpose
Provides a list of names of elements in the Parrot::Configure object's
main data structure.
=item * Arguments
None.
=item * Return Value
List of elements in the part of the Parrot::Configure object's data
structure storing Perl 5 configuration data.
=back
=back
=cut
sub keys_p5 {
my $self = shift;
return CORE::keys %{ $self->{p5} };
}
=head1 CREDITS
Based largely on code written by Brent Royal-Gordon C<brent@brentdax.com>.
=head1 AUTHOR
Joshua Hoblitt C<jhoblitt@cpan.org>
=head1 SEE ALSO
F<docs/configuration.pod>, L<Parrot::Configure>, L<Parrot::Configure::Step>,
L<Parrot::Configure::Step>
=cut
1;
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: