Perl Document
Perl Document
#!/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:
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"
$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.";