Perl Part I · Module 2 · Variables, Scalars, and Data Types
Strings as Scalar Values
The other common thing a scalar holds is text, and Perl treats it with no more ceremony than a number — the same variable, the same assignment, no declared type. What deserves attention is the relationship between the two. The text '42' and the number 42 are different values stored in different ways, and Perl will let you use either one in almost any place the other would work. That convenience is the foundation of the next chapter and the reason this one matters in practice: everything your scripts will eventually read out of a log or a report arrives as text, whatever it looks like.
Foundation12 min readPerlPart IFundamentalsStringsText
Part I · Module 2 · Chapter 2.5 · Strings as Scalar Values
1. Text Is Just a Scalar Value
Nothing new is needed to store text. The same my, the same =, the same scalar:
use strict;
use warnings;
my $name = "Ravi";
my $digits = "42";
print "$name\n";
print $digits + 1, "\n";
print length($digits), "\n";Ravi
43
2Read the last two lines carefully, because together they are the whole chapter.
$digits + 1printed43. The variable holds the text"42", and Perl used it as a number without being asked.length($digits)printed2. As text it is two characters long.lengthis a built-in that counts characters.
So "42" is text that Perl is willing to treat as a number when a number is wanted. That is the behaviour Chapter 2.6 is devoted to; this chapter establishes what text is first.
2. "42" and 42 Are Not the Same Value
They are stored differently — one as a sequence of characters, one as a number — and in most expressions you cannot tell, because Perl converts as needed.
Where you can tell is when you ask a question that only makes sense for one of them. length is the clearest case:
use strict;
use warnings;
my $as_text = "42";
my $as_number = 42;
print length($as_text), "\n";
print length($as_number), "\n";2
2Both report 2 — because to answer "how long is it?" Perl had to treat the number as text, and 42 written out is two characters. That is the conversion happening in the other direction, and it is the symmetry worth noticing: Perl will make text behave like a number, and a number behave like text, whichever the situation calls for.
The practical upshot for now: you rarely have to think about which one you have. The exceptions are real and specific, and they are what Chapters 2.6, 2.8 and 2.13 exist for.
3. Two Kinds of Quote, Briefly
Perl has two ways to write a string literal, and they differ in one respect:
use strict;
use warnings;
my $name = "Ravi";
print "double: $name\n";
print 'single: $name', "\n";double: Ravi
single: $name- Double quotes substitute variable values — the interpolation you have used since Chapter 1.7.
- Single quotes hand Perl the characters exactly as typed.
$nameinside single quotes is a dollar sign followed by four letters.
That is as far as this chapter goes. Quoting has real depth — escape sequences, choosing your own delimiters, when single quotes are the only safe option — and Chapters 4.1 and 4.2 cover it properly. The rule that serves you until then: use double quotes when you want a variable's value in the text, single quotes when you want the characters you typed.
4. Everything Read From the Outside Is Text
This is the section the rest of the course depends on.
When a script reads a line — from a file, from a tool's output, from the input stream you met in Chapter 1.7 — what arrives is text. Always. A log line reporting errors: 12 does not deliver the number twelve; it delivers the characters e, r, r, o, r, s, and so on, and somewhere in there a 1 and a 2.
That has two consequences that will shape every parsing script you write in Part II:
- A field that looks like a number is text until something converts it. Perl's willingness to convert makes this painless most of the time, which is exactly why it is easy to forget.
- A field that is not a number is also text, and looks no different. A column containing
N/A, or-, or nothing at all, arrives in precisely the same form as one containing12. Your script cannot tell them apart by type, because they have the same type.
Chapter 2.13 is about the warning Perl gives you when the second case meets arithmetic, and Chapter 1.11 already showed it:
Argument "N/A" isn't numeric in addition (+) at not-numeric.pl line 5.For now, the habit to start forming is simply to notice when a value came from outside your script, because that is the value whose shape you do not control.
5. Why This Matters in Engineering Work
A simulation log, a timing report, a filelist and a tool's console output are all text files. Nothing a flow script reads is natively a number, which means the conversion from text to number is not an occasional detail — it is one of the two or three things a parsing script actually does.
The engineering risk is specific, and it is worth stating now even though the tools to handle it come later. A tool that normally writes a number in a column will sometimes write something else: N/A when a measurement could not be taken, a dash when a section does not apply, an empty field when a run was cut short. Those arrive as text exactly like the numbers do. A script that adds up the column will convert them all, treat the non-numbers as zero, and produce a total that is both wrong and plausible.
Nothing in this chapter fixes that. What this chapter gives you is the correct expectation: the value is text, its shape is the tool's decision, and checking it is your job. Part II builds the checks.
6. Common Mistakes
Assuming a value is a number because it looks like one. The usual cause is forgetting it came from outside the script. The symptom appears later, when an unexpected N/A reaches arithmetic.
Using single quotes and expecting interpolation. print 'total: $count' prints the dollar sign and the name. The giveaway is a literal $something in your output.
Using double quotes around text that contains a $. The reverse mistake: Perl tries to substitute a variable that may not exist. Under use strict an undeclared name is a compile error, which is a useful outcome — it tells you immediately. Single quotes are the fix.
Expecting length on a number to fail. It does not; it converts and counts characters, as §2 showed. Perl's conversions go both ways.
7. What You Should Be Able to Say Now
- Text is an ordinary scalar value, stored with the same
myand=as a number. "42"and42are different values, stored differently, and usable in most of the same places.- Perl converts in both directions — text used as a number, and a number used as text.
lengthanswers a question about text, which is why it converts a number before counting.- Double quotes interpolate variables; single quotes give you the characters as typed.
- Everything read from a file, a tool or the input stream arrives as text — including fields that are not numbers at all.
8. Exercise
Exercise 1 — both directions. Declare one scalar holding the text "100" and one holding the number 100. Print each one's length, and print each one plus 1. Record all four results and say which of the four involved a conversion.
Exercise 2 — predict the quotes. Without running it, write down exactly what each line prints: print "seed: $seed\n"; and print 'seed: $seed', "\n";, given my $seed = 42;. Then run both.
Exercise 3 — make the strict error happen. Write a line that uses double quotes around text containing a $ followed by a word you never declared. Run it under use strict, record the message, and explain why single quotes would have been the correct choice.
Exercise 4 — reason about a report column. A tool writes a column that usually holds a number but sometimes holds N/A. Both arrive in your script as text. Describe, in two or three sentences and without writing code, what your script would have to check before using that column in arithmetic, and what it should do when the check fails.
9. Summary
- Text is a scalar value — no special declaration, no separate variable kind.
"42"and42are different values that behave the same in most expressions because Perl converts between them.- Conversion works both ways: text used as a number, and a number used as text, as
lengthdemonstrates. - Double quotes interpolate, single quotes do not. Module 4 covers quoting in full.
- Everything read from outside the script is text, including the fields that are not numbers.
- The tool decides the shape of the data, and checking it is your job — the expectation Part II's parsers are built on.
Chapter 2.6 takes the conversion this chapter kept pointing at and makes it the subject: what Perl does when text and numbers meet, and why the convenience has a sharp edge.
Where this fits
Part of the Perl curriculum.
