4.+Perl+Basics
4.+Perl+Basics
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl)
First Perl Program
Data Types
Operators
Loops
Conditional Statements
Subroutines
Few Points
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 2
Refer the code
Talk about:
First line in the code (also known as the Interpreter line or the “shebang” line)
Tells which interpreter to use (show an example)
Usually located at “C:\” in windows, and at “/usr/bin/” in Unix/Linux
-w
Used to flag warnings. It warns about the potential to misinterpret syntax located in the script.
use strict;
Checks for the definition and the usage of the variables in the script. This stops the execution of the script instead of
just giving warnings.
Show an example of type of Error flagged by this
Ground Rules:
Comments begin with #
All statements end in ;
{} are used to combine statements in to blocks
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 3
Scalars
Single Value, Name preceded by $, assume any value assigned (be it integer, hexadecimal, float, character, string),
scope defined via keyword “my” (“my” is required if we have use strict; in the code)
Names of Scalar - Must not begin with digit, is case-sensitive, and some special names are reserved (like $_ and $1)
Arrays
Multiple Ordered Values, Name preceded by @, can store any number of Ordered Scalars (strings, numbers,
combination), indexed by number, dynamic sizes and values, easy iterations (using for, foreach, etc), scope defined
via keyword “my”
Index of an array starts from 0 and each scalar of an array can be referred via $array[$index]. Negative number
index count from the end.
Discuss the use of , “” .. qw [] in the different examples
Discuss the use of shift, unshift, push, pop
Hashes
Multiple Unordered Scalars organized in key/value pair, Name preceded by %, can store any number of Unordered
Scalars (strings, numbers, combination), indexed by key name, dynamic sizes and values, easy iterations (using for,
foreach, etc), scope defined via keyword “my”
Scalars are stored in random order, search faster as compared to an array
To refer a value of Scalar: $hash{$key}, Value to a key can be assigned directly via $hash{$key} = Value
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 4
Numeric
++, --, +, *, +=, *=, <<, >>
Increment, Decrement, Addition, Multiplication, Assignment, Bitwise
String
., x, .=, x=
Concatenation, Repetition, Assignment
Quoting
qw (one we saw in arrays)
Boolean
>, <, lt, gt, ==, !=, eq, ne, &&, ||, !, and, or, not, ?:, >=, <=, ge, le
List
shift, unshift, push, pop, split, join, sort, reverse, grep
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 5
for
foreach
while
until
while do
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 6
if, elsif, else
unless
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 7
Subroutines are used to group related statements that could together form a task.
Subroutines are declared by “sub” keyword
Subroutines are called via “&”
Arguments can be passed both by value and reference
Subroutines can return values
Nested subroutines (recursion) are possible
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 8
Getting more information on a command:
perldoc, man
Robin Garg, Instructor: Power of Perl (A course covering basic and advanced Perl) 9