Workshop

The workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try to understand the quiz and exercise answers before you go on to tomorrow's lesson.

Quiz

1:What is a package? Why are packages useful?
A1: Packages are used to, well, package sets of global variables in a single unit, such that those units can be combined without one unit's variables tromping over another's. Packages work best for scripts that are made up of lots of parts that must work in harmony, or for modules of reusable library code.
2:How do you call a variable by its full package name?
A2: The full package name for any variable or subroutine consists of the variable symbol ($ for scalars, @ for arrays, % for hashes, & for subroutine calls), the package name, two colons, and the variable name, for example, $AModule::avariable or &Amodule::asubroutine().
3:Can you use my with global variables? Why would you want to do this?
A3: Declaring global variables with my prevents them from being declared in the main package, which makes them slightly more efficient for value lookup and assignment, as well as making your script more well-behaved should it ever be incorporated into a package or combined with other scripts.
4:What does the line use strict do? Why would you want to use it?
A4: use strict is a special Perl directive that makes sure all the variables in your script are local variables or assigned to a particular package. At this point in your Perl knowledge, it's most useful for catching random global variables and making sure your scripts are self-contained as far as variable declarations go.
5:What are the differences between libraries, modules, packages, and pragmas?
A5: Libraries are collections of Perl code intended to be reused. Libraries can use packages to manage variable names inside that library. A module is a library that uses a package and has the same filename as that package. Modules include code that allows that module to be reused easily in other scripts. A pragma is a special kind of module that affects Perl's operation during both compile time and runtime.
6:What is the CPAN? Where is it?
A6: CPAN stands for Comprehensive Perl Archive Network; it's a collection of user-contributed modules, scripts, documentation, and utilities for use by anyone programming in Perl. CPAN is available at several sites around the world; you can find a site local to you starting at http://www.cpan.org/.
7:What is the @INC array used for?
A7: The @INC array defines the directories in which Perl will look for modules and code imported into your scripts using use.
8:How do you import a module into your script? What does that give you?
A8: Import a module into your script using use with the name of the module and an optional list of variables or import tags. Importing a module gives you access to the variables and subroutines defined by that module.
9:What is an import tag and why would you use it?
A9: An import tag defines a subset of variables and subroutines in the module to be imported into your own script. Import tags are defined by the module developer and documented in the documentation for that module.
10:How do you call a subroutine you've imported from a module? How do you call a subroutine from an object-oriented module?
A10: Subroutines imported from modules can be called just like regular subroutines, using the name of the subroutine with parentheses surrounding any arguments.

In object-oriented modules, you must create a new object before you can call subroutines. With a new object stored in a scalar variable, you'd then call a subroutine using the $var->sub() syntax, where $var is the name of the variable holding the object and sub is the name of the subroutine. Subroutines defined inside objects are called methods.

Exercises

1:Define a subroutine that takes a single argument, a string, and builds a new string with each of the characters separated by another globally defined value (such as “:”). Return the new string. The catch: Use the same variable name for the local variable that holds the new string, and the global variable that stores the separator character. HINT: Don't use use strict or my globals.
A1: The secret is to use a real global, stored in the package main, and then refer to it by its full package name in the body of the subroutine. Note that this doesn't work with global variables defined by my because they do not belong to any package.
#!/usr/local/bin/perl -w

$x = ":";               # separator character (global)
print &splitit("defenestration"), "
";
sub splitit {
    my $string = $_[0];
    my $x = '';                 # new string (global);

    $x = join $main::x, (split //,$string);
    return $x;
}

2:BUG BUSTER: What's wrong with this script?
use This:That;                 # import module
while (<>) {
    print theother($_);
}

A2: There's only one colon in that module name; modules with two names always have two colons.
3:Modify the wrapit.pl script to prompt you for the column width, and then wrap the input text to that width.
A3: Changing wrapit.pl is a simple question of modifying the $columns variable. This variable isn't imported into your script by default, so you'll have to explicitly import it yourself.
#!/usr/local/bin/perl -w
use strict;

use Text::Wrap;                 # import module defaults
use Text::Wrap qw($columns);    # also import $columns
$= "";                        # Paragraph mode
my $indent ="> ";                      # indent character

print 'Enter a width: ';
chomp($columns = <STDIN>);

while (<>) {
    print wrap($indent, $indent, $_);
}

4:Consider the Config module, part of the standard Perl library. This module is used to store configuration information about the current version of Perl. Using the documentation that comes with Config (which you can get to via the perldoc program, the Shuck application in MacPerl, or via the perlmod man page), write a script that prints out the various values available in Config. NOTE: The Config module does not automatically import any subroutine names.
A4:
The subroutine myconfig() will do this for you. Because this
subroutine is not imported by default, you'll have to call it with its full
package name (or import it explicitly):
#!/usr/local/bin/perl -w
use strict;
use Config;

print Config::myconfig();
										

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset