Subject: | Finding the key for an invalid value in a hash |
I'm attempting to create an error message for a hash value which
incorporates the value's key.
The twist is that I'm using the -values option, and I don't know how
to get Data::Domain to provide the key for a particular value.
All values should be numeric, e.g.
my $domain = Struct( -values => List( Num ) );
I'd like the error message to indicate which key had an invalid value.
The default message structure looks like this:
$Result = {
'-values' => [
'Num: invalid number'
]
};
Which doesn't provide the key
I thought I could use a lazy constructor and get access to the path
via the context, but it doesn't create a path which is traversable,
even for the value:
my $domain = Struct(
-values => List(
sub {
my $ctxt = shift;
Dump( [ $ctxt->{path} ], ['Path'] );
Dump( [ node_from_path( $ctxt->{root}, @{ $ctxt->{path} } ) ],
['Node'] );
Num;
} ) );
results in:
$Path = [
'-values',
0
];
$Node = undef;
I had hoped to simply change the '-values' to '-keys'. Even if the
path were traversable, that would require that the values and keys
were iterated in the same order.
Is there any means of identifying the key for a bogus value?
Thanks,
Diab
Here's my sample code:
use Data::Dumper;
use Data::Domain -all;
my %data = ( foo => 'bar' );
sub Dump { print Data::Dumper->new( @_ )->Dump; }
my $domain = Struct( -values => List( Num ) );
Dump [ $domain->inspect( \%data ) ], [ 'Result' ];
my $domain = Struct(
-values => List(
sub {
my $ctxt = shift;
Dump( [ $ctxt->{path} ], ['Path'] );
Dump( [ node_from_path( $ctxt->{root}, @{ $ctxt->{path} } ) ],
['Node'] );
Num;
} ) );
$domain->inspect( \%data );