http://qs321.pair.com?node_id=11111540


in reply to Dualvar via table

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.

Replies are listed 'Best First'.
Re^2: Dualvar via table
by LanX (Saint) on Jan 17, 2020 at 21:21 UTC
    > I've thought a little about using the dual nature of variables in some perl code, but haven't actually taken the plunge

    I'm not sure if it's a good idea. Perl is adjusting the slots to each other depending on use history.

    Hence you'd magically loose information, and I'm not aware of a way to inhibit this.

    That's awfully hard to debug if it happens.

    (will add an example later)

    update
    Playing around, look at what happens at line 7

    DB<3> use Scalar::Util qw(dualvar); DB<4> $v = dualvar 137, 'Buster'; DB<5> p "$v" Buster DB<6> p 0+$v 137 DB<7> p $v++ # read string, increment number Buster DB<8> p $v++ # what...? 138 DB<9> p "$v" 139 DB<10>

    after incrementing $v, i.e. changing it in numeric context, the string-slot is adjusted to the number-slot°

    So can this only happen when I change one of the slots?

    If yes, shouldn't such dualvars always been made readonly, too?

    Hope you understand my worries.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    update

    °) or some flags are changed. Who knows....

    FWIW: a very good article from Brian here: https://www.effectiveperlprogramming.com/2011/12/create-your-own-dualvars/

      LanX:

      Yeah, it could be problematic. I haven't put any thought into its ramifications.

      ...roboticus

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

        Regarding your motivation:

        If I wanted attach extra information to a variable, I'd bless its reference.

        If I wanted to do this with a value, I'd tie the info into the value and accessed it via tied .

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice