$count = 1; $count = $count++; $count++ $count = $count * 2; $count *= 2 |
Numeric < < = == >= > != 2 > 3 3 == 3 2 >= 1 || OR && AND Strings eq ne |
Hash %number = qw( One 1 Two 2 Three 3 }; $numbers{one} gives 1 |
if ( $a =~ /^$String/i ) { print "Yes $String is
in $a\n"; } else
{
print "$String is not in
$a\n";} # Ignores case |
for($i = 1 ; $i < 31; $i++) { $count
*= 2; print "$count "; } |
print "Standard while"; $count = 1; while ($count <= 9 ) {
print "$count\n"; $count++; } |
print "Do until statement"; $count = 1; do { print "$count\n";
$count++; } until ($count >= 9 ); |
last # breaks out of loop next
# skips rest of block without
terminating, or moves the continue redo #
Restart loop |
SOMELABEL:
while (condition){ Things to do; If (other condition) {
Lass SOMELABEL; # Break out of
Block labelled
SOMELABEL
} } |
print "Enter number :>"; $number = <STDIN>; chomp ($number); $number =~ tr/A-Z/a-z/; |
delete $word{“one”}
# Removes the element One 1. |
&a = “This is a test” |
print "Enter :>"; $Input1 = <STDIN>; chomp ($Input1); # remove crlf print "You entered $Input1\n"; |
Reads standard in from command line
#! /usr/bin/perl # add line number to each line of input file $count=0; while (<>) { print "$count $_" ;
$count++; } |
#! /usr/bin/perl # awk and sed type substitution while (<>) {
s/a/@@@/g; print "$_" ; } ./Prog
File |
Create a log file #! /usr/bin/perl -w # open
(LOG, ">>/tmp/logfile.out") || die "Could not open /tmp/logfile.out"; print LOG "This is the first line\n"; open (PWD,"/etc/passwd"); while (<PWD>) { chomp;
print "$_\n"; } |
Select();
# Changes (redirects)
file handle Select(LOG); # All
output to log Select(STDOUT);
# Back to screen |
What Operating system
my $os = $^O; print "Running in: $os\n"; |
$input = <STDIN>; chomp ($input);
# Removes crlf |
if (2 != 3){ print “OK”; }else { Print
“Not OK”; }
if
( try eq try ) {
print "Yes\n"; }else { print "No\n"; } |
=~
# similar to grep
if ( “Try this” =~ /^Tr/
) {
Print “Yes, Tr is contained in the string Try this”; } else {
Print “No, Tr is not contained in the string Try this”; } |
Hash must be included %number
print
“Enter number :>; $number = <STDIN>; Chomp ($number); $number =~ tr/A-Z/a-z/;
if ($words{$number} ) { print “$words{$number}\n; } else { print “Not in list\n”; } |
|
print "Standard until"; $count = 1; until
($count >= 9 ) {
print "$count\n";
$count++; } |
@a = (1,2,3,4,5,6,7,8,9); foreach (reverse @a) { print "$_\n"; } |
|
Unless (this) {that;} = this || that |
if ( $words{$number} ){ print "$words{$number}\n"; } else {
print "Not in list\n"; } |
/^Tr/i # Ignore case |
$_
Is a scratch variable used by perl. Printing
it will return your last var |
printf “%15s %d $10.2f\n”,
$s, $n, $r;
# Print string in a 15 char field
# Print number in a 5 char field
# Print floating point number to 2 decimal
# places in a 10 character field |
/Fred\b/;
# Boundaries matches fred,
elfred but not Fredrick /\bFred/;
# Boundary matches
Fredrick, but not elFred /\bFred\b/;
# Fred only /\b+\b/;
# “y+x” but not “++” or “ + “ /\bFred\B;
# “Fredrick” but not “Fred Flintstone” [0123456789] [0-9]
# [0-9\-]
# 0 to 9 or – [one|two|three]
# one or two or three |
Functions
#! /usr/bin/perl # sub ADD {
my ($times); # sum is now a local variable
local ($value) = 5; # This is a semi private variable, LOOKUP $times=50; return $_[0] * $times; } $times=10; # This will be printed bellowawk; print
ADD(10); print "\n$times\n"; |
#! /usr/bin/perl -w # opendir (ETC, "/etc") || die "Could not open /etc"; while ($name = readdir(ETC)){ print "$name\n"; } closedir (ETC); |
chdir unlink # Delete link(“existing file”,”Linked file”) # Hard link symlink(May exist”,”Link file”) ~ symbolic chmod(0666,”file1M”,”file2”,”fileEtc”
chown(500,35,”file1”.”file2”,”fileETC”); utime rename |
Process
as input
#! /usr/bin/perl -w open (WHO, "who|"); system
("clear"); while (<WHO>) {
print $_; } close WHO; |