HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to PerlOperatorsStringsArrays (@)Hashes (%)Conditions (if)LoopsFunctions (sub)
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
✍️ Text & Number
📋 Array & Hash
🔁 Condition & Loop
🧩 Function (sub)
Perl›Strings

Strings

length, uc, substr, and concatenation with .

Simple enough for a 5-year-old to understand

Length and uppercase

Example code
1
my $s = "hello";
2
print length($s), "\n";
Length: 7
Output / InputLength: 7
3
print uc($s), "\n";
Uppercase: HELLO
Output / InputUppercase: HELLO
Output / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the text "hexlibrary" in UPPERCASE.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

In Perl, strings are held in scalar variables and are denoted by names starting with a dollar sign. Quote selection carries meaning in Perl: inside double quotes, variables are replaced with their values, while inside single quotes, they are written literally.

This distinction is critical in daily use. When writing file paths or regular expressions, single quotes save you from escape clutter; when generating messages, double quotes incorporate variables directly into the text.

String operations are where Perl is at its strongest. There are built-in functions for length, slicing, searching, replacing, and formatting; regular expressions are also embedded directly into the language.

What you will learn

  • The difference between quote types
  • Concatenating text
  • Length and slicing operations
  • Cleaning up line endings
  • Generating formatted output

Who is this for?

  • Those who will process text files
  • Script writers reading user input
  • Developers who will generate report output

When to use?

Every line read from a file carries a newline character at the end; if it is not cleaned before comparison, matches will fail unexpectedly.

In fixed-width report outputs, the formatting function is much safer than manually adding spaces.

Common mistakes

Wrong

my $ad = "Ayse";
print 'Hello $ad';

Inside single quotes, variables are not replaced with their values; the variable name is written to the screen literally along with the dollar sign. If you want the value, you must use double quotes. This is the most common quoting mistake in Perl.

Correct

my $ad = "Ayse";
print "Hello $ad";

Wrong

my $satir = <STDIN>;
if ($satir eq "yes") {
  print "approved\n";
}

The line read from the input carries a newline character at the end, so the comparison never matches and the condition never runs. You don't get an error message; the program silently behaves incorrectly. The end of the read line must be cleaned.

Correct

my $satir = <STDIN>;
chomp($satir);
if ($satir eq "yes") {
  print "approved\n";
}

Wrong

my $metin = "hello";
uc($metin);
print $metin;

The uppercase conversion function returns a new string, it does not change the original; since you didn't assign the result anywhere, the lowercase version is still printed to the screen. Disregarding the return value is a silent error and produces no warning.

Correct

my $metin = "hello";
$metin = uc($metin);
print $metin;

Frequently asked questions

What is the difference between chomp and chop?

chomp only deletes the trailing newline character, otherwise it does nothing. chop, on the other hand, deletes the last character no matter what it is. When reading files and input, chomp is almost always the correct one.

How do I measure string length?

The length function gives the number of characters. For Turkish characters to be counted correctly, you need to set the character encoding of the script properly; otherwise, multi-byte characters are counted as more than one.

How are search and replace performed within a string?

With regular expression operators. The matching operator is used for searching, and the substitution operator is used for replacing. Perl's power in this area comes from regular expressions being a part of the language rather than a separate library.

Summary

Double quotes interpolate variables, single quotes do not.

Concatenation is done with the dot operator.

The end of read lines must be cleaned with chomp.

String functions return a new value.

Regular expressions are embedded into the language.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code