(Perl:
Lesson 2)
{ Basic loops: for,
while, and foreach }
Section 0. Background
Information |
- What is Perl
- Perl is a high-level, general-purpose,
interpreted, dynamic programming language. Perl was originally developed by
Larry Wall in 1987 as a general-purpose Unix scripting language to make
report processing easier. Since then, it has undergone many changes
and revisions and become widely popular amongst programmers. Larry Wall
continues to oversee development of the core language, and its upcoming
version, Perl 6. Perl borrows features from other programming languages
including C, shell scripting (sh), AWK, and sed. The language provides
powerful text processing facilities without the arbitrary data length limits
of many contemporary Unix tools, facilitating easy manipulation of text
files. Perl gained widespread popularity in the late 1990s as a CGI
scripting language, in part due to its parsing abilities.
- Getting Perl
- For the purposes of these perl lesson, I will
be using a perl package that comes standard on Backtrack, Ubuntu and most
flavors of Linux and Unix.
- However, if you are using Windows, instead of a
Linux, Unix or MAC operating system, you still have options.
- Pre-Requisite
-
Lab Notes
- In this lab we will do the following:
- Create a basic For Loop.
- Create a basic While Loop.
- Create a basic Foreach Loop.
- Legal Disclaimer
- As a condition of your use of this Web
site, you warrant to computersecuritystudent.com that you will not use
this Web site for any purpose that is unlawful or
that is prohibited by these terms, conditions, and notices.
- In accordance with UCC § 2-316, this
product is provided with "no warranties, either express or implied." The
information contained is provided "as-is", with "no guarantee of
merchantability."
- In addition, this is a teaching website
that does not condone malicious behavior of
any kind.
- Your are on notice, that continuing
and/or using this lab outside your "own" test environment
is considered malicious and is against the law.
- © 2013 No content replication of any
kind is allowed without express written permission.
Section 1.
Login to BackTrack |
- Login to BackTrack
- Instructions:
- Login: root
- Password:
Whatever you changed it too
-
- Bring up the GNOME
- Instructions:
- Type startx
-
- Bring up a console terminal
- Instructions:
- Click on the Terminal Window
- Become the student user and make a directory
- Instructions:
- su - student
- mkdir -p perl_lessons
- cd perl_lessons
Section 2. Basic For
Loop - Create
lesson2a.pl |
- Create perl script lesson2a.pl
- Instructions
- vi lesson2a.pl
- Press Enter
- Creating your script
- Instructions:
- Press "i" to get into Insert Mode
- Highlight and Copy the Below Script
-
#!/usr/bin/perl
#Basic For Loop
#my $i = 0;
#1) "my" in front of a variable means to treat the variable,
# in this case variable "$i", as a local variable.
#------------------------------------------------------------
#2) $i = 0, means assign $i initially to 0. In other words,
# 0 is the starting value for $i
#$i < 10
#1) This means continue looping while $i is less than 10
#$i++
#1) When a variable is followed by a "++" operator, then the
# variable, in this case $i will be incremented by 1.
print "Basic for loop\n";
for(my $i = 0; $i < 10; $i++)
{
print "Line: $i\n";
}
- Edit --> Paste
- Save Your Work
- Instructions:
- Press the Esc Key
- Type ":wq!"
- Press Enter
- Run the perl script
- Instructions:
- chmod 700 lesson2a.pl
- ./lesson2a.pl
- You should output similar to the below
screen.
Section 3.
Basic While Loop - Create lesson2b.pl |
- Create perl script lesson2b.pl
- Instructions:
- vi lesson2b.pl
- Press Enter
- Creating your script
- Instructions:
- Press "i" to get into Insert Mode
- Highlight and Copy the Below Script
-
#!/usr/bin/perl
#Basic While Loop
#my $i = 0;
#1) "my" in front of a variable means to treat the variable,
# in this case variable "$i", as a local variable.
#------------------------------------------------------------
#2) $i = 0, means assign $i initially to 0. In other words,
# 0 is the starting value for $i
#$i < 10
#1) This means continue looping while $i is less than 10
#$i++
#1) When a variable is followed by a "++" operator, then the
# variable, in this case $i will be incremented by 1.
print "Basic while loop\n";
my $i = 0;
while($i < 10)
{
print "Line: $i\n";
$i++;
}
- Edit --> Paste
- Save Your Work
- Instructions:
- Press the Esc Key
- Type ":wq!"
- Press Enter
- Run the perl script
- Instructions:
- chmod 700 lesson2b.pl
- ./lesson2b.pl
- You should output similar to the below
screen.
Section 4.
Basic Foreach Loop - Create lesson2c.pl |
- Create perl script lesson2c.pl
- Instructions:
- vi lesson2c.pl
- Press Enter
- Creating your script
- Instructions:
- Press "i" to get into Insert Mode
- Highlight and Copy the Below Script
-
#!/usr/bin/perl
#Basic foreach loop
#my @ARRAY = qw(0 1 2 3 4 5 6 7 8 9);
#1) @ARRAY, in perl a word starting with a "@" sign is an array.
#2) We make the @ARRAY variable local
#3) We assign the @ARRAY variable 10 elements, ranging 0 to 9;
#4) qw means that there is white space between array elements.
#foreach my $i (@ARRAY)
#1) foreach, is a type of for loop
#2) So, foreach element in the array, do whatever is in between
# curly braces. In our case, it is the the simple print line.
print "Basic foreach loop\n";
my @ARRAY = qw(0 1 2 3 4 5 6 7 8 9);
foreach my $i (@ARRAY)
{
print "Line: $i\n";
}
- Edit --> Paste
- Save Your Work
- Instructions:
- Press the Esc Key
- Type ":wq!"
- Press Enter
- Run the perl script
- Instructions:
- chmod 700 lesson2c.pl
- ./lesson2c.pl
- You should output similar to the below.
- Follow the below instructions
- Instructions
- Create a new program called lesson2d.pl
- Have the program ask the user "How many
loop iterations"
- Example
- print "How
many loop iterations: ";
chomp($count = <stdin>);
- Assign the above standard input to a
variable called $count.
- Create a for loop that terminates when
the $count variable is reached.
- Example
- for(my $i =
0; $i < $count; $i++)
- Remember to make your program
executable by doing the following:
-
Proof Of Lab Instructions:
- date
- echo "Your Name"
- Put in your actual name in place of
"Your Name"
- e.g., echo "John Gray"
- perl -c lesson2d.pl
- ./lesson2d.pl
- Supply how many loop iterations you
desire.
- Press the <Ctrl> and <Alt> keys at the
same time
- Press the <PrtScn> key
- Past into a word document
- Upload to Moodle
|
 
|