Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Is an SV and IV or a PV or an NV or a UV? How can I tell?

by Anonymous Monk
on Oct 24, 2011 at 18:27 UTC ( #933450=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

How can I find out if my SV is a IV or a PV? (Within perl)

  • Comment on Is an SV and IV or a PV or an NV or a UV? How can I tell?

Replies are listed 'Best First'.
Re: Is an SV and IV or a PV or an NV or a UV? How can I tell?
by Yary (Pilgrim) on Oct 24, 2011 at 18:44 UTC
    How about this:
    use 5.010; # just for "say" use B; ($f,$g)=(333,"xyz"); say ref B::svref_2object(\$f); say ref B::svref_2object(\$g);
    edit Devel::Peek might also help you.

      This will give the answer without the superfluous "B::" prefix:

      use feature qw( say ); use B qw( ); my ($f,$g) = (333,"xyz"); say B::class(B::svref_2object(\$f)); say B::class(B::svref_2object(\$g));
      IV PV

      Note that this returns the SV *type*. It can return "PV" even if the scalar has no PV.

      use feature qw( say ); use B qw( ); $_ = 333; "".$_; $_ = undef; say B::class(B::svref_2object(\$_));

      This is what the OP asked, but he probably wants to know what the scalar *has*.

Re: Is an SV and IV or a PV or an NV or a UV? How can I tell?
by ikegami (Pope) on Oct 24, 2011 at 21:24 UTC

    Are you asking what the SV is (NULL, BIND, IV, NV, PV, PVIV, PVNV, PVMG, REGEXP, PVGV, PVLV, PVAV, PVHV, PVCV, PVFM, PVIO), what fields the SV has (IV, NV, PV, GP, etc) or which value is present in the SV (IV, UV, NV, PV, RV, GP, private IV, private NV, private PV, etc)?

    For example,

    $ perl -MDevel::Peek -E'$_ = 2**31; ++$_; say "value: $_"; Dump $_;' value: 2147483649 SV = PVNV(0x8cc0ac8) at 0x8cebef8 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK,IsUV) UV = 2147483649 NV = 2147483648 PV = 0x8cdf378 "2147483649"\0 CUR = 10 LEN = 12

    $_ is a PVNV. It has fields IV*, NV and PV. It has UV and PV values.

    I find the last most likely. If so, asking the question is surely due to a bad design decision. A PV, IV, UV and NV of twenty should all be considered the same value. That's why Perl doesn't provide an interface to provide that information in core (although it's trivial for an XS module to access that information).

    * — It's called "IV", but Devel::Peek printed "UV" because it currently contains a UV.

Re: Is an SV and IV or a PV or an NV or a UV? How can I tell?
by Tux (Abbot) on Oct 24, 2011 at 21:28 UTC

    or? A scalar can hold many of those at the same time:

    $ perl -MDP -wE'DDump Data::Peek::triplevar"\x{03c0}",3,3.14159265' SV = PVNV(0x8760c9c) at 0x817d0a4 REFCNT = 1 FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK,UTF8) IV = 3 NV = 3.14159265 PV = 0x818bdf0 "\317\200"\0 [UTF8 "\x{3c0}"] CUR = 2 LEN = 12 $

    With Data::Peek's DDual () you can get all parts seperate into different scalars:

    $ perl -MDP -wE'DPeek for DDual Data::Peek::triplevar"\x{03c0}",3,3.14 +159265' PV("\317\200"\0) [UTF8 "\x{3c0}"] IV(3) NV(3.14159) SV_UNDEF IV(0)

    FWIW \x{03c0} is the Unicode representation for π (\N{GREEK SMALL LETTER PI} when using use charnames;)

    Enjoy, Have FUN! H.Merijn
Re: Is an SV and IV or a PV or an NV or a UV? How can I tell?
by patcat88 (Deacon) on Oct 25, 2011 at 16:10 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://933450]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2018-10-21 14:12 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (119 votes). Check out past polls.