Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://742945]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-10-18 04:16 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (242 votes). Check out past polls.