Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

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

Scalars: How They Work (about numbers, text strings, and binary strings)

This tutorial is based on the meditation How scalars work (about numbers, text strings, and binary strings) by Juerd. Copied with full explicit permission.

This is a simplified high level view of relations between Perl scalar types.


                             ┌───────────────┐
                      ┌───── │ REFERENCE     │ ─────┐
                      │      │ (ROK flag on) │      │
                      │      └───────────────┘      │
      numeric context │                             │ string context
                      │                             │
                      ▼                             ▼
┌─────────────────────────┐                     ┌──────────────────────────────┐
│ NUMBER                  │    string context   │ TEXT STRING                  │
│ encoded internally      │ ──────────────────▶ │ (POK flag on)                │
│ as any of:              │                     │ encoded internally           │
│ * integer (IOK flag on) │   numeric context   │ as one of:                   │
│ * double (NOK flag on)  │ ◀────────────────── │ * iso-8859-1 (UTF8 flag off) │
└─────────────────────────┘                     │ * utf8 (UTF8 flag on)        │
                  │     ▲                       └──────────────────────────────┘
                  │     │                         ▲     │           ▲
                  │     │                         │     │           │
             pack │     │ unpack           decode │     │ encode    │
                  │     │                         │     │           │
                  ▼     │                         │     ▼           │ :encoding
                ┌─────────────────────────────────────────┐         │ PerlIO
                │ BINARY STRING                           │         │ layer
                │ (POK flag on)                           │ ◀───────┘
                │ (UTF8 flag off)                         │
                └─────────────────────────────────────────┘
                        ▲  ▲  ▲
                        │  │  │
                        │  │  │
                        ▼  ▼  ▼
                  ┌─────────────────────────────┐
                  │ OUTSIDE PERL                │
                  │ files, sockets, filenames,  │
                  │ environment, system calls   │
                  └─────────────────────────────┘

(A Perl programmer does not have to know about the internal flags ROK, IOK, NOK, POK, and UTF8, but if you're interested, read perlguts.)

Keep text and binary strings/semantics separated! (Good style anyway!)

If you don't keep them separate, and use a binary string as a text string, it is assumed to be iso-8859-1 encoded.

If you don't keep them separate, and use a text string as a binary string, one of the following things happens, with or without warnings:

  1. the internal iso-8859-1 buffer is used (always the case if the internal buffer is not utf8 encoded).
  2. the internal utf8 buffer is used.
  3. the iso-8859-1 encoded version is used:
    1. characters above U+00FF are utf8 encoded, while the rest is iso.
    2. characters above U+00FF are modulo'ed 256.
    3. characters above U+00FF are dropped.
    4. characters above U+00FF cause an exception to be thrown.

If you do keep them separate, and always explicitly convert between the two types by explicitly decoding and encoding or using the :encoding layer on a filehandle, you stay in control of what happens and your program will behave more predictably.

About the diagram:

The diagram is inside an HTML <PRE> element, and so normally will get rendered using a monospace font. Which exact font is used is normally a browser configuration, and depending on your settings, the diagram may not look perfect. In particular, the font selected must have entries for the various graphical characters used in the diagram. If it does not, the browser will select the character from some other – possibly non-monospace – font, thus screwing up the diagram. Feel free to try different monospace fonts until you get results you like. One which is known to work is DejaVu Sans Mono. Also Lucida Console seems to be very close.

See this note about how the diagram was created.


In reply to Scalars: How They Work (about numbers, text strings, and binary strings) by pedagogues

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (10)
    As of 2014-10-01 11:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (9 votes), past polls