Is a widely used programming language developed by Larry Wall in the late 1980's. Since being originally written as a scripting language to assist in system administration tasks, it has taken off as a popular language for doing everything from low-level systems programming to website scripting. If you have ever thought about getting into programming on your Mac, Perl is a great place to start! Preparing for Perl All you will need for this primer is a text editor, the terminal, and Perl (of course!). Luckily for us, Perl comes with OS X 10.7 and previous versions of OS X, even without Developer Tools installed. Here's how to get everything ready:. Open the TextEdit application.
From the Format menu, select Make Plain Text. (If the only option available is Make Rich Text, you don't have to do anything.). Open the Terminal application. (You can find it in Applications → Utilities.) We're good to go!
Your First Perl Command Before we start writing code, let's take a look at the version of Perl we have installed. Hop over to your Terminal window and type perl -v, and press Return. You should see something like this: This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-thread-multi-2level (with 2 registered patches, see perl -V for more detail) Copyright 1987-2010, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using 'man perl' or 'perldoc perl'. If you have access to the Internet, point your browser at, the Perl Home Page. Look at that. You just ran your first Perl command!
Creating a Perl Script Now let's try something a little more fun. Switch over to TextEdit and type the following text exactly how it appears below: #!/usr/bin/perl print 'Hello, Macinstruct user! N'; Save the file as hello.pl right on your desktop. The.pl extension tells Mac OS X that this is a file full of Perl code.
The next part is really important, so make sure you do it correctly! By default, files on your computer are not able to execute code. This is a security feature designed to make sure you are only running code that you explicitly tell the operating system it can run. You need to tell Mac OS X that you want to be able to execute this file. Hop in to the terminal and run the following command: cd /Desktop This command changes the working directory ( cd = 'change directory') to your Desktop. The tilde in front of /Desktop is shorthand for my home folder.
Next, type the following command: chmod +x hello.pl This command adds executable attributes to your file. You are telling Mac OS X, 'Yes, it's safe to execute the code in this file!' To finally run your program, type the following in to the terminal window:./hello.pl You should see the following output: Hello, Macinstruct user! You're on your way to being a real Perl pro! Let's break down what our code did for us. #!/usr/bin/perl This line is telling OS X that when it runs the code, it will use Perl as our interpreter.
Without this line, OS X wouldn't know what programing language to use to run our code. Print 'Hello, Macinstruct user! N'; This line is using the print command to print the Hello. Line back to our screen.
The n at the end is a newline character - a special character sequence that is recognized by the interpreter to change itself into a new line instead of printing the characters n. The last thing on this line is a semicolon - every statement in Perl ends in a semicolon. Dabbling in Arrays OK, let's try something a little more advanced (and fun!). Change hello.pl to look like this: #!/usr/bin/perl print 'Hello, $ARGV0! N'; Save your file and hop back in to the terminal window. We already made the file executable, so we can just run the command.
However, this time we are going to add a command line argument to our command. Run this in your terminal:./hello.pl Rick (Of course, you'll want to use your own name!) Pretty cool, huh? $ARGV0 is a variable saved by Perl that is your first command line argument. When you run this command, $ARGV0 is replaced by your argument - your name - and prints it out. The observant reader may notice something strange here: we used $ARGV0 to get our first command line argument.
Why does the 0 indicate we want the first argument? '0' does not equal '1'! Well, lets get a little more advanced for a moment. When your program runs, Perl stores all of your command line arguments in what is known as an 'array'. An array, in elementary terms, is just a bunch of things all stored together into one piece of data.
These individual things are stored at indexes. $ARGV0 is telling Perl that we want to access the piece of data that is stored at index '0' in our array called '$ARGV'. Array indexes start at 0, so the element at index 0 is the first item in our array. Adding Numbers with Perl With that knowledge, let's try something a little more difficult and a little more useful. Let's make a little program to add two numbers together. You will want to run a command like this:./add.pl 5 10 And your terminal will print: 15 First, a note about quotes.
When you quote variables in your Perl program, you interpolate the variables - you replace the variables with their values. If you want Perl to add a few numbers together, we need to make sure that Perl sees numbers, and not variables, when we write our little addition script - so we aren't going to place our variables in quotes. I'm going to step back and let you try this one. You should create a new file in TextEdit and call it add.pl.
Here are a few things you will need to know:. To add two numbers together in Perl, you just need to use + in between your two variables. If you want to concatenate (put together) additional items in your print statement, you will put a period in between the things you want to put together. For example, you may want to add a new line to your print statement - try this:. ' n' Remember to add a semicolon to the end of each statement, and remember to make your script executable! Solution: #!/usr/bin/perl print $ARGV0 + $ARGV1. ' n'; Related Articles.
Meet Your Macinstructor Richard Myers is an amateur programmer from southern New Jersey. His interests include programming, playing ping-pong, and writing about himself in the third person. He is married and has two old dogs. Contact him at.
Part I: Apache2:: Core API The API provided by this module deals with access, authentication and authorization phases. Apache2::CmdParms provides the Perl API for Apache command parameters object. Apache2::Command provides the Perl API for accessing Apache module command information Apache2::RequestRec provides the Perl API for Apache connection record object. Apache2::ConnectionUtil provides the Apache connection record object utilities API.
This package contains constants specific to Apache features. Apache2::Directive provides the Perl API for manipulating the Apache configuration tree Apache2::Filter provides Perl API for Apache 2.0 filtering framework. Apache2::FilterRec provides an access to the filter record structure. Apache2::HookRun exposes parts of the Apache HTTP protocol implementation, responsible for invoking callbacks for each HTTP Request cycle phase. Apache2::Log provides the Perl API for Apache logging methods. Apache2::MPM provides the Perl API for accessing Apache MPM information. Apache2::Module provides the Perl API for creating and working with Apache modules With.
Sections, it is possible to configure your server entirely in Perl. Apache2::Process provides the API for the Apache process object, which you can retrieve with $s-process: Apache2::RequestIO provides the API to perform IO on the Apache request object. Apache2::RequestRec provides the Perl API for Apache requestrec object. Apache2::RequestUtil provides the Apache request object utilities API. Apache2::Response provides the Apache request object utilities API for dealing with HTTP response generation process. Apache2::ServerRec provides the Perl API for Apache serverrec object.
Apache2::ServerUtil provides the Apache server object utilities API. Apache2::SubProcess provides the Perl API for running and communicating with processes spawned from modperl handlers. Apache2::SubRequest contains API for creating and running of Apache sub-requests.
While APR::URI provides a generic API to dissect, adjust and put together any given URI string, Apache2::URI provides an API specific to Apache, by taking the information directly from the $r object. Therefore when manipulating the URI of the current HTTP request usually methods from both classes are used.
Various Apache utilities that don't fit into any other group. Part II: APR:: Core API On load this modules prepares the APR enviroment (initializes memory pools, data structures, etc.) APR::Base64 provides the access to APR's base64 encoding and decoding API. APR::Brigade allows you to create, manipulate and delete APR bucket brigades. APR::Bucket allows you to create, manipulate and delete APR buckets. APR::BucketAlloc is used for bucket allocation. APR::BucketType allows you to query bucket object type properties. This package contains constants specific to APR features.
APR::Socket provides the Perl interface to APR date manipulating functions. APR::Error handles APR/Apache/modperl exceptions for you, while leaving you in control. APR fileinfo structure provides somewhat similar information to Perl's stat call, but you will want to use this module's API to query an already stat'ed filehandle to avoid an extra system call or to query attributes specific to APR file handles. APR::IpSubnet object represents a range of IP addresses (IPv4/IPv6). A socket connection can be matched against this range to test whether the IP it's coming from is inside or outside of this range. APR::OS provides the Perl interface to platform-specific APR API. APR::PerlIO implements a Perl IO layer using APR's file manipulation API internally.
APR::Pool provides an access to APR pools, which are used for an easy memory management. APR::SockAddr provides an access to a socket address structure fields. APR::Socket provides the Perl interface to APR sockets.
An interface to aprerrno.h composite error codes. APR::String provides strings manipulation API. APR::Table allows its users to manipulate opaque string-content tables.
APR::ThreadMutex interfaces APR thread mutexes. APR::ThreadRWLock interfaces APR thread read/write locks. APR::URI allows you to parse URI strings, manipulate each of the URI elements and deparse them back into URIs. Various APR utilities that don't fit into any other group. APR::UUID is used to get and manipulate random UUIDs.
Part III: ModPerl:: This package contains constants specific to modperl features. ModPerl::Global provides an API to manipulate special perl lists. At the moment only the END blocks list is supported. Modperl 2.0 provides many methods, which reside in various modules. One has to load each of the modules before using the desired methods.
ModPerl::MethodLookup provides the Perl API for finding module names which contain methods in question and other helper functions, to find out out what methods defined by some module, what methods can be called on a given object, etc. ModPerl::MM is a 'subclass' of ExtUtils::MakeMaker for modperl 2.0, to a degree of sub-classability of ExtUtils::MakeMaker. META: document that for now we don't chdir into the script's dir, because it affects the whole process under threads. ModPerl::PerlRunPrefork should be used by those who run only under prefork MPM. URIs in the form of will be compiled as the body of a Perl subroutine and executed. Each child process will compile the subroutine once and store it in memory.
It will recompile it whenever the file (e.g. Test.pl in our example) is updated on disk.
Think of it as an object oriented server with each script implementing a class loaded at runtime. ModPerl::RegistryBB is similar to ModPerl::Registry, but does the bare minimum (mnemonic: BB = Bare Bones) to compile a script file once and run it many times, in order to get the maximum performance. Whereas ModPerl::Registry does various checks, which add a slight overhead to response times. ModPerl::RegistryCooker is used to create flexible and overridable registry modules which emulate modcgi for Perl scripts. The concepts are discussed in the manpage of the following modules: ModPerl::Registry, ModPerl::Registry and ModPerl::RegistryBB. This modules allows compilation of scripts, running under packages derived from ModPerl::RegistryCooker, at server startup.
The script's handler routine is compiled by the parent server, of which children get a copy and thus saves some memory by initially sharing the compiled copy with the parent and saving the overhead of script's compilation on the first request in every httpd instance. ModPerl::Util provides modperl utilities API. Part IV: Helper Modules / Applications Apache2::compat provides modperl 1.0 compatibility layer and can be used to smooth the transition process to modperl 2.0. Apache2::porting helps to port modperl 1.0 code to run under modperl 2.0. It doesn't provide any back-compatibility functionality, however it knows to trap methods calls that are no longer in the modperl 2.0 API and tell what should be used instead if at all. If you attempts to use modperl 2.0 methods without first loading the modules that contain them, it will tell you which modules you need to load.
Finally if your code tries to load modules that no longer exist in modperl 2.0 it'll also tell you what are the modules that should be used instead. Apache2::Reload reloads modules that change on the disk. Apache2::Resource uses the BSD::Resource module, which uses the C function setrlimit to set limits on system resources such as memory and cpu usage. The Apache2::Status module provides some information about the status of the Perl interpreter embedded in the server.
This module is highly platform dependent, please read the section. It also does not work.
Part V: Internal Modules ModPerl::BuildMM is a 'subclass' of ModPerl::MM used for building modperl 2.0. Refer to ModPerl::MM manpage. Part VI: Related Modules Apache::Request, Apache::Cookie, etc.
See or for documentation of the 3rd party Apache:: modules.