Notes: Perl Lab 03
Outline
- Program Arguments
- Conditional Statements
- Comparison Operators
- File Test Operators
- Regular Expression Operators
- Boolean Operators
-
Perl Script Arguments
Perl scripts can take arguments just as any other Unix
commanline program would. The arguments added are stored in the
built-in array names @ARGV
. These may be accessed
within the perl script as any other array would be. Example
below:
|
chomp($ARGV[$#ARGV]);
print "The first argument has a value of \"$ARGV[0]\"\n";
foreach $argument (@ARGV) {
$counter++;
print "Argument $counter is $argument\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_0.pl "Hello World"
The first argument has a value of "Hello World"
Argument 1 is Hello World
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
-
Conditional Statements
To control the execution of section of code, the if
statement can be used. The if statement evaluates a condition
and then executes the "block" if the condition is
"true". A condition is true if it returns a value
that is not 0 (zero), "" (null string). Undefined
variables will be evaluated as "false" as well. A
block is a grouping of statements between braces. An if
stateent can be used to control the execution of a single
statement without a block but the block syntax is more commonly
used. A simple example:
|
$one = "chicken";
$two = "egg";
print "Two is \"egg\"\n" if($two eq 'egg');
if($one eq $two ) {
print "They are equal.\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_1.pl
Two is "egg"
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
Additional conditions can be tested and executed with the
elsif
and else
options:
|
$first = 3;
chomp($second = $ARGV[0]);
if( $first == $second ) {
print "They are equal.\n";
}elsif( $second >= 4 ) {
print "Second is 4 or more.\n";
}else{
print "All are false.\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_2.pl 4
Second is 4 or more.
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_2.pl 2
All are false.
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_2.pl 3
They are equal.
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
-
Comparison Operators
Below is a table of comparison operators that may be used in
perl.
Comparison |
Numeric Operator |
String Operator |
equal |
== |
eq |
not equal |
!= |
ne |
less than |
< |
lt |
greater than |
> |
gt |
less than or equal |
<= |
le |
-
File Test Operators
Perl allows you to test whether a file or directory exists or
has specific attributes (such as a specific permission
attribute). Below are a few of the more commonly used tests:
Test Operator |
Description |
-e | Returns "true" if the file exists. |
-r | Returns "true" if the file is readable. |
-w | Returns "true" if the file is writeable. |
-x | Returns "true" if the file is executable. |
-d | Returns "true" if the file is a
directory. |
-f | Returns "true" if the file is a
regular file. |
There are several other tests which can be used, see man
perlfunc
and look under the "-X" section for
details.
|
$first_file = '/etc/fstab';
$second_file = '/usr/share';
if( -d $second_file ) {
print "$second_file is a directory\n";
}
if( -r $first_file ) {
print "$first_file can be read\n";
}else{
print "Cannot read file $first_file.\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_3.pl
/usr/share is a directory
/etc/fstab can be read
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
-
Regular Expression Operators
Perl has the built in ability to use "regurlar
expressions" as a method of matching strings. Regular
expressions are used by many Unix utilities and text editors.
In perl, a regular expression comparison is specified by the
=~
operator. The expression itself will be placed
between slashes "//". A simple example follows:
|
print "Enter your name:\n";
chomp($input = <STDIN>);
if( $input =~ /ar/ ) {
print "Your name comtains the substring \"ar\"\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_4.pl
Enter your name:
Mark
Your name comtains the substring "ar"
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_4.pl
Enter your name:
Tucker
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
Regular expressions can be negated with the !~
operator.
|
print "Enter your name:\n";
chomp($input = <STDIN>);
if( $input !~ /X/ ) {
print "Your name does not comtain the letter \"X\"\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_5.pl
Enter your name:
Mark
Your name does not comtain the letter "X"
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
Regular expressions allow you to specify whether a string begins
or ends with a certain patter. The ^ symbol is used to specify
if a string begins with the pattern and the $ symbol is used to
specify the ending pattern. Example:
|
print "Enter your name:\n";
chomp($input = <STDIN>);
if( $input =~ /e$/ ) {
print "Your name ends with the letter \"e\"\n";
}elsif( $input =~ /^Ma/ ) {
print "Your name begins with the letters \"Ma\"\n";
}else{
print "The strings do not match\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_6.pl
Enter your name:
Mark
Your name begins with the letters "Ma"
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_6.pl
Enter your name:
Elephant
The strings do not match
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_6.pl
Enter your name:
Eeeee
Your name ends with the letter "e"
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
There are several special escaped character values that can
match specific string types:
Expression | Matches... |
/\w/ | Matches any alphanumeric and "_" characters |
/\d/ | Matchs any digit character [0-9] in the string |
/\D/ | Matches any non-digit character in the string |
An example of testing for the existance of a digit in a string:
|
print "Enter a word:\n";
chomp($input = <STDIN>);
if( $input =~ /\d/ ) {
print "The string \"$input\" contains a digit\n";
}else{
print "The string \"$input\" does not contains a digit\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_7.pl
Enter a word:
grep
The string "grep" does not contains a digit
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_7.pl
Enter a word:
aardvark 1275
The string "aardvark 1275" contains a digit
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
More detailed information and examples can be found in the Perl
regular expressions man page, man perlre
.
-
Boolean Operators
Conditional statments can make their tests based on multiple
conditions using boolean operators. The operators are listed
below:
symbolic operator |
text operator |
Function |
&& |
and |
Returns "true" if both conditions are true. |
|| |
or |
Returns "true" if either of the conditions are true. |
! |
not |
Returns "true" if the condition is false. |
|
print "Enter a word:\n";
chomp($input = <STDIN>);
if(( $input =~ /e$/ ) && ( $input =~ /^A/)) {
print "Yes! It matches\n";
}
print "Enter a number less than 10:\n";
chomp($input = <STDIN>);
if(( $input !~ /\d/) || ( $input >= 10)) {
print "Invalid input\n";
}
exit;
|
When executed, the script above produces the following output:
|
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_8.pl
Enter a word:
anteater
Enter a number less than 10:
4
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03> ./lab03_8.pl
Enter a word:
Ante
Yes! It matches
Enter a number less than 10:
awk
Invalid input
tuckerm@platypus:/mnt/homes/CLASSES/MET4720/LAB03>
|
last updated: 18 Mar 2012 13:16