Generalized single element access
chromatic explained his problem with auto-dereferencing in the hash and array operators, and I said a bit more about it. I missed the right day to post this, but I had a half baked idea about solving the problem.
Basically, the auto-dereferencing, in combination with Perl 5.12 allowing hash operators to extend to arrays, means that part of the problem doesn't care what you have and another part does. When you want to do a single element access, you still have syntax that must denote the reference type. If you have a hash, you need curlies:
foreach my $key ( keys $hash_or_array ) {
my $value = $hash_or_array->{ $key };
}
But, if that's an array reference, you need square braces:
foreach my $key ( keys $hash_or_array ) {
my $value = $hash_or_array->[ $key ];
}
Get the wrong thing there and your Perl program dies. This sort of thing happens when you solve the easy half of the problem without following it all the way through.
But, who cares? Why don't we have a generalized single element access that doesn't care what sort of aggregate it is? Parens are taken for code references. The remaining ASCII pair is the angle brackets:
foreach my $key ( keys $hash_or_array ) {
my $value = $hash_or_array->< $key >;
}
But, we have Unicode source now, so we have lots of paired characters:
my $value = $hash_or_array->" $key ";
my $value = $hash_or_array->' $key ';
my $value = $hash_or_array->« $key »;
my $value = $hash_or_array->◀ $key ▶;
my $value = $hash_or_array->▶ $key ◀;
my $value = $hash_or_array->◤ $key ◢;
my $value = $hash_or_array->☛ $key ☚;
But, with generalized quoting, we can include subroutines too:
my $value = $hash_or_array_or_sub->« @keys »
If the subroutine is going to take multiple arguments, we can make this an automatic slice for the aggregates.
But what is the key is itself an array reference? We can auto deref that to get the slice. If it's a hash, we can take the list of keys as the slice indices.
Oh hell, isn't this half of Perl 6 now (and the half that I like)?
Leave a comment