O'Reilly logo

Learning Perl Student Workbook, 2nd Edition by brian d foy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix B. Answers to Chapter 2 Exercises

Answer 2.1: Although the exact warning message differs between versions of Perl, each of the programs should output a warning. To turn these into a full program, insert the given lines into a short program such as this one then run the program to see the warning:

#!/usr/bin/perl
use warnings;
print;

Program 1 uses the print function, but doesn’t print anything. You should get a warning that complains about an uninitialized value. The warning message also tells you the program name (I called mine test) and the line number of the warning:

Use of uninitialized value in print at test line 3.

Program 2 adds two numbers, but doesn’t do anything with the result. That’s a pretty silly thing. Why do the work if you don’t use the result for anything?

Useless use of a constant in void context at test line 3.

Program 3 adds a number to a variable that has no value then prints the result. The warning message complains about the uninitialized value in the addition. You should also get the result of the print too; that’s the 1 after the warning:

Use of uninitialized value in addition (+) at test line 3.
1

Answer 2.2: You can use the same program you created for the Answer to Exercise 2.1. Program 1 complained about an uninitialized value. With diagnostics on, you get the same warning, but also the entry from the perldiag for that warning. The longer warning tries to explain what you might have done to cause the warning. The entry may not always talk about your ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required