Newline handling in Raku

How the different newline characters are handled, and how to change the behavior

Different operating systems use different characters, or combinations of them, to represent the transition to a new line. Every language has its own set of rules to handle this. Raku has the following ones:

  • \n in a string literal means Unicode codepoint 10.

  • The default nl-out that is appended to a string by say is also \n.

  • On output, when on Windows, the encoder will by default transform a \n into a \r\n when it's going to a file, process, or terminal (it won't do this on a socket, however).

  • On input, on any platform, the decoder will by default normalize \r\n into \n for input from a file, process, or terminal (again, not socket).

  • These above two points together mean that you can - socket programming aside - expect to never see a \r\n inside of your program (this is how things work in numerous other languages too).

You can change the default behavior for a particular handle by setting the :nl-out attribute when you create that handle.

my $crlf-out = open(IO::Special.new('<STDOUT>'), :nl-out("\\\n\r"));
    $*OUT.say: 1;     # OUTPUT: «1␤»
    $crlf-out.say: 1; # OUTPUT: «1\␤␍»

In this example, where we are replicating standard output to a new handle by using IO::Special, we are appending a \ to the end of the string, followed by a newline and a carriage return ; everything we print to that handle will get those characters at the end of the line, as shown.

In regular expressions, \n is defined in terms of the Unicode definition of logical newline. It will match . and also \v, as well as any class that includes whitespace.

See Also

Containers

A low-level explanation of Raku containers

Contexts and contextualizers

What are contexts and how to switch into them

Control flow

Statements used to control the flow of execution

Enumeration

An example using the enum type

Exceptions

Using exceptions in Raku

Functions

Functions and functional programming in Raku

Grammars

Parsing and interpreting text

Hashes and maps

Working with associative arrays/dictionaries/hashes

Input/Output the definitive guide

Correctly use Raku IO

Lists, sequences, and arrays

Positional data constructs

Metaobject protocol (MOP)

Introspection and the Raku object system

Native calling interface

Call into dynamic libraries that follow the C calling convention

Raku native types

Using the types the compiler and hardware make available to you

Numerics

Numeric types available in Raku

Object orientation

Object orientation in Raku

Operators

Common Raku infixes, prefixes, postfixes, and more!

Packages

Organizing and referencing namespaced program elements

Performance

Measuring and improving runtime or compile-time performance

Phasers

Program execution phases and corresponding phaser blocks

Pragmas

Special modules that define certain aspects of the behavior of the code

Quoting constructs

Writing strings and word lists, in Raku

Regexes

Pattern matching against strings

Sets, bags, and mixes

Unordered collections of unique and weighted objects in Raku

Signature literals

A guide to signatures in Raku

Statement prefixes

Prefixes that alter the behavior of a statement or a set of them

Data structures

How Raku deals with data structures and what we can expect from them

Subscripts

Accessing data structure elements by index or key

Syntax

General rules of Raku syntax

System interaction

Working with the underlying operating system and running applications

Date and time functions

Processing date and time in Raku

Traits

Compile-time specification of behavior made easy

Unicode versus ASCII symbols

Unicode symbols and their ASCII equivalents

Unicode

Unicode support in Raku

Variables

Variables in Raku

Independent routines

Routines not defined within any class or role.

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.