Sei sulla pagina 1di 10

Engineering Learning Community

Hands-On
Perl Assessment

Engineering Learning Community


Engineering Training

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercise-Based Learning
Before you get started learning something about JT, JT::Test, and Params, work through this exercise-based
learning assessment to make sure that you have the requisite Perl programming skills needed to
understand and use the Juniper test modules. Use the online resources listed for each exercise to learn
more about the concept illustrated in that exercise. You can also find information about the following Perl
books to identify and study the information you will need to complete the exercises.

"Programming Perl" by Larry Wall, Tom Christiansen, and Jon Orwant. Published by O'Reilly.
"Learning Perl" by Randal L. Schwartz, Tom Phoenix, and brian d foy. Published by O'Reilly.
"Beginning Perl" by James Lee with Simon Cozens and Peter Wainwright. Published by Apress.
"Effective Perl Programming" by Joseph N. Hall, Joshua A. McAdams, and brian d foy. Published by
Addison-Wesley.

We recommend that you attempt to solve the programming exercises in this document before you review
the answers, located at http://matrix.juniper.net/community/training/engineering/systestlearning.

Assumptions and Prerequisites

You should already have your scripting environment installed and configured to your satisfaction.
You can use any editor or IDE that you like, as long as you do not use a word processing program,
like Microsoft Word. Word processing programs typically add control characters that will interfere
with the proper interpretation of your code files.

You should already have Perl installed or have access to the System Test Perl interpreter. Most
UNIX and Linux distributions, as well as Mac OS X, include a Perl installation. If you are running a
Windows machine and you want to install Perl locally, you can download and install the ActiveState
Community version of Perl at:
http://www.activestate.com/activeperl/downloads

You should already have a sandbox environment set up. If you don't, submit a helpdesk.juniper.net
ticket and request a sandbox.

You should know how to use the sandbox and where your team keeps its tools, scripts, etc.

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercises
These exercises are intended to assess your understanding of and ability to employ certain Perl language
constructs and data structures prior to taking the Test Automation course. In the Test Automation course,
you'll learn how to use Juniper's specific System Test Perl modules, JT, JT::Test, and Params.

Exercise 1: Loops
Objective: The objective of this exercise is to demonstrate your understanding of loops and of using
<STDIN> to gather user input.
a. Write a script that adds 3 to a starting value of 0, and prints each value until the loop reaches 36.
b. Next, modify the script to prompt the user, using <STDIN>, for the starting value, the seed number,
and the ending value, and then performs the same task as the previous script.

Resources:
The following resource is specific to Perl control constructs:
http://perldoc.perl.org/perlintro.html#Conditional-and-looping-constructs

Notes:

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercise 2: Arrays
Objective: To demonstrate your mastery of arrays.
a. Write a script and define an array of the days of the week. In this script, accept input from <STDIN>
that allows the user to enter a number from 1 to 7. In response to the input, print the
corresponding day of the week to the screen. Assume the week starts on Monday.
NOTE: Remember that Perl starts counting from 0.
b. Remove a value from the array, using either pop or shift, then add the element back to the array
using either push or unshift

Resources:
The following resources are specific to Perl arrays:
http://perldoc.perl.org/perlfaq4.html#Data:-Arrays
http://perldoc.perl.org/perldata.html
http://perldoc.perl.org/perlfaq7.html#How-can-I-pass/return-a-{Function%2C-FileHandle%2CArray%2C-Hash%2C-Method%2C-Regex}%3F

Notes:

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercise 3: Hashes
Objective: To demonstrate your mastery of hashes
a. Create a hash called "testbeds" and store the names and models of a few routers, switches, firewalls,
or other Juniper devices in the "testbeds" hash. Display the hash contents.
b. Write a script that performs the same task as the Arrays exercise, but uses a hash instead of an array.
Other than creating the hash itself, this can be a short program, maybe only 3 or 4 lines.

Resources:
The following resources explain how to use hashes in Perl:
http://perldoc.perl.org/perlfaq4.html#Data:-Hashes-%28Associative-Arrays%29
http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/
http://www.tizag.com/perlT/perlhashes.php

Notes:

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercise 4: File I/O


Objective: To demonstrate your mastery of file handling.
a. Write a script that creates a file containing the values from your "testbeds" hash from the hash
exercise. Put one testbed name and model number on each line of the file. Close the file.
b. Write a script that opens the file you just created and appends another line to the end of the file, prints
the file to the screen, and closes the file.

Resources:
The following resources explain how to perform file I/O and how to manipulate file data:
http://www.comp.leeds.ac.uk/Perl/filehandling.html
http://www.troubleshooters.com/codecorn/littperl/perlfile.htm
http://www.docdroppers.org/wiki/index.php?title=Perl_File_Handling
http://perldoc.perl.org/perlfaq5.html

Notes:

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercise 5: Regular Expressions


Objective: To demonstrate your facility with regular expressions and with sorting.
a. Write a script that prints all of the testbed models from your testbeds.txt file (from Exercise 5)
without the testbed names (in other words, print out everything from the right side of the hash).
Use a regular expression. Output the results to the screen.
b. Figure out how to sort items and output the sorted items to the screen.

Resources:
The following resources explain how to use Perl regular expressions:
http://perldoc.perl.org/perlre.html
http://perldoc.perl.org/perlretut.html
http://www.addedbytes.com/download/regular-expressions-cheat-sheet-v2/pdf/

Notes:

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercise 6: References
Objective: To demonstrate your facility with references to scalars and arrays.
a. Work with references by performing the following steps:
1.
2.
3.
4.
5.
6.
7.

Create a simple scalar variable.


Create a reference to that variable.
Print the variable and the reference.
Create an array and load it with simple numeric values.
Create an array reference.
Print the array reference.
Add a few values to the array without directly referencing the array.

b.

Resources:
The following resources explain Perl references and how to use them:
http://perldoc.perl.org/perlref.html
http://oreilly.com/catalog/advperl/excerpt/ch01.html

Notes:

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Exercise 7: Subroutines
Objective: To demonstrate your understanding of how to write subroutines in Perl.
a. Write a subroutine/function that generates a random number between 0 and 10 and passes the
result back to a calling program in the same file.
b.

Resources
The following resources describe how to create and use subroutines in Perl:
http://perldoc.perl.org/perlsub.html
http://oreilly.com/catalog/lperl3/chapter/ch04.html
http://www.comp.leeds.ac.uk/Perl/subroutines.html

Notes:

2010 Juniper Networks, Inc.

Current as of 1/7/2011

Engineering Learning Community

Summary
Now that you've finished this assessment, you should be ready to move on to the Test Automation course
covering the Perl-specific Juniper modules (JT, JT::Test, and Params) which you'll use to write tests, connect
to and manage routers, and to gather results of your tests.

Resources
Even if you are an experienced Perl programmer, you will find a number of useful tips on these sites. While
by no means a complete list of Perl resources, the following list is a great set of resources.
Perl.org (http://www.perl.org) All things Perl.
CPAN (http://www.cpan.org) Massive repository of Perl modules.
Eclipse (http://www.eclipse.org) Eclipse IDE.
PerlMonks (http://www.perlmonks.org/?node=Tutorials) Incredibly useful forum.
Larry Wall's Very Own Perl Page (http://www.wall.org/~larry/perl.html) The father of Perl.
EPIC (http://www.epic-ide.org) Perl plug-in for Eclipse.
ActivePerl Community Edition (http://www.activestate.com/activeperl/downloads) ActiveState Perl
distribution for Windows, Linux, and Mac OS X.
Learn Perl (http://learn.perl.org/tutorials/) Nice list of Perl tutorials and presentations.
Perl Quick Reference Card (http://johnbokma.com/perl/perl-quick-reference-card.html) Feel free to
download, but please do not violate the author's requests.
Perldoc.perl.org, List of Tutorials (http://perldoc.perl.org/index-tutorials.html)
Perldoc.perl.org, Beginner's Object-Oriented Perl Tutorial (http://perldoc.perl.org/perlboot.html)
perldoc.perl.org, A Short Tutorial on Perl Debugger (http://perldoc.perl.org/perldebtut.html)
perldoc.perl.org, Regular Expression Quick Start (http://perldoc.perl.org/perlrequick.html)
Perl 5 by Example by David Medinets (http://affy.blogspot.com/p5be/index.htm)
Impatient Perl by Greg London (http://blob.perl.org/books/impatient-perl/iperl.htm)
Stanford's Essential Perl (http://cslibrary.stanford.edu/108/EssentialPerl.html)
The Perl Foundation (http://www.perlfoundation.org/)
Perl.com, An Introduction to Testing (http://www.perl.com/pub/2001/12/04/testing.html)

2010 Juniper Networks, Inc.

Current as of 1/7/2011

10

Potrebbero piacerti anche