ComputerSecurityStudent (CSS) [Login] [Join Now]




|UNIX >> Perl Lessons >> Current Page |Views: 13060

(Perl: Lesson 2)

{ Basic loops: for, while, and foreach }


Section 0. Background Information
  1. 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.

  2. 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.
  3. Pre-Requisite
  4. Lab Notes
    • In this lab we will do the following:
      1. Create a basic For Loop.
      2. Create a basic While Loop.
      3. Create a basic Foreach Loop.

  5. 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
  1. Login to BackTrack
    • Instructions:
      1. Login: root
      2. Password: Whatever you changed it too

     

  2. Bring up the GNOME
    • Instructions:
      1. Type startx

     

  3. Bring up a console terminal
    • Instructions:
      1. Click on the Terminal Window

     

  4. Become the student user and make a directory
    • Instructions:
      1. su - student
      2. mkdir -p perl_lessons
      3. cd perl_lessons

     

Section 2. Basic For Loop - Create lesson2a.pl
  1. Create perl script lesson2a.pl
    • Instructions
      1. vi lesson2a.pl
      2. Press Enter

     

  2. Creating your script
    • Instructions:
      1. Press "i" to get into Insert Mode
      2. 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";
          }
      3. Edit --> Paste

     

  3. Save Your Work
    • Instructions:
      1. Press the Esc Key
      2. Type ":wq!"
      3. Press Enter

     

  4. Run the perl script
    • Instructions:
      1. chmod 700 lesson2a.pl
      2. ./lesson2a.pl
      3. You should output similar to the below screen.

 
Section 3. Basic While Loop - Create lesson2b.pl

  1. Create perl script lesson2b.pl
    • Instructions:
      1. vi lesson2b.pl
      2. Press Enter

     

  2. Creating your script
    • Instructions:
      1. Press "i" to get into Insert Mode
      2. 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++;
          }
      3. Edit --> Paste

     

  3. Save Your Work
    • Instructions:
      1. Press the Esc Key
      2. Type ":wq!"
      3. Press Enter

     

  4. Run the perl script
    • Instructions:
      1. chmod 700 lesson2b.pl
      2. ./lesson2b.pl
      3. You should output similar to the below screen.

 

 
Section 4. Basic Foreach Loop - Create lesson2c.pl

  1. Create perl script lesson2c.pl
    • Instructions:
      1. vi lesson2c.pl
      2. Press Enter

     

  2. Creating your script
    • Instructions:
      1. Press "i" to get into Insert Mode
      2. 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";
          }
           
      3. Edit --> Paste

     

  3. Save Your Work
    • Instructions:
      1. Press the Esc Key
      2. Type ":wq!"
      3. Press Enter

     

  4. Run the perl script
    • Instructions:
      1. chmod 700 lesson2c.pl
      2. ./lesson2c.pl
      3. You should output similar to the below.

 

 
Section 5. Proof of Lab

  1. Follow the below instructions
    • Instructions
      1. Create a new program called lesson2d.pl
      2. Have the program ask the user "How many loop iterations"
        • Example
          • print "How many loop iterations: ";
            chomp($count = <stdin>);
      3. Assign the above standard input to a variable called $count.
      4. Create a for loop that terminates when the $count variable is reached.
        • Example
          • for(my $i = 0; $i < $count; $i++)
      5. Remember to make your program executable by doing the following:
        • chmod 700 lesson2d.pl
    • Proof Of Lab Instructions:
      1. date
      2. echo "Your Name"
        • Put in your actual name in place of "Your Name"
        • e.g., echo "John Gray"
      3. perl -c lesson2d.pl
      4. ./lesson2d.pl
        • Supply how many loop iterations you desire.
      5. Press the <Ctrl> and <Alt> keys at the same time
      6. Press the <PrtScn> key
      7. Past into a word document
      8. Upload to Moodle


Help ComputerSecurityStudent
pay for continued research,
resources & bandwidth