Workshop

The workshop section, part of each chapter, has two parts:

  • A Quiz, to make sure you've understood the concepts I covered in this chapter

  • Exercises, so you can work with Perl on your own and gain experience actually using what you've learned.

Answers to both the quiz and exercise questions are shown below.

Quiz

1:What does Perl stand for? What does it mean?
A1: Perl stands for Practical Extraction and Report Language. That means it's a language for extracting things from files and creating reports on those things. The practical part means it's a useful language for these sorts of tasks.
2:Who wrote Perl originally? Who maintains it now?
A2: Larry Wall is the original author of Perl and continues to be intimately involved with its development. Perl is maintained and supported primarily by a group of volunteer developers.
3:What's the most recent version of Perl?
A3: The exact answer to this question will vary depending on the version of Perl you have installed. The major current version, however, is Perl 5.6.
4:What are the basic differences between compiled and interpreted languages? Which one is Perl? Why is this useful?
A4: Compiled languages use a compiler program to convert the program source code into machine code or bytecode. You then run that final version to execute the program. With interpreted languages, however, the source code is the final code, and the interpreter program reads the source file and executes it as is.

Perl is a combination of a compiled and an interpreted language. It behaves like an interpreted language in that it's fast to create, fast to change, and portable across different platforms; but it also compiles the source before running it and, therefore, has the speed and error-correcting features of a compiled language.

5:Which statement best describes Perl?
  • Perl is a small, powerful, tightly defined language with a minimum of constructs to learn and use.

  • Perl is a large, powerful, flexible language with lots of different ways of doing different things.

A5: The second statement best describes Perl:
  • Perl is a large, powerful, flexible language with lots of different ways of doing different things.

6:What's on the http://www.perl.com/ Web site?
A6: The http://www.perl.com/ Web site is the central repository of all things Perl: it's the place to look for the most recent version of Perl, the Comprehensive Perl Archive Network (tools, modules, and utilities relating to Perl), documentation, frequently asked questions, and more information—just about anything you could want that relates to Perl.
7:What's the difference between a Perl one-liner and a Perl script?
A7: A Perl one-liner is a usually short (often one-line) script run directly from the command line (or, on the Mac, run from the One Liner dialog box). A Perl script is usually a longer script contained in a separate file.
8:What do the -w and -e parts of a Perl one-liner do?
A8: The -w option turns on Perl warnings. The -e indicates the next bit of text, inside quotes, is a line of Perl code.
9:What does the shebang line in a Perl script do?
A9: The shebang line is used on Unix to tell Unix which program to execute for a given script. It contains the path name to the Perl interpreter on your platform.

On platforms other than Unix, the shebang line looks just like a regular comment and is usually ignored.

10:How do you create comments in Perl?
A10: Comments in Perl start with a #. Everything from the # to the end of the line is ignored.
11:What are Perl warnings? How do you turn them on for one-liners? For scripts on your platform? Why are they useful?
A11: Perl warnings are special diagnostic messages that can help fix common errors and point out places where you might be doing something that will result in behavior you might not expect. Beginning Perl programmers are advised to turn on warnings as you learn; to help understand how Perl behaves in different unusual situations.

To turn on warnings for Perl one-liners, use the -w option on the Perl command line.

To turn on warnings on Unix, use the -w option in the shebang line.

To turn on warnings on Windows, use the -w option on the Perl command line.

Exercises

1:Try modifying the hello.pl, echo.pl, and cookie.pl scripts in various small ways. Don't get carried away and try to develop an operating system or anything, just try small things.

See what kind of errors you get if you try to introduce various errors into the script (for example, try typing comments without the leading #, removing closing quote marks, or forgetting a semicolon). Become familiar with the sorts of errors that Perl complains about when you forget parts of a statement.

A1: No answers to Exercise 1.
2:Modify the Hello World script to print the greeting twice.
A2: Here's one way to do it:
#!/usr/ bin/perl -w

print "Hello, World!
";
print "Hello, World!
";

3:Modify the Hello World script to print the greeting twice—on the same line.
A3: Here's one way to do it:
#!/usr/ bin/perl -w

print "Hello, World! Hello, World!
";

Here's another way:

print "Hello, World!";
print "Hello, World!
";

4:BUG BUSTER: What's wrong with this script?
!/usr/local/bin/perl -w
print "Hello, World!
";

A4: There's a hash mark missing from the first line of that script. That line will produce an error (or, on Unix, the script probably won't run at all).
5:BUG BUSTER: What's wrong with this one? (Hint: there are two errors)
#!/usr/local/bin/perl -w
print 'Enter your name: '
# save the data $inputline = <STDIN>;
print $inputline;

A5: There are two errors:
  • The first print statement is missing a semicolon at the end of the line.

  • The line just after that starts with a comment—all the text after the hash mark is considered a comment and is ignored.

6:(Extra Credit) Combine the Hello World and Cookie examples so that the script prompts you for your name and then says hello to you, repeatedly, until you type goodbye. Here's some sample output from this script:
Enter your name: Laura
Hello Laura!
Enter your name: Anastasia
Hello Anastasia!
Enter your name: Turok the Almighty
Hello Turok the Almighty!
Enter your name:  goodbye
hello goodbye!
%
									

A6: Here's one way to do it (given what you've learned today):
#!/usr/ bin/perl -w

$name = "";

while ( $name ne 'goodbye') {
  print 'Enter your name: ';
  chomp($name = <STDIN>);
  print "Hello, ";
  print $name;
  print "!
";
}

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

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