ComputerSecurityStudent (CSS) [Login] [Join Now]




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

(Perl: Lesson 1)

{ Basic print statement, variable assignment, and stdin }


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. Learn about comments and print statements.
      2. Learn about basic variable string assignments.
      3. Learn about standard input.

  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. Comments and Print Statements - Create lesson1.pl
  1. Create perl script lesson1.pl
    • Instructions:
      1. vi lesson1.pl
      2. Press Enter

     

  2. Creating your second script
    • Instructions:
      1. Press "i" to get into Insert Mode
      2. Highlight and Copy the Below Script
        • #!/usr/bin/perl

          #This is a basic print statement in perl

          #A "#" at the start of a line is use to create comments

          #Notice How the line starts with print and the contents
          #are surrounded by quotation marks.

          #the \n is a hard return

          print "Hello World\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 lesson1.pl
      2. ./lesson1.pl
      3. You should see "Hello World" displayed like below.

 
Section 3. Variable String Assignment - Create lesson1b.pl

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

     

  2. Creating your second script
    • Instructions:
      1. Press "i" to get into Insert Mode
      2. Highlight and Copy the Below Script
        • #!/usr/bin/perl

          #A basic variable in perl starts with a "$" sign

          #To keep the explanation simple, please use quotations
          #around the string that you are assigning to each variable.

          #In your case, change Johnny to your first name, and Gray
          #to your last name.

          $first_name = "Johnny";
          $last_name = "Gray";

          #Notice how we placed variable after the word Hello in
          #the print statement.

          print "Hello $first_name $last_name\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 lesson1b.pl
      2. ./lesson1b.pl
      3. You should see "Hello Johnny Gray" displayed like below.
    • Note:
      • You should change Johnny Gray to your actual name.

 

 
Section 4. Standard Input - Create lesson1c.pl

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

     

  2. Creating your third script
    • Instructions:
      1. Press "i" to get into Insert Mode
      2. Highlight and Copy the Below Script
        • #!/usr/bin/perl

          #Notice the first two print statements to not contain
          #a \n, aka a hard return.

          #Remember that a \n force the cursor to go to the
          #next line

          #What is chomp?
          #chomp in perl will remove a \n character from a variable
          #in this case

          #What is stdin?
          #stdin stands for stdin input.
          #In this case stdin will be your first and your last names.

          print "Enter Your First Name: ";
          chomp($first_name = <stdin>);

          print "Enter Your Last Name: ";
          chomp($last_name = <stdin>);

          print "Hello $first_name $last_name\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 lesson1c.pl
      2. ./lesson1c.pl
    • Note:
      • Please input your actual first name and last name.

 

 
Section 5. Proof of Lab

  1. Proof of Lab
    • Instructions
      1. Create a new program called lesson1d.pl.
        • This program will be very similar to lesson1c.pl
      2. Have lesson1d.pl take in the following standard input variables
        • First Name
        • Last Name
        • Today's Date
          • Note: If you use an apostrophe in a print statement you will have to put a backslash in front of it.  (e.g., \').
        • Email Address
        • (See Expected Input in the below Picture)
      3. Your output should produce FOUR separate print lines
        • (See Expected Output in the below Picture)
      4. Remember to make your program executable by doing the following:
        • chmod 700 lesson1d.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 lesson1d.pl
      4. ./lesson1d.pl
      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