Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

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

Strings and numbers in Perl are represented by the same data type: scalars. When the JSON module encounters something that looks like a number, it has to guess whether you wanted to output it as a number or a string. To do this it peeks at the scalar's internals (the SV structure - in particular, its flags) to see whether the scalar has ever been treated as a string. If it has, then it outputs it as a quoted string.

Install Devel::Peek and note the difference here:

$ perl -MDevel::Peek -E'my $x = 1; print "$x\n"; print Dump($x)' 1 SV = PVIV(0x864e218) at 0x864d738 REFCNT = 1 FLAGS = (PADMY,IOK,POK,pIOK,pPOK) IV = 1 PV = 0x86483a8 "1"\0 CUR = 1 LEN = 12 $ perl -MDevel::Peek -E'my $x = 1; print $x,"\n"; print Dump($x)' 1 SV = IV(0x86b0724) at 0x86b0728 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 1

In the first example, the variable $x has been interpolated into a string, and thus acquires a POK flag indicating that the SV structure has a pointer to a string. In the second example, although $x has still been printed out, with a new line afterwards, there was no concatenation; no string operator performed on $x; so it doesn't acquire a POK flag. The JSON module would output this as a number.

You can make a variable's POK flag disappear by adding 0 to it:

$ perl -MDevel::Peek -E'my $x = 1; print "$x\n"; $x += 0 ;print Dump($ +x)' 1 SV = PVIV(0x9cba210) at 0x9cb9730 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 1 PV = 0x9cb43a0 "1"\0 CUR = 1 LEN = 12

Notice that the actual string pointer has been kept (that's the PV line), but it is no longer "OK" - it might have been invalidated by the addition, so the POK flag has gone.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re: Peculiar incident: printing a variable changes its type by tobyink
in thread Peculiar incident: printing a variable changes its type by mkhan

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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2018-05-28 10:41 GMT
    Find Nodes?
      Voting Booth?