Added support for easily getting the original key name of a munged key: added a virtual '.coses: original key' entry to all hashes in COSES (works similar to the virtual '.length' item that all arrays have). Edited the debug string to mention these virtual items.

This commit is contained in:
ian%hixie.ch 2001-11-30 16:20:22 +00:00
Родитель 389c6783a8
Коммит 050ad60758
2 изменённых файлов: 32 добавлений и 14 удалений

Просмотреть файл

@ -62,7 +62,8 @@ sub getDefaultString {
!
! This example uses almost all the features of COSES, and so is
! quite a useful example to study. (It doesn't use all of the values
! of <set>'s attributes nor the escaping attributes of <text>.) It's
! of <set>'s attributes, the escaping attributes of <text>, nor the
! special '.length' and '.coses: original key' hidden keys.) It's
! also a great help when debugging! You can use it at any point in a
! COSES document merely by nesting it, so you can, for example,
! study what is happening with a <set> statement. If you declare
@ -91,6 +92,13 @@ sub getDefaultString {
<else>
<set variable="prefix" value="(prefix).(index)">
<include href="debug.dumpVars"/>
<!-- if ((prefix)) is an array, then
((prefix).length) would give the length of the array
if ((prefix)) is a hash element, then
((prefix).coses: original key) would give the
original name of the hash key, even if it contained
characters like '.' or '()'.
-->
</set>
</else>
</set>

Просмотреть файл

@ -286,22 +286,32 @@ sub expand {
sub evaluateVariable {
my $self = shift;
my($variable, $scope) = @_;
my($variable, $scope) = @_; # $scope is the whole data hash at this point
my @parts = split(/\./o, $variable, -1); # split variable at dots ('.') (the negative number prevents null trailing fields from being stripped)
# drill down through scope
my $scopeName = '';
foreach my $part (@parts) {
if (ref($scope) eq 'HASH') {
$scope = $scope->{$part};
} elsif (ref($scope) eq 'ARRAY') {
if ($part =~ /^\d+$/o) {
$scope = $scope->[$part];
} elsif ($part eq 'length') {
$scope = scalar(@$scope);
} else {
$self->assert(1, "Tried to drill into an array using a non-numeric key ('$part')");
}
if ($part eq 'coses: original key') {
$scope = $scopeName;
} else {
$self->error(1, "Could not resolve '$variable' (the part giving me trouble was '$part')");
$scopeName = $part;
if (ref($scope) eq 'HASH') {
if (defined($scope->{'coses: original keys'}) and
defined($scope->{'coses: original keys'}->{$part})) {
$scopeName = $scope->{'coses: original keys'}->{$part};
}
$scope = $scope->{$part};
} elsif (ref($scope) eq 'ARRAY') {
if ($part =~ /^\d+$/o) {
$scope = $scope->[$part];
} elsif ($part eq 'length') {
$scope = scalar(@$scope);
} else {
$self->assert(1, "Tried to drill into an array using a non-numeric key ('$part')");
}
} else {
$self->error(1, "Could not resolve '$variable' (the part giving me trouble was '$part')");
}
}
}
if (defined($scope)) {
@ -309,7 +319,7 @@ sub evaluateVariable {
while (ref($scope) eq 'SCALAR') {
$scope = $$scope;
}
return $scope;
return $scope; # $scope is the string resulting from evaluating the variable at this point
} else {
return '';
}