Coding
Perl on your Mac part 1
Perl (Practical Extraction and Report Language) is a predominately Unix driven program. There are interpreters for all the major (and minor) OSs 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:
#!/usr/bin/perl
#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
Type 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 Xers).
If you are having trouble or would like to ask me a question please send me mail: jonknee@macmerc.com If not, continue on with Part 2!
All personal comments should be sent to the author. All other discussion should be done in the Forums
[ Back to Coding | Sections Index ]





