| 1 | package Config::Maker::Eval; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | require Config::Maker::Path; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Config::Maker::Eval - Environment to run user-code in Config::Maker | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # In perl-code in metaconfig, config or template | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Get($path) | ||||||||||||||||||||||||||||
| 19 | Get($path, $default) | ||||||||||||||||||||||||||||
| 20 | Get1($path) | ||||||||||||||||||||||||||||
| 21 | Get1($path, $default) | ||||||||||||||||||||||||||||
| 22 | Value($path_or_option) | ||||||||||||||||||||||||||||
| 23 | Type($path_or_option) | ||||||||||||||||||||||||||||
| 24 | Exists($path) | ||||||||||||||||||||||||||||
| 25 | Unique($path) | ||||||||||||||||||||||||||||
| 26 | One($path) | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | All user code executed by Config::Maker, whether read from metaconfig, config | ||||||||||||||||||||||||||||
| 31 | or template, is executed in Config::Maker::Eval package. In that package, | ||||||||||||||||||||||||||||
| 32 | following convenience functions are available. Note, that when relative path is | ||||||||||||||||||||||||||||
| 33 | specified to any of them, it is resolved relative to the current topic ($_). | ||||||||||||||||||||||||||||
| 34 | Thus it must contain a config element. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =over 4 | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =item Get(I<$path>, I<[$default]>) | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Resolves I<$path> and returns list of results, or the first result in scalar | ||||||||||||||||||||||||||||
| 41 | context. If I<$default> is given, and the path does not match, I<$default> is | ||||||||||||||||||||||||||||
| 42 | returned. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 13 | ▶ | sub Get { | ||||||||||||||||||||||||||
|
Subroutine
Get: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 13 | my $path = shift; | |||||||||||||||||||||||||||
| 48 | 13 | $path = Config::Maker::Path->make($path); | |||||||||||||||||||||||||||
| 49 | 13 | my $res = $path->find($_); | |||||||||||||||||||||||||||
| 50 | 13 | ▶ | return wantarray ? @_ : $_[0] unless @$res; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 51 | 9 | ▶ | return wantarray ? @$res : $res->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item Get1(I<$path>, I<[$default]>) | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Resolves path and returns the result. If there is more than one result, or if | ||||||||||||||||||||||||||||
| 57 | the path does not match and no default is given, throws an error. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 8 | ▶ | sub Get1 { | ||||||||||||||||||||||||||
|
Subroutine
Get1: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 8 | my $path = shift; | |||||||||||||||||||||||||||
| 63 | 8 | $path = Config::Maker::Path->make($path); | |||||||||||||||||||||||||||
| 64 | 8 | my $res = $path->find($_); | |||||||||||||||||||||||||||
| 65 | 8 | ▶ | croak "$path should have at most one result" if @$res > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 7 | ▶ | croak "$path should have a result" unless @$res || @_ == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$res or @_ == 1
MC/DC100%2/2
@$res or @_ == 1
@$res
@_ == 1
Truth table100%3/3
A or B
A= @$resB= @_ == 1
| |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | return @$res ? $res->[0] : $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item Value(I<$path_or_option>) | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Returns value of config element or matching path (exactly one must match). If | ||||||||||||||||||||||||||||
| 73 | no arguments given, returns value of $_. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | sub Value { | ||||||||||||||||||||||||||
|
Subroutine
Value: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | my $arg = (@_ ? $_[0] : $_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | if(UNIVERSAL::isa($arg, 'Config::Maker::Option')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | return $arg->{-value}; | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 1 | return Get1($arg)->{-value}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =item Type(I<$path_or_option>) | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Returns type of config element or matching path (exactly one must match). If | ||||||||||||||||||||||||||||
| 89 | no arguments given, returns type of $_. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | ▶ | sub Type { | ||||||||||||||||||||||||||
|
Subroutine
Type: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | my $arg = (@_ ? $_[0] : $_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 2 | ▶ | if(UNIVERSAL::isa($arg, 'Config::Maker::Option')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | return $arg->{-type}; | |||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 1 | return Get1($arg)->{-type}; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item Exists(I<$path>) | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Returns true iff I<$path> matches at least one config element. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | sub Exists { | ||||||||||||||||||||||||||
|
Subroutine
Exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 3 | my @res = Get($_[0]); | |||||||||||||||||||||||||||
| 110 | 3 | return @res > 0; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item Unique(I<$path>) | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Returns true iff I<$path> matches at most one config element. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | sub Unique { | ||||||||||||||||||||||||||
|
Subroutine
Unique: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 3 | my @res = Get($_[0]); | |||||||||||||||||||||||||||
| 121 | 3 | return @res <= 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item One(I<$path>) | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Returns true iff I<$path> matches exactly one config element. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | ▶ | sub One { | ||||||||||||||||||||||||||
|
Subroutine
One: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 3 | my @res = Get($_[0]); | |||||||||||||||||||||||||||
| 132 | 3 | return @res == 1; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Jan Hudec <bulb@ucw.cz> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Copyright 2004 Jan Hudec. All rights reserved. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 146 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | configit(1), perl(1), Config::Maker(3pm). | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 9 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||