Perl 6 On XCode
Volume Number: 23 (2007)
Issue Number: 05
Column Tag: Perl
Perl 6 On XCode
Bringing the power of Perl 6
to the XCode environment
by Jose R.C. Cruz
Introduction
First developed in 1987 by Larry Wall et al, Perl has evolved into the scripting language of choice for developing software solutions such as CGI, system administration scripts, and database management tools. It is also one of the first scripting languages designed to be platform-agnostic. Furthermore, it comes with a very extensive library of functions and subroutines, which can also be extended or improved.
This article introduces the next major revision of the Perl language, version 6. It will provide a concise overview of the language, especially in areas where it improves upon its predecessor, Perl 5. It also introduces two open-source projects, Pugs and Parrot, both of which will become the basis of the new Perl 6 runtime system. It will then demonstrate how to use the XCode IDE to port these projects to OS X, as well as how to add support for Perl 6 to the IDE. A copy of this project can be obtained at the following URL: ftp://ftp.mactech.com/src/mactech/volume23_2007/23.05.sit.
Enter Perl 6
A Brief Background History
Perl 6 started out in 2001 as a series of 8 documents known as the Apocalypses. These documents address the current shortcomings of the Perl language, as well as the proposed changes and additions to improve it. Each Apocalypse document is numbered to correspond to a specific chapter from the official Perl book, Programming in Perl, also fondly referred to as "The Camel Book".
A second set of 6 documents, collectively known as the Exegeses, provides a more detailed description of the changes proposed in the Apocalypses. They also provide a number of code examples to illustrate the new syntax and semantics of the Perl 6 language. Later on, a third set of 12 documents, the Synopses, contains the official design specifications of the entire Perl 6 environment. For a more detailed look at these documents, visit the official Perl 6 home page at http://dev.perl.org/perl6.
The fact that the development of Perl 6 started with design specifications is unique in the history of the language. Earlier versions of Perl were developed without the benefit of a single specifications document. In fact, each time discrepancies were found between the official user's documentation and sources, it is often the former that gets updated. Compare this with other modern languages such as FORTRAN or C, whose compilers and interpreters are expected to comply with their respective standards.
The Perl 6 Advantage
Perl 6 is essentially a complete overhaul of the entire Perl language. When completed, it will bring a large number of long-awaited features and improvements to the Perl development community. Some of the more notable ones are as follows:
support for static data types
formalized and consistent parameter lists
strict sigil invariance
more robust object oriented design
improved regex support
simplified and streamlined grammatical syntax
support for preprocessor macros
an actual switch statement
Explaining each Perl 6 feature in great detail is beyond the scope of this article. Furthermore, the specification is still a work-in-progress. Those who are interested in learning more about the language, as well as how to start writing Perl 6 scripts, should consult the book, Perl 6 Essentials, which is listed at the end of this article.
A note to all Perl users: The Perl 6 specification is not designed to be backward compatible to previous versions of Perl. However, it does support the creation of a compatibility layer that will allow the execution of Perl 5.x scripts. The only limitation is that these scripts will be unable to avail themselves of the new language features.
The Perl 6 Projects
Two open-source projects are currently being developed to implement the Perl 6 specification. The first project, the Perl 6 User's Golfing System (Pugs), attempts to implement the Perl 6 language specification as defined in the Synopses. Its end products are two command-line tools: pugs, an interpreter with an interactive shell, and pugscc, a Perl 6 compiler. The latter tool is capable of compiling an existing Perl 6 script into Perl 5, JavaScript, or PIR (more on this later) for execution.
At the time of this article, the latest stable version of Pugs is 6.4.11. The source tarball for this project, together with instructions on how to compile it, can be downloaded at www.pugscode.org.
Unlike most open-source projects, however, Pugs is written in Haskell, which is a pure functional programming language with non-strict semantics. Since GCC does not have built-in support for Haskell, the Glasgow Haskell Compiler (GHC) needs to be installed into OS X in order to compile the Pugs sources. An OS X installer for the latest version of GHC (6.4.1), which also fixes a compatibility bug with GCC 4.0, can be downloaded at <http://haskell.org/ghc/download_ghc_641.html#macosx>.
Furthermore, the hs-plugins library needs to be installed with GHC. This library enables GHC to compile, load, and evaluate Haskell sources at runtime. The OS X installer for this library is available at http://darcs.net/DarcsWiki/ ¬
CategoryBinaries#headc14449f675f1f36c70703538d40e79f056a69bd9
The second open-source project is Parrot. This project attempts to create a register-based software CPU that is designed to run a wide range of languages as efficiently as possible. It uses a multi-layered design that enables it to support both dynamic languages such as Perl or Ruby, as well as static ones such as Java. It also uses just-in-time compilation to improve its execution speed. Once completed, it will become the de facto runtime engine for Perl 6.
Figure 1. The Parrot Runtime Engine.
Figure 1 shows a simplified diagram of the Parrot engine. Here, a parser (usually Pugs) converts a Perl 6 script into an intermediate file format known as a PIR, or Parrot Intermediate Representation. This PIR file is then compiled into assembly code, which is then optimized and translated into byte code for execution by the virtual machine.
At the time of this writing, the latest stable version of the Parrot engine is version 0.4.4. This version contains an initial implementation of a Perl 6 compiler, as well as compilers for APL and Scheme. It adds support for hierarchical class names, tree transformations, and rules/operators precedence. It also has an improved and redesigned grammar engine.
The latest Parrot source tarball, together with instructions on how to compile the project, is available at the Parrot home page located at http://www.parrotcode.org/source.html
Since Parrot requires at least GCC 3.x and Perl 5.6 for compilation, it will require version 10.3 or newer of MacOS X.
Porting Perl 6 with XCode
The XCode Advantage
Like all open-source projects, compiling Parrot and Pugs is essentially a command-line affair. Both projects use the venerable makefile system to configure and control their respective compilation process.
However, with a little work, the XCode development environment can be used to port these open-source projects to OS X. Its user-friendly text editor, together with its well-designed search/replace features, can be quite useful in navigating the plethora of files and scripts that make up these projects. Also, the configuration, compilation, and even the installation processes for these projects can be controlled to a fine degree through the judicious use of the Run Script build phase.
Parrot in XCode
The XCode project for Parrot (Figure 2) is created using the Empty Project template from the New Project Assistant dialog. Its project directory contains all the files and subdirectories obtained from the Parrot source tarball. The project is assigned with three build configurations: Debug, Release, and Install. The first two are standard configurations provided by the project template, while the last one is used by XCode to install the resulting binaries and support files directly into the host OS X system after a successful compile and test run.
The Parrot project has two targets: Parrot Make, which is the default target, and Parrot Clean. The Parrot Clean target is an external target. Its sole function is to reset the entire project to an uncompiled state when the Clean option is chosen from the Build menu. The Parrot Make target, on the other hand, is a shell script target. It contains four Run Script phases, each phase controlling an aspect of the Parrot build process.
Figure 2. The Parrot XCode project.
The Parrot_Config phase first checks the current build configuration by querying the CONFIGURATION setting. It then executes the Perl script, Configure.pl, while passing the desired installation directory using the --prefix option. Once the script has finished, it then checks to see if any errors occurred during configuration, and if at least four of the principal files were generated.
The second phase, Parrot_Compile, starts the compilation process by invoking the make tool. The tool then uses the Makefile generated during the configuration phase. Again, the phase checks for any errors generated during the process.
The next phase, Parrot_Test, runs a series of standardized tests to see if the newly built Parrot engine is working correctly. These test scripts are bundled with the Parrot sources and should not be altered to ensure reliable results.
This phase first queries the PARROT_TEST_MODE setting to determine the type of test that needs to be run. If the setting is set to 1, the test process is started by invoking the make tool with a test option. A setting of 2, on the other hand, will invoke the tool with a fulltest option. Any other setting will cause this phase to skip the test process entirely.
The fourth and final phase, Parrot_Install, controls the installation of Parrot binaries and support files. Available only to the Release and Install configurations, it starts the installation process by invoking the make tool with an install option. The Debug configuration, however, skips this phase entirely, and an unknown configuration will generate an error signal.
The destination directory used by the installation process is the one set in the Parrot_Config phase through the --prefix option. For the Release configuration, the process uses the directory specified by the build setting, INSTALL_PATH. By default, this is set to the value of
$(BUILD_DIR)/Library/Developer/Perl6/parrot
where BUILD_DIR is the location of the build directory with respect to the Parrot.xcodeproj bundle. The Install configuration, on the other hand, uses the one specified by PARROT_PATH_INSTALL. This setting default to
${LOCAL_DEVELOPER_DIR}/Perl6/parrot
where LOCAL_DEVELOPER_DIR is the /Library/Developer directory of the OS X boot volume. In retrospect, this directory proved to be a more appropriate place to install additional developer tools. Most open-source projects usually install their end products in either /user/local or /opt/local. However, unlike those two directories, LOCAL_DEVELOPER_DIR is a public directory and, as such, does not require any sudo privileges to be accessed.
Pugs in XCode
The second XCode project, Perl6 (Figure 3), is also created using the Empty Project template. Its project directory contains all the files and subdirectories that came with the Pugs tarball file. Furthermore, it contains a reference link to the Parrot.xcodeproj bundle. It also shares the same three build configurations as the Parrot XCode project.
Figure 3. The Perl 6 XCode project.
The Perl6 project has four targets. Parrot Only is an aggregate target that directly references Parrot Make from the Parrot XCode project. Selecting this target will start the build process for that project. The target also contains a single Run Script phase, Parrot_Move. The function of this phase is to copy the resulting Parrot binaries and files into the Perl 6 build directory if the current build configuration is set to Release. Why use a Run Script phase as oppose to a Copy Build phase? The Copy Build phase does not provide any means of detecting the current build configuration. Only the Run Script phase has this capability by checking the CONFIGURATION build setting.
The second target, Pugs Clean, is an external target that is used by the project to reset itself back to an uncompiled state. It also invokes the Parrot_Clean target from the Parrot XCode project. Perl 6 is another aggregate target that invokes the two targets, Parrot Only and Pugs Only, in their respective order. This target is used when building and testing both the Parrot engine and the Pugs tools.
The fourth and final target is Pugs Only. Like Parrot Make, this shell script target has four run script phases. The first phase, Pugs_Config, starts the configuration process by running the Perl script, Makefile.pl. It also sets the installation directory for the final product by passing the directory path using the PREFIX option. It then checks for any configuration errors, and to see if a valid Makefile has been created.
The second phase, Parrot_Compile, first checks the build setting, PUGS_OPTIMISED, to determine how to compile the Pugs tool. A setting of 1 will invoke the make tool without any command options. Any other setting will invoke the same tool with an unoptimized option.
The choice of compilation option directly determines the runtime performance of the Pugs tools, as well as the length of the compilation process. An optimized compilation takes a considerable amount of time to complete, but results in binaries that provide fast Perl 6 interpretation and/or compilation. Conversely, an unoptimized compilation has a much shorter completion time. However, it results in binaries with much slower runtime performance. Overall, choosing the appropriate option will depend largely on how the final product is used or distributed.
The third phase, Pugs_Test, runs a series of standard test scripts to ensure that the resulting Pugs tools will perform as designed. It starts the test process by invoking the make tool with a test option. Since Pugs requires the Parrot engine in order to process Perl 6 scripts, this phase will first check to see if the latter is installed at the directory path specified by PARROT_PATH_EXE. Only then will it proceed with the test process.
Be aware that the Pugs test process takes a long time to complete. Unless the host system has enough computing horsepower to spare, this process should either be disabled for debugging purposes, or assigned to a separate system for execution. To disable the test process, set the build setting, PUGS_TEST_MODE, to 0.
The fourth and final phase, Pugs_Install, prepares the installation process for the current build configuration. As with Parrot_Install, this phase is available only for the Release and Install configurations. It is skipped in its entirety by the Debug configuration. Any other configurations, however, will generate an error signal.
The Release configuration uses the path specified by the build setting, INSTALL_PATH, to install the resulting Pugs tools and support files. This setting has the default value of
$(BUILD_DIR)/Library/Developer/Perl6
The Install configuration, on the other hand, uses the one specified by the build setting, PUGS_PATH_INSTALL. This one has the default value of
${LOCAL_DEVELOPER_DIR}/Perl6
Integrating Perl 6 with XCode
The First Perl 6 Script
Once the Perl 6 binaries and support files are installed into the host OS X system using the previous two XCode projects, you can start writing your Perl 6 scripts. If you prefer executing your scripts through the Terminal prompt, make sure to first update the PATH variable by inserting the entries shown in Listing 1 to your .bash_profile file. Use your favorite text editor to update this invisible file, and make sure to restart your Terminal session in order for the changes to take effect.
Listing 1. Updating the .bash_profile file.
#define the Perl6 tools path
#
PERL6_ENV=/Library/Developer/Perl6
#start of PARROT configuration
PERL6_PARROT=${PERL6_ENV}/parrot
export PUGS_EMBED=perl5
PATH=${PERL6_PARROT}/bin:${PERL6_PARROT}/lib:${PATH}
#..end of PARROT configuration
#start of PUGS configuration
PATH=${PERL6_ENV}/usr/bin:${PERL6_ENV}/lib:${PATH}
#..end of PUGS configuration
Then write the script (let us call it hello.p6) using a text editor. A typical script should have at least the following entries.
use v6.0;
say "Hello there, and welcome to Perl 6!"
Notice that the use keyword is used to indicate that the script is to be interpreted or compiled as a Perl 6 script. Then type pugs hello.p6 at the Terminal prompt to run the Perl 6 script. If the installation was successful, and all Perl 6 binaries and support files are properly compiled, Pugs should print the following output to the Terminal window
Hello there, and welcome to Perl 6!
The Perl 6 Project Template
However, you can use XCode to write and execute your Perl 6 scripts without having to start a Terminal session. This approach allows you to make use of the excellent features provided by XCode, such as search and replace, and integrated source-code management. You can also add a standard project template to XCode in order to maintain consistency between projects as well as maximize code reuse.
Figure 4 shows the contents of a basic Perl 6 project template. This template is based on the Empty Project template and it contains a single source file named main.p6. It also contains a reference to the Perl 6 library files, which should be installed in the directory /Library/Perl6. It has a single Shell Script Target named Perl6 Compile, which itself has a single Run Script phase named Run Perl6.
Figure 4. The Perl 6 project template.
Listing 2 shows the basic contents of the main.p6 file. The file starts of with a standard header comment written using POD tokens. POD, or Plain Old Documentation, is markup language used for writing documentation for Perl scripts and modules. It serves the same purpose as HeaderDoc and JavaDoc.
Following the header comments is the Perl keyword, use. The numerical value after this keyword indicates that the entire script itself is a valid Perl 6 script and should be treated as such. A value less than 6 (e.g. 5.8.6) will tell Pugs to run the script in its Perl 5 compatibility layer.
Finally, right after that keyword is the first Perl 6 keyword, say. This will print out the string argument to stdout and append a new line to the output. It works similarly to the traditional Perl 5 statement of
print "Hello there, and welcome to Perl 6!\n";
Listing 2. The main.p6 file.
#!/Library/Developer/usr/bin/pugs
# Description of the file.
#
=pod
=head1 Project
«Name_of_the_project»
=head2 Created by:
«Name_of_the_software_developer»
=head2 Created on:
«Date_when_the_project_was started »
=head2 Copyright:
«Copyright_Year» «Name_of_the_organisation». All rights reserved.
=cut
# basic Perl 6 statement
use v6.0;
say "Hello there, and welcome to Perl 6!";
Figure 5 shows the project settings used by the Perl 6 project template. The setting, PERL6_PATH, points to the Perl 6 directory where Pugs and Parrot are installed. The PATH environmental variable in XCode is then updated to include the locations of the Pugs and Parrot binaries. Finally, the build setting, PERL6_LOG_ERRORS, is used to determine whether or not to write the Perl 6 execution errors to a log file. If set to 1, any error messages generated by the Perl 6 script will be written to the log file, perl6_run.err.
Figure 5. Settings of the Perl 6 project template.
The contents of the Run Script phase, Run Perl6, are shown in Listing 3. The script first checks to see if Parrot, Pugs, and the Perl 6 libraries are correctly installed in the OS X system. It then checks the PERL6_LOG_ERRORS to see if an error log should be prepared before executing the Perl 6 script. Finally, it starts executing the Perl 6 script by passing the file, main.p6, to the pugs tool.
Listing 3. The Run Script phase, Run Perl6.
# Check to see if Perl 6 is installed
#
# checking for Parrot
if [ ! -x ${PERL6_BIN_PARROT}/parrot ];
then
echo
echo "Parrot is not installed on your system."
exit 1
elif [ ! -x ${PERL6_BIN_PUGS}/pugs ];
then
echo
echo "Pugs is not installed on your system."
exit 1
elif [ ! -d ${LIBRARY_SEARCH_PATHS} ];
then
echo
echo "The Perl6 library is not installed on your system."
exit 1
else
# execute the Perl 6 script
#
if [ ${PERL6_LOG_ERRORS} -eq 1 ];
then
# Initialise the error log
#
echo "Log started on:" 1>>perl6_run.err
date 1>>perl6_run.err
# Run the Perl 6 script
#
pugs main.p6 1>>perl6_run.log 2>>perl6_run.err
# Finalise the error log
#
echo "Log ended on:" 1>>perl6_run.err
date 1>>perl6_run.err
# Check the log for errors
if [ -s perl6_run.err ];
then
# check the contents of the Log
#
ERR_CNT=`grep -e "[eE]rror" perl6_run.err | wc -l`
if [ $ERR_CNT -ne 0 ];
then
echo "Errors were generated while running the Perl 6 script."
exit 1
fi
fi
else
# Just run the Perl 6 script
#
pugs main.p6
fi
fi
# Successful execution
echo "The Perl 6 script ran without any problems."
exit 0
To finalize the project template, the .xcodeproj bundle is opened using the Finder and two user-specific files containing the file extensions,.mode1 and .pbxuser, are deleted. Then a TemplateInfo.plist file (Listing 4) is created and added to the bundle. This file is used by XCode to identify and preprocess the project template each time it is selected from the New Project Assistant (Figure 6). Once the template is finished, it is then copied to the appropriate subdirectory within
/Library/Application Support/Apple/Developer Tools/Project Templates/
Listing 4. The TemplateInfo.plist file for the Perl 6 project template.
{
FilesToMacroExpand = (
"main.p6",
);
Description = "This project is used to develop Perl6 scripts in ¬
XCode.";
}
Figure 6. The project template as seen from New Project Assistant.
Concluding Remarks
Perl 6 is the next major revision of the Perl language. It provides a wide variety of new features, as well as long-awaited improvements to the language. It is also the first revision to be designed around an official language specification.
Bringing Perl 6 to OS X is accomplished by using XCode to port the Parrot and Pugs projects to the platform. These two open-source projects form the basis of the entire Perl 6 system. Adding Perl 6 support to XCode is also easily accomplished by creating and adding a project template to the development environment. This template contains the basic placeholder file, build settings, and library references that are necessary for developing and running Perl 6 scripts.
Bibliography and References
Randall, Allison, Dan Sugalki, and Leopold Totsh. Perl 6 Essentials. Sebastopol, California. (c) 2003. O'Reilly Inc.
Wall, Larry, Tom Christiansen, and Jon Orwant. Programming in Perl, Third Edition. Cambridge, Massachusetts. (c) 2000. O'Reilly Inc.
Wikipedia. Perl. In Wikipedia, the free encyclopedia. The Wikipedia Community, 2006 May 10. Online: http://en.wikipedia.org/wiki/Perl.
Wikipedia. Perl 6. In Wikipedia, the free encyclopedia. The Wikipedia Community, 2006 Apr 26. Online: http://en.wikipedia.org/wiki/Perl_6.
JC is a freelance engineering consultant and writer currently residing in North Vancouver, British Columbia. He divides his time between writing technical articles, and teaching origami at his local district's public library. He can be reached at anarakisware@cashette.com.