Strings in Perl

In this tutorial of our Perl Course, we will start working with the famous strings.
Programming language Perl tutorial about strings

Strings eminPerl

Strings are nothing more than a character group, for example:
"Perl"
"Progressive Perl"
"Music 2112"
"Progressive Perl tutorial \n"

The shortest string is empty: ""
The largest is the one that completes the memory of your machine.

Strings always appear within quotes, single or double.

Single quotes

Everything within single quotes will be displayed as it appears in the code except the single quote and the slash \

Run the following script:
#!/usr/bin/perl
print 'Progressive Perl Tutorial \n';
Results: Progressive Perl Tutorial \n

That is, we typed \n and \n appeared, it was not interpreted as a line break.
To print the bar, do: \\
To print a quote, do: \'

The following script displays a slash and a single quotation mark:
#!/usr/bin/perl
print '\\ \'';

Double quotes

This one we already used in our tutorial on Hello, World! and print and say functions.
If we do:
print "Progressive Perl Course! \ n";
It will interpret \n as a line break, instead of printing '\ n' to the screen.

We will use more double quotation marks when working with variables soon in our Perl handout.
Note that:
print 1 + 1;

Results in 2 on the screen.

Already:
print "1 + 1";
Results on screen: 1 + 1

The first is a mathematical operation with numbers, the second example is a string.

Understand the difference well! In the second case, the number 1 and the + operator are interpreted as characters of a string, they are text.

Perl String Operators

The first operator we are going to learn is concatenation: .
Yes, one point.

It serves to concatenate, 'stick', join strings, see:
print "Perl". "Progressive";
(same as "PerlProgressive")

print "Perl". " ". "Progressive";
(same as "Progressive Perl"

print "Perl". "". "Progressive ". "\n";
(same as "Progressive Perl \n")

Another important operator is the repetition: x
(yes, an x)

Everything you use with it repeats itself, for example:
print "Rush" x 3;
The result is: RushRushRush

This even works for numbers:
print 4 x 3;
The result is: 444

No comments:

Post a Comment