2. Comments, Arguments, Environment


1.  Write a script called multicomment.pl in your home directory with the following content:
#!/usr/bin/perl -n
#	$0:	Scans a file for entries from California or Texas
#
#		This program uses "perl -n" mode to scan a file and print the appropriate lines
#		We will print lines for California addresses intact, and print lines for Texas
#		addresses after changing them to reflect the new name.
#
#	print lines that contain the string "CA"
/CA/ && print;
s/TX/Republic of Texas/g && print;	# help Texas secede
#s/, TX$/, Estado de tejas/g && print	# change language
# END OF multicomment.pl

2. Create a file called Statefile containing the following data:
$ cat statefile
1234 University Avenue Berkeley, CA 94567
31 Avenue A Corpus Christi, TX 80444
123 5th Avenue Seattle, WA  98101

3. Run the multicomment.pl program using Statefile as its argument.  
(a) Identify the lines that are a comment block in the program, (b) lines that are incidental 
comments, and  (c) lines that are structural comments.

4. (a) Which line starts with a # character but is not a comment?  
(b) What role does $0 play on the 2nd line?

5.  Perl provides for Plain Old Documentation (POD).  This mechanism allows you to 
embed special comments in your program that can be post-processed to turn it into various
kinds of documentation.  pod2html lets you convert the comments into a web page; 
pod2man can do the same thing to produce a man page for your program.  Every 
POD directive starts with an = sign at the beginning of the line (and preceded by a blank line).
Perl sees this = sign and that and all lines thereafter are treated as POD documentation until 
perl sees =cut on a line by itself (preceded by a blank line).

6. Write a script called deadblock.pl in your home directory with the following content:
#!/usr/bin/perl

#	$0:  program demonstates the use of fake POD markup
#			to comment out a block of code
#

print "This is a normal section of the code...\n";

=debuggingNow

print "This is code in the dead block.\n";
print "This is more code in the dead block.\n";
print "This is the last line of code in the dead block.\n";

=cut

print "This is code after the dead block.\n";
# END OF deadblock.pl

7. (a) When you run deadblock.pl, what is output?  
(b) What happens if you move the =debuggingNow line (and the before and after blank lines) 
down to go after the print "This is code in the dead block. \n"?  
(c) What if you move it to be after print "This is more code in the dead block. \n" 
with no blank lines?

8. Write a script called helloarg.pl in your home directory with the following content:
#!/usr/bin/perl

print "Program Name: $0\n";
print "1st Argument: ", shift, "\n";
print "2nd Argument: ", shift, "\n";
print "3rd Argument: ", shift, "\n";

# END OF helloarg.pl

9. (a) When you run the following instance of the program, what is output?
$ helloarg.pl Larry Moe Curly
(b) What is the function of the shift command?
(c) What is the first line of output if you execute the full path to the script e.g.
$ $HOME/helloarg.pl Larry Moe Curly
(d) What is the output when the following is executed?
$ ./helloarg.pl -v "Robert Johnson" --
(e) What is the output when this program runs with a single argument?

10. Environmental variables can be accessed from within a perl program using the 
perl hash: $ENV{'shellvariablename'}   Write a script called hellouserid.pl as follows:
#!/usr/bin/perl

print "Hello ", $ENV{'LOGNAME'}, "\n";
# END OF hellouserid.pl

11. What is the output from this script?

12. At the shell prompt, execute the following command:
$	export USER="Your Full Name"

13. Write a short program based on hellouserid.pl that greets the name represented by
$USER.

14. Modify the program written in 13. that offers a greeting from a name given on
the command line to the name held in the $USER environment variable.

IF IT DOESN"T WORK.

1. Make sure to faithfully copy and paste the scripts above into a  vi editing session.

2. Incidental Comments are short descriptions of one or more code lines.  Structural Comments
are commands that were commented out.  The Kernel directive #! is not a comment when it
is the first 2 characters of the 1st line of the file.

3. The variable $0 represents the 1st word (command name) on the commandline.  The
shift function shifts the contents of $n into ${n-1}, where n >= 1.

4. One way of writing hellouser.pl is:
	#! /usr/bin/perl
	USAGE="Usage: hellouser.pl"
	# insure that $USER is defined
	print "Hello ", $ENV{'USER'}, "\n";
	# END OF helluser.pl
	
5. One way of writing hellouser+.pl is:
	#! /usr/bin/perl
	USAGE="Usage: hellouser+.pl"
	# insure that $USER is defined
	print "Hello ", $ENV{'USER'}, "from ", shift, "\n";
	# END OF helluser+.pl

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