Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Hands-On
Perl Assessment
Current as of 1/7/2011
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.
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.
Current as of 1/7/2011
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:
Current as of 1/7/2011
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:
Current as of 1/7/2011
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:
Current as of 1/7/2011
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:
Current as of 1/7/2011
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:
Current as of 1/7/2011
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.
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:
Current as of 1/7/2011
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:
Current as of 1/7/2011
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)
Current as of 1/7/2011
10