Program 4 • Perl and Rapid Development Solution 1 of 4




  1. Overview


Scripting is a style of programming whereby small programs are developed rapidly. This is also sometimes called rapid prototyping. Perl is a language which supports this particular programming paradigm very well because it is a very powerful and interpreted language. There is no need to do the usual compile-run cycle, since the program is compiled every time it is run.



The make(1) utility determines automatically which pieces of a large program need to be recompiled, and issue the commands to recompile them. This project will also enhance your knowledge of make and Makefiles, as presented in prerequisite courses. Every programmer should have a detailed knowledge of make.


  1. An implementation of a subset of gmake


In this assignment, you will use Perl to write a replacement for a subset of gmake.




pmake — perl implementation of a subset of gmake



pmake [-d] [target]




The pmake utility executes a list of shell commands associated with each target, typically to create or update files of the same name. The Makefile contains entries that describe how to bring a target up to date with respect to those on which it depends, which are called prerequisites.




The following options are supported. All options must precede all operands, and all options are scanned by Getopt::Std::getopts (perldoc).


-d      Displays debugging information. Output is readable only to the imple-mentor.




The following operand is recognized.




An attempt is made to build each target in sequence in the order they are given on the command line. If no target is specified, the first target in the Makefile is built. This is usually, but not necessarily, the target all.




Reads the file called Makefile in the current directory. If the file does not exist, pmake exits with an error message.




  • No errors were detected.


CMPS-112 •  • Program 4 • Perl and Rapid Development                                                                 2 of 4


  • An error in the Makefile was detected. Or if any subprocess returned a non-zero exit status or failed on a signal, and the command was not pre-ceded by the minus sign () marker.




Generally, whitespace delimits words, but in addition, punctuation is recog-nized as well. Each line of input is a comment, an empty line, a dependency, or a command.



  • Any line with begins with a hash, possibly preceded by whitespace (spa-ces and tabs) is ignored. Empty lines consisting only of whitespace are also ignored.


macro = value


Macro definitions are kept in a symbol (hash) table, to be substituted later.


target . . . : prerequisite . . .


Each target’s time stamp is checked against the time stamps of each of the prerequisites. If the target or prerequisite contains a percent sign (%), it is substituted consistently. If any target is obsolete, the following commands are executed. A target is obsolete if it is a file that is older than the prerequisites or does not exist. A prerequisite is either a file or another target. If a file, its time stamp is checked. If not, the target to which is refers is made recursively. No target is made more than once.




A command is any line for which the first character is a tab . The line is echoed to STDOUT before executing the command. The line is then passed to the system function call for execution by the shell. The resulting exit status and signal is then tested. If either is non-zero, pmake exits at that point.


  • command


Behaves like command, except that the command is not echoed to STDOUT before being executed.


  • command


Behaves like command, except that a non-zero exit status or signal does not cause pmake to exit at that point.




Whenever a dollar sign appears in the input file, it represents a macro substi-tution. Macros are substituted from innermost to outermost braces. If a dollar sign is followed by any character except a left brace that one character is the macro name. Otherwise, the characters between the braces constitute the name of the macro.





Represents the dollar sign itself.




Represents the first file specified as a prerequisite.




Represents the first file specified as a target.


CMPS-112 •  • Program 4 • Perl and Rapid Development                                                                 3 of 4


${ . . . }   The contents of the braces are substituted with the value of the macro name, which may be multiple characters, not including a closing brace.



  1. Commentary


Here are some hints that will be useful in familiarizing yourself with Perl and how to perform certain kinds of coding.


  • The directory /afs/ contains examples of Perl scripts. And the subdirectory code/ of this directory contains relevant code.


  • The function system will pass a comment string to the shell and set the vari-able $? to the wait(2) return value. If the termination signal is 0 (bits 6…0), then the program exited normally and bits 15…8 contain the exit(2) status returned by the program. Otherwise, bits 6…0 contain the signal that caused the program to terminate, and bit 7 indicates whether or not core was dumped. The following code can be used to extract this information :


my $term_signal = $? & 0x7F;


my $core_dumped = $? & 0x80;


my $exit_status = ($? >> 8) & 0xFF;


  • A C++ program code/sigtoperl.cpp prints out a description of all of the sig-nals. The output is in code/perlsignals.out This output may be inserted into your Perl program.


  • Use the function system to run the command. $? is the wait(2) exit status. The notation wait(2) refers to the manual page in section 2 of the manual. The command may be read with the command


man -s 2 wait


  • Keep all macros in a hash table.


  • To extract the innermost macro substitution, the following pattern will avoid nested macros : \${[^}]+}. Alternately, you may wish to parse macro lines into an AST matching braces. Remember that regular expressions don’t han-dle matched structures but context free grammars do.


  • Keep each target in a hash with the prerequisites and commands as a refer-ence to a list. Hashes are used in Perl to represent structs. Thus, the follow-ing will point $p at a struct with two fields :


$p = {FOO=> 3, BAR=> [1, 2, 3]}


  • The stat function returns a list of file attributes. The modification time is the value of interest when comparing time stamps on files. See perlfunc(1).


@filestat = stat $filename;


my $mtime = $filestat[9];


(i) Look at the subdirectories .score/test* and see what gmake does with them.


CMPS-112 •  • Program 4 • Perl and Rapid Development                                                                 4 of 4


  1. What to submit


Submit one file, specifically called pmake, which has been chmoded to executable (+x). The first line must be a hashbang for Perl. Also, use strict and warnings. Your name must come AFTER the hashbang line. Grading will be done by naming it as a shell script. Do not run it by typing the word perl as the first word on the command line. The first few lines are :




  • Your name and use strict;


use warnings;


If you are doing pair programming, submit PARTNER as required by the pair program-ming instructions in cmps112-wm/Syllabus/pair-programming.

error: Content is protected !!