0% found this document useful (0 votes)
84 views

Perl Document

This document provides an overview of the Perl programming language. It discusses how to execute Perl scripts from the command line or text files. It also covers basic Perl syntax including using semicolons or curly braces to end lines of code, and treating lines beginning with # as comments. Additionally, it describes different data types in Perl like scalars, arrays, and hashes as well as syntax for conditional statements and loops.

Uploaded by

argha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Perl Document

This document provides an overview of the Perl programming language. It discusses how to execute Perl scripts from the command line or text files. It also covers basic Perl syntax including using semicolons or curly braces to end lines of code, and treating lines beginning with # as comments. Additionally, it describes different data types in Perl like scalars, arrays, and hashes as well as syntax for conditional statements and loops.

Uploaded by

argha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PERL DOCUMENT

EXECUTING PERL SCRIPT


There are two ways of running a Perl script: at the command line and from a
text file.
1)For very short scripts, you can just type the whole program at the
command line and watch the computer run it.

C:\>perl -e "print 'Hello, World!'"


Hello, World!
2)For longer scripts (and ones you would like to reuse), you create a plaintext document with the program code and then tell the Perl interpreter to
run this program, usually on the command line

#!/usr/bin/perl
print 'Hello, World!';
Then, on the command line:

C:\>perl myprogram.pl
Hello, World!
BASIC PERL SYNTAX
All lines of code in Perl must end with one of two things: a semicolon or
curly braces:
print "Hello.";
sub {print "Hello."}
Any line that begins with the character # is treated by Perl as a comment
and is ignored.Example is below:

# This next line sorts alphabetically

QUOTE MARKS

1) Double quotes
2) Single quotes
3) Backtick (USUALLY USED WITH UNIX COMMAND )
Double quotes allow variable interpolation. That means that this code:
Example:
$name = "Alejna";
print "Hello, $name!\n";
will print this:
Hello, Alejna!

DATA TYPES

1) Scalar variables
2) Arrays
3) Hash
EXAMPLE OF VARIABLES
A scalar is a variable that holds a single value.Scalar names begin with $:

$age = "20"

There are two kinds of variables that hold multiple


pieces of data: arrays and hashes.
An array is a variable that holds multiple values in series. Array names
begin with @:

@names = ("Howard", "Leslie", "Bob");


A hash is a variable that holds pairs of data. Hash
names begin with %:
%traits = ("name" => "Howard",
"age" => "30", "eyes" => "brown");
Conditional Loops Syntax

if ($price > 100) {


print "Too expensive!\n";
}

Ifelse Conditional Syntax


if ($price > 100) {print "expensive"}
elsif ($price < 10) {print "bargain!"}
elsif ($price < 40) {print "cheap"}
Else Conditional Syntax
if ($grade < 60) {
print "Failing grade!: $grade\n";
}
else {print "Grade is $grade.\n"}
Loops
1) While loop syntax
while ($money < 1000) {
$pay = Get_Paycheck ();
$money = $money + $pay;
}
print "Ok, we can pay the rent!\n";
2) Until loop
until ($money < 1000) {
$money = Eat_Out ($money);
}
print "Time to eat noodles!\n";
3) For loop
for ($i = 1; $i <= 100; $i ++) {
print "We've reached $i\n";
}
4) Foreach loop
foreach $grade (@grades) {
if ($grade < 60) {
print "Failing grade!: $grade\n";

$fail_tally ++;
}
}
print "We have $fail_tally failing
students!\n";
EXITING THE LOOP
Sometimes you want to exit the loop via a method other
than the standard one. We do this using the statements
next, redo, and last.
next means "skip over everything else in the block,
increment the counter, and evaluate the conditional again."
redo means "skip over everything else in the block and
evaluate the conditional again, without incrementing the
counter."
last means "exit the block and never come back."
EXAMPLE
foreach $student (@students) {
if ($student eq "END_REGISTERED") {
last;
}
elsif ($student eq "Silber"){
next;
}
else {
$grade = Check_Grade ($student);
}
print "$student: $grade\n";
}
IMPORTANT FUNCTIONS IN PERL
1) SPLIT SYNTAX
$sentence = "Sue and I split up.";

@words = split(/ /, $sentence);


$list = "Eenie, meenie, miney, moe";
@words = split(/,/, $list);
Counting element of an array
$count = @people;
print "$count\n";
2) LENGTH SYNTAX
$string = "abcdefghij";
$howbig = length($string);
3) SORT SYNTAX
*) in terms of characters
@array = ("Betty", "Cathy", "Abby");
@array = sort(@array);
*) in terms of numbers
4) Adding elements to arrays (PUSH SYNTAX)

@numbers = ("210", "450", "333");


$numbers[3] = "990";
OR
push(@numbers, "990");
5)Taking elements from arrays(POP SYNTAX)
@numbers = ("210", "450", "333");
$last = pop(@numbers);
Alternatively, $last = $numbers[2];
But in this case the element [2] will not be removed from the
array while using pop 333 will remove from the array.

You might also like