Dirk80 has asked for the wisdom of the Perl Monks concerning the following question:

I often have the case when reading a binary value from a file that it also has a textual representation. So I can use the dualvar functionality of Scalar::Util to solve this issue.

But that's not enough for me. Usually I have a corresponding table which contains the valid values and its representations. To avoid doing the same checks so often in code, I decided to tie the read scalar variable to a package which is doing these checks for me. Additionally on a change it shall always update the numeric and string context.

Here an example:

#!/usr/bin/perl use strict; use warnings; use My::DualVar; my %table = ( 1 => 'NORTH', 2 => 'SOUTH', 3 => 'EAST', 4 => 'WEST' ); # Case 1: Table num -> str print "Case 1: Dualvar via num2str table\n\n"; { my $direction = 2; My::DualVar->tie($direction, \%table); print_dualvar($direction); $direction = 4; print_dualvar($direction); $direction = 'NORTH'; print_dualvar($direction); } # Case 2: Table str -> num print "Case 2: Dualvar via str2num table\n\n"; { my $direction = 'SOUTH'; My::DualVar->tie($direction, reverse %table); print_dualvar($direction); $direction = 4; print_dualvar($direction); $direction = 'NORTH'; print_dualvar($direction); } sub print_dualvar { print "as num: " . ($_[0]+0) . "\n"; print "as str: $_[0]\n"; print "\n"; }

And here my written package:

package My::DualVar; use strict; use warnings; use Carp; use Scalar::Util qw( dualvar looks_like_number ); sub tie { # tie <class>, <scalar>, <arg1: scalar>, <arg2: table (hash or ref +hash)> tie $_[1], $_[0], $_[1], @_[2 .. $#_]; } sub TIESCALAR { my $class = shift; my $val = shift; croak "Given value is not defined" unless defined $val; my %table = @_ == 1 && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; croak "Given table is empty" if keys %table == 0; die "Given value $val is not in table" unless exists $table{$val}; # TODO: reject given table if values are not unique # Is there an efficient way to do this? my %reverse_table = reverse %table; my $impl; if( looks_like_number($val) ) { # num table, i.e. key: num, value: str while (my ($num, $str) = each %table) { croak "Key $num of given table is not a number" unless loo +ks_like_number($num); croak "Value $str of given table is not a string" if ref($ +str); } $impl = bless { '_val' => dualvar($val, $table{$val}), '_num_t +able' => \%table, '_str_table' => \%reverse_table }, $class; } else { # str table, i.e. key: str, value: num for my $num (values %table) { croak "Value $num of given table is not a number" unless l +ooks_like_number($num); } $impl = bless { '_val' => dualvar($table{$val}, $val), '_num_t +able' => \%reverse_table, '_str_table' => \%table }, $class; } return $impl; } sub FETCH { my ($impl) = @_; return $impl->{'_val'}; } sub STORE { my ($impl, $val) = @_; if( looks_like_number($val) ) { die "Invalid value $val" unless exists $impl->{'_num_table'}-> +{$val}; $impl->{'_val'} = dualvar($val, $impl->{'_num_table'}->{$val}) +; } else { die "Invalid value $val" unless exists $impl->{'_str_table'}-> +{$val}; $impl->{'_val'} = dualvar($impl->{'_str_table'}->{$val}, $val) +; } } 1;

This code is working. But I would be interested in your opinion. What could I do better?

And the second thing. Currently my code would work randomly if the values of the given table (hash) are not unique. Is there an efficient way to check whether the values of a hash are unique? Then I would reject such a hash

Or would there be a solution to return several values if the hash is not unique, e.g. key 2 and 5 would have value 'SOUTH'?

In numeric context it would not work. If I would set a dualvar variable to 'SOUTH', then I would have to return 2 or 5 in the FETCH-method. Perhaps in numeric context the smaller value should be returned.

In string context I think it is possible. Because I could give back a concatenated string and I still would have a scalar.