Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (5)
    As of 2020-04-05 01:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The most amusing oxymoron is:
















      Results (33 votes). Check out past polls.

      Notices?