Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Dualvar via table by Dirk80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-26 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found