Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Dirk80:

It appears (from my casual reading) that each tie would create a new copy of the table, so extensive use may cost more time and memory than needed. So you might want to have a function in My::DualVar that creates the table, and then you can tie the table to variables as necessary. I haven't played with tying variables (much or recently), so I'm sure my syntax is (likely to be) wrong, but what I'm thinking is something that would give you usage something like:

my %directions = ( 1=>'NORTH', 2=>'SOUTH', 3=>'EAST', 4=>'WEST' ); my $table_fwd = My::DualVar->new(%directions); my $table_rev = My::DualVar->new(reverse %directions); tie $direction, \$table_fwd; my ($dir, $dir2) = (4, 1); tie $dir, \$table_fwd; tie $dir2, \$table_fwd; print_dualvar($dir); ++$dir2; print_dualvar($dir2); tie $dir2, \$table_rev; for $dir2 (qw(SOUTH WEST EAST)) { $dir = $dir2 + 0; print_dualvar($dir); }

I've thought a little about using the dual nature of variables in some perl code, but haven't actually taken the plunge. (What I was thinking was along the lines of parsing a file, and keeping the original representation of a number in the string section so that if calculations are unexpected, I can see what string led to the fault. As a terrible example, suppose I was reading the number part of a street address into a variable (using a stupid technique):

use strict; no warnings; # !!! my @addresses = ( "1141 Tennessee Avenue", "12E10th Street" ); for (@addresses) { my $house_number = $_+0; print "House# across the street is ", $house_number+1, "\n"; print "DEBUG: Original house number '$house_number'\n\n"; }

Which prints:

$ perl t.pl House# across the street is 1142 DEBUG: Original house number '1141' House# across the street is 120000000001 DEBUG: Original house number '120000000000'

Where I would want to see:

House# across the street is 1142 DEBUG: Original house number '1141 Tennessee Avenue' House# across the street is 120000000001 DEBUG: Original house number '12E10th Street'

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Dualvar via table by roboticus
in thread 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 exploiting the Monastery: (3)
    As of 2020-05-25 18:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      If programming languages were movie genres, Perl would be:















      Results (146 votes). Check out past polls.

      Notices?