Home arrow Perl Programming arrow Scalars and Operators

Scalars and Operators

In this second part of a five-part series on scalars in Perl, you'll learn about operators (both arithmetic and bitwise), among other things. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars and Operators
  2. Converting Between Numbers and Strings
  3. Operators
  4. Bitwise Operators
By: Apress Publishing
Rating: starstarstarstarstar / 1
April 01, 2010

print this article



Alternative Delimiters

That’s all very well, of course, until we want a / in the string. Suppose we want to replace “Slashdot” with “/.”—now we’re back where we started, having to escape things again. Thankfully, Perl allows us to choose our own delimiters so we don’t have to stick with//. Any nonalphanumeric (that is, nonalphabetic and nonnumeric) character can be used as a delimiter, provided it’s the same on both sides of the text. Furthermore, you can use{},[],(), and<>as left and right delimiters. Here’s a few ways of doing theprint qq/.../;, all of which have the same effect:

#!/usr/bin/perl -w
# quotes6.pl

print qq|'"Hi," said Jack. "Have you read /. today?"'\n|;
print qq#'"Hi," said Jack. "Have you read /. today?"'\n#;
print qq('"Hi," said Jack. "Have you read /. today?"'\n);
print qq<'"Hi," said Jack. "Have you read /. today?"'\n>;

We’ll see more of these alternative delimiters when we start working with regular expressions.


There’s one final way of specifying a string—by means of a here-document. This idea was taken from the Unix shell, and works on any platform. Effectively, it means that you can write a large amount of text within your program, and it will be treated as a string provided it is identified correctly. Here’s an example:

#!/usr/bin/perl -w
# heredoc.pl

print <<EOF;

This is a here-document. It starts on the line after the two arrows,
and it ends when the text following the arrows is found at the beginning
of a line, like this:


A here-document must start with<<and then a label. The label can be anything, but is traditionallyEOF (end of file) orEOT(end of text). The label must immediately follow the arrows with no spaces between, unless the same number of spaces precedes the end marker. It ends when the label is found at the beginning of a line. In our case, the semicolon does not form part of the label, because it marks the end of theprint()function call.

By default, a here-document works like a double-quoted string. In order for it to work like a single-quoted string, surround the label in single quotes. This will become important when variable interpolation comes into play, as we’ll see later on.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: