Perl on your Mac

Get the WebProNews Newsletter:

[ Business]

Perl (Practical Extraction and Report Language) is a predominately Unix driven program. There are interpreters for all the major (and minor) OS’s but to run them locally you have to have Unix, Linux, or Mac OS X. If you want to get the interpreter for Classic it is available for free at http://www.macperl.com. You can still follow what we will be doing, just ignore all the X mumbo jumbo.

Our first program:

Open up the command line (apps>utilities>terminal) and type pico

Type the following in exactly as is:

#Our first application
print "What is your name? ";
$name = <STDIN> ;
print "Hi $name, nice to meet you!";

Now type Control-O and enter test.pl for the file name, press enter

Press Control-X

chmod 755 /Users/YOURUSERNAME/test.pl

You are done just enter the path to the file to run it (/Users/YOURUSERNAME/test.pl) If you are in Classic just enter that same code into MacPerl and press Command-Shift-R.

Recap: The first line (and it has to be the first line) of this script tells the computer that this code is Perl, and where to find Perl. This code gets compiled at run time (unlike C, C++, and an array of other languages) so the computer needs to know where to compile it at every time it is run. You can always find where Perl is by typing where perl. OS X defaults it to /usr/bin/perl (most common place for all systems). What this program does is it prints (to the screen not an actual printer) a string of text. In this case What is your name and then a new line (/n). All strings have to be quoted and end in a semicolon. In fact almost every line in Perl ends in a semicolon and if they don’t need one they will still work with them so when in out add one in. The input gets put into a scalar variable (always marked with a dollar sign before the name) called name. <STDIN> stands for Standard Input. Again we end with a semicolon. The next line prints another string but this time adds in the variable we just created. The chmod changes the permissions of the file. In Unix every file has permissions. You can read more about chmod and permissions here.

In my next article I’ll explain all the ins an outs of scalar variables and arrays. Stay tuned and have fun! We’ll get to some useful stuff soon (at least for all you X’ers).

Scalar variables:

Always marked with a $ before ($scalar_variable)
Can contain A-Z, a-z, 0-9, and underscores in the name and must start with a letter.
Integers and character strings can be assigned as the value
Can be assigned different values in the same script such as:

$one = 1;
$one = 2; #$one now contains 2

Are case sensitive so $A and $a are two different scalars
Can be used in conjunction with each other such as:

$one = 1;
$two = 2;
$answer = $one + $two;

The operators for Scalar’s are as follows:

Add = +
Subtract = –
Divide = /
Multiply = *

To increment a scalar (has to be a number) add ++ or — after it like:

$value = 6;
$value++; #the ++ increases the 6 to a 7
print $value; #will print 7

This is the principal that web counters work on. When a visitor visits the page it triggers a Perl script that reads a number from a file increases it by one prints it to the web and then prints it to file for the next time a hit is occurred.

Print function:

The print function is probably one of the most important things you will need to learn in Perl. Thankfully it is simple. Perl doesn’t actually print things (on your printer) it outputs them on screen. To print something just put the following:


You can print scalars, strings, numbers, or whole files (you will learn this at a later date). Be careful to quote strings of text (Perl will let you get away without quoting scalar variables but not text strings). Also don’t forget that ending semicolon.

Standard Input:

We briefly touched this in our last lesson but not enough to thoroughly understand it. The code for standard input looks like this:


That chomp isn’t always needed but is a good idea because sometimes input can get an extra line in it and we don’t want that. If there is nothing to chomp it does nothing. This code picks up whatever was typed in and dumps it into the $VARIABLE_NAME variable.


Comments are pieces of text that are solely meant for human readers of the code. The computer automatically ignores them. In Perl they are marked by a pound sign #. Everything after a pound sign is ignored. You have to comment every line (there are no multi-line comments in Perl like there are in C, PHP, and many other languages). Commenting is a really really really really (did I make my point?) good idea. Even if you code looks good after your 5th Mountain Dew at 3 a.m. I promise you it won’t look good when your boss is looking over your shoulder at 9 a.m. The first line of every Perl script is a comment but this one isn’t ignored, it tells the comp where to find Perl.

A simple program using: Scalar Variables, Print Function and Standard Input and comments is below:

print "How old are you? "; #prints question
$age = <STDIN> ; #puts response into scalar
chomp($age);#get rid of that new line
$age_days = $age * 365; #times age by number of days in a year
print "You are approximately $age_days days old "; #print answer

If you are in X make the code a text file ending in .pl and chmod it to 755. Then you can run it by typing the path to file in the terminal.

Your homework is to mess with the X terminal. Practice writing stuff in pico and chmoding it and moving around the directory. Here are a few commands to get you started:

ls lists current directory

cd changes directory (cd usr moves you to the usr folder, cd / always moves you back to the root directory).

If you are on Classic you don’t have any homework except to get OS X as soon as you can. It is the future and kicks butt for Perl!

If you are having trouble or would like to ask me a question please send me mail:
jonknee@macmerc.com or chat through AIM: jonknee41 (add to buddy list)

*Originally Published at http://www.MacMerc.com

Jon Gales is a PHP consultant and internet publisher. He writes
Macintosh slanted content for both MacMerc.com and MacDesignOnline.com.
Jon publishes both MobileTracker.net and NewsIsCrappy.com.

Perl on your Mac
Comments Off
Top Rated White Papers and Resources

Comments are closed.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom