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

Re^2: Dualvar via table

by LanX (Archbishop)
on Jan 17, 2020 at 21:21 UTC ( #11111549=note: print w/replies, xml ) Need Help??


in reply to Re: Dualvar via table
in thread Dualvar via table

> 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/

Replies are listed 'Best First'.
Re^3: Dualvar via table
by roboticus (Chancellor) on Jan 18, 2020 at 00:06 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11111549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-02-29 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (128 votes). Check out past polls.

    Notices?