Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Dualvars besides $!

by ikegami (Patriarch)
on Jan 27, 2014 at 20:36 UTC ( [id://1072275]=note: print w/replies, xml ) Need Help??


in reply to Dualvars besides $!

Are there other dualvars in perl core?

$ perl -MScalar::Util=isdual -E' $x=1; say isdual($x)?1:0; "$x"; say isdual($x)?1:0; ' 0 1

The stringification is cached.

Replies are listed 'Best First'.
Re^2: Dualvars besides $! (boring)
by tye (Sage) on Jan 28, 2014 at 02:16 UTC

    IMHO, that's a rather stupid interpretation of "dualvar". If the string value equals the stringification of the other value (IV or NV), then it shouldn't be considered a "dualvar". But then, that's what one would expect from a lazy XS function. A Perl version would just do 0+$x eq $x instead.

    !1 doesn't need to be implemented as a dualvar. $x=''; {no warnings; 0+$x;} is also enough to get you what is needed for !1 (a string value of '' and a numeric value of 0 w/o a warning). The canonical value for !1 that Perl uses happens to be a real dualvar, though. But "both an empty string and zero" isn't enough to imply "dualvar".

    Win32::TieRegistry can use dualvars.

    Update: To be clearer, a Perl version of isdual() would be:

    sub isdual { my( $v ) = @_; no warnings; return 0 if int $v ne $v^'0' # No IV nor NV in value || $v eq 0+$v # Cached string just same as IV/NV || $v == "$v" # Cached number just same as 0+PV ; return 1; }

    - tye        

Re^2: Dualvars besides $!
by davido (Cardinal) on Jan 27, 2014 at 21:32 UTC

    The stringification is cached.

    This is a good point. Is this good rule of thumb (bound to be imperfect but close enough)? Anytime an SV with an IV or NV creates a PV representation, that will remain in effect (making it a dualvar) until the container receives a new assignment.


    Dave

      say "!",$x,"!"; doesn't, but "everything else" does.
Re^2: Dualvars besides $!
by Tux (Canon) on Jan 28, 2014 at 11:27 UTC

    It is possible (in XS) to put anything in the specific slots:

    $ perl -MData::Peek -wC3 \
           -E'my $tv = Data::Peek::triplevar ("\N{GREEK SMALL LETTER PI}", 3, 3.1415);' \
           -e'say for map { $_ // "<undef>" } DDual ($tv)'
    π
    3
    3.1415
    <undef>
    0
    

    Data::Peek's DDual will return all of those: my ($pv, $iv, $nv, $rv, $hm) = DDual ($var [, $getmagic])


    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-25 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found