3. Variables


1.  Consider the code below. Predict what will be printed.  Assume that
$boiling and $freezing have been set to 212 and 32 respectively.  Test
the code by running it.
(a) $ perl -e '$boiling = 212; print qq{Boiling temperature in Fahrenheit: $boiling\n};'
(b) $ perl -e '$freezing = 32; print q{Freezing temperature in Fahrenheit: $freezing\n};'
(c)  $ perl -e '$freezing = 32; $boiling = 212; print qq{Liquid range for water: in
 Fahrenheit: $freezing - $boiling\n};'
(d) $ perl -e '$freezing = 32; $boiling = 212; print q{range: $boiling - $freezing: }, 
$boiling - $freezing, "\n";'

2.What is the output when this script called vars1.pl is run?
#! /usr/bin/perl
$USAGE = 'USAGE: vars1.pl'

$num1, $num2, $num3;
$num1 = 2;
$num2 = 4; 
$num3 = 8;

($n4, $n5, $n6) = (16, 32, 64);

print '$num1 is: ', $num1, "\n";
print '$num2 is: ', $num2, "\n";
print "\$num3 is: $num3\n";

printf "%8s %8s %8s\n", '$n4', '$n5', '$n6';
printf "%8d %8d %8d\n", $n4, $n5, $n6;
# END OF vars1.pl

3. Write an Address book program called phone.pl that assigns values to 
variables called namesi (1<= i <= 4), addressi (1<= i <= 4), and phonei  
(1<= i <= 4).  Assign values to these variables within the program. 
Output the 4 address book entries (name, address, phone) as a table, 
with headers: name, address, phone.

4. Modify the phone.pl program to accept the information interactively 
input to the program.

5. Modify the phone.pl program to accept the information as a file 
input to the program.

6. Predict the successive values of $val for the following expression sequence:
	$val = 5 * 7 + 3 * 3;
	$val += 10 ** 2;
	$val /= 42 % 15
	
7, Write a program where you place each of the following values into a 
variable and then print the value both in a numeric and a string context 
to observe the results.
	99,999,999
	999999999999
	3.141592654
	9.999999999999999999e+18
	05551212
	0x551212
	0x55FF1212
	0x55FFGG22
	08675309
	
8. Consider the program context1.pl.  What will be the output when this 
program is run?
#! /usr/bin/perl
$USAGE = 'USAGE: context1.pl';

$v1 = 100;
$v2 = 80;
$r1 = $v1 + $v2; print "$r1 \n"; 	
$r2 = $v1 . $v2; print "$r2 \n";

$name = bob;
$r3 = $v1 + $name; print "$r3 \n"; 
$r4 = $v1 . $name; print "$r4 \n";

$number = "800-555-1212";
$r5 = $v1 . $number; print "$r5 \n"; 
$r6 = $v1 + $number; print "$r6 \n";
# END OF context1.pl

9.  Run the context1.pl program using perl -w.  What is the output and what are
the differences between the two ways of running the program?

10. Consider the program svars.pl.  Run this program and observe the results.
#! /usr/bin/perl
$USAGE = 'USAGE: svars.pl';

print "Hello, this file name is: $0\n";
print "My process id is: $$\n";

# run an external command
system ("date");
print "date command yeilded $?\n";

# run a command that should fail
system ("tail bogusfilename"); 
print "tail command yeilded $?\n";

print BOGUSFILEHANDLE "important information\n";
print "Writing to bogus filehandle; Error string is set to [$!]\n";
# END OF svars.pl

What are the meanings of each output line? What are the STDERR outputs?

11. Run the following program called errmesg.pl and observe the output.
#! /usr/bin/perl
$USAGE = 'USAGE: errmesg.pl';

print "This is regular program output\n";
print STDERR "$0: Error - This is simulated error output!\n";
print "The program is designed to produce two lines of output.\n";
#END OF errmesg.pl

12. Run errmesg.pl in the following way: errmesg.pl > junkfile
What is output to the screen. Where happened to the STDOUT?

IF IT DOESN"T WORK.

1. The qx...x and qqx...x are called literal quoting operators applied to 
strings (q for single quotes; qq for double quotes), x can take on any 
(paired) character that is not in the string, such as ( ), { }, [ ], #, /, in 
order to delimit the string to be quoted.

2. For interactive input, use  assigned to a variable and be 
sure to provide instructions  so the user knows what to type in.

3. For string context, concatenate the variable with the null string; For 
numerical context, add 0 to the variable.

4. Like the shell, $0 represents the command word on the command line, 
$$ represents the current process id of the program that is running, 
$? represents the status of the command (0 = successful, non-zero = 
non-sucessful), $! represents the error message that perl provides 
associated with a system or run time error.

Questions about the questions? Send mail to Robert Katz: rkatz@ned.highline.ctc.edu
Last Update January 12, 2002