Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

by pedagogues
on Feb 11, 2009 at 02:30 UTC ( #742945=perltutorial: print w/ replies, xml ) Need Help??


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
                │ 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.

Comment on Scalars: How They Work (about numbers, text strings, and binary strings)
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-25 11:27 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (160 votes), past polls