Richard's Perl Step 02,
Perl's Building Blocks: Numbers and Strings



- I used "Sams Teach Yourself Perl in 24 Hours" - 3rd Edition as a reference for this page
- Run in windows DOS prompt
perl thisprogram.pl

perl c:\......\thisprogram.pl

Perl is used in so many places because Perl is a glue language. A glue language is used to bind things together.
What Perl is good at is tying elements together. Perl can take your database, convert it into a spreadsheet-ready file, and, during the processing, fix the data if you want. Perl can also take your word processing documents and convert them to HTML for display on the Web.
As a side effect of being a language designed to glue elements together, Perl is very adaptable. It runs under, at last count, about two dozen OS —and probably more. Perl's programming style is very flexible, so you can do the same things in many ways.

Examples:

01_hello.pl - the first program (print and escape character) (text only)

02_variables.pl - declare variables (text only)

03_operators.pl - simple operations (text only)

 

Examples following Perl 24 hours book, Hour 2:

eg001.pl (text only)

eg002.pl (text only)

 

Hours 2 Literals
2 different types of scalar constants called literals: numeric literals and string literals

Numbers

Number

Type of Literal

6

An integer

12.5

A floating point number

15.

Another floating point number

.7320508

Yet another floating-point number

1e10

Scientific notation

6.67E - 33

Scientific notation (e or E is acceptable)

4_294_296

A large number with underscores instead of commas

Do not use a leading zero in front of a number, such as 010. To Perl, a leading zero means that the literal represents an octal number—base 8. Other bases, hex, binary.

Strings
The two primary ways of quoting strings are to use single quotation marks ('') or double quotation marks ("").

Put a backslash character in front of each quotation mark that you want Perl to treat simply as a character, as shown here:
"Then I said to him, \"Go ahead, make my day.\""

The primary difference between double-quoting and single-quoting a string is that single-quoted strings are quite literal; every character in a single-quoted string (except the sequence \') means exactly what is there.
In a double-quoted string, on the other hand, Perl checks to see whether variable names or escape sequences are present and translates them if they are.

Sample escape sequences (also entire list of escape sequences)

 

Sequence

Represents

 

\n

Newline

 

\r

Carriage return

 

\t

Tab

 

\b

Backspace

 

\u

Change next character to uppercase

 

\l

Change next character to lowercase

 

\\

A literal backslash character

 

\'

A literal ' inside of a string surrounded by single-quotation marks ('').

 

\"

A literal " inside of a string surrounded by quotation marks.


Scalar Variables

- indicate a scalar variable with a dollar sign (e.g. $a)
- Other variable types (hashes and arrays) use a different identifier or no identifier at all (filehandles).

Perl has a special variable, $_, whose value is used as a "default" by many operators and functions.
$_="Dark Side of the Moon"; print; # Prints the value of $_, "Dark side..."

Numeric Operators

2 + 3      # 2 plus 3, or 5
5.1 - 2.4  # 5.1 minus 2.4, or 2.7
3 * 12     # 3 times 12 = 36
14 / 2     # 14 divided by 2, or 7
10.2 / 0.3 # 10.2 divided by 0.3, or 34
10 / 3     # always floating-point divide, so 3.3333333...

"hello" . ' ' . "world" # same as 'hello world'
"barney" x (4+1) # is "barney" x 5, or "barneybarneybarneybarneybarney"

When a string value is used where an operator needs a number (say, for multiplication), Perl automatically converts the string to its equivalent numeric value .
"12" * "3" gives the value 36.
"12fred34" * " 3" will give 36

"Z" . 5 * 7 # same as "Z" . 35, or "Z35"


One-Operand (Unary) Operators

Operator

Sample Usage

Result

int

int(5.6234)

Returns the integer portion of its argument (5).

length

length("nose")

Returns the length of its string argument (4).

lc

lc("ME TOO")

Returns its argument shifted to lowercase letters ("me too").

uc

uc("hal 9000")

Returns its argument shifted to uppercase letters ("HAL 9000").

cos

cos(50)

Returns the cosine of 50 in radians (.964966).

rand

rand(5)

Returns a random number from 0 to less than its argument. If the argument is omitted, a number between 0 and 1 is returned.

full list of named operators perlop and perlfun


Increment and Decrement

When the operator is applied to a text string, and the text string starts with an alphabetic character and is followed by alphabetic characters or numbers, this operator becomes magical.

$a="999";
$a++;
print $a; # prints 1000, as you'd expect

$a="c9";
$a++;
print $a; # prints d0. 9+1=10, carry 1 to the c.

$a="zzz";
$a++;
print $a; # prints "aaaa". If it's an alphabetic character, it becomes the next letter in sequence


Angle Operator (<>)

is primarily used for reading and writing files;

print "What size is your shoe?";
$size=<STDIN>;
chomp $size; # <STDIN> also includes the newline character that the user typed by pressing Enter.
print "Your shoe size is $size. Thank you!\n";


More Assignment Operators

$a+=3;
$line.=", at the end"; # ", at the end" is appended to $line
$y*=$x # same as $y=$y*$x
$r%=67; # Divide by 67, put remainder in $r


A Few Words on Strings and Numbers

For the most part, Perl allows you to use numbers and strings interchangeably.

$a=42; # A number
print $a+18; # displays 60.
$b="50";
print $b-10; # Displays 40

$a=42/3;
$a=$a . "Hello"; # Using a number like a string.
print $a # displays "14Hello"

$a="Hello, World!";
print $a+6; # displays the number 6