Coding

Perl on your Mac part 2

By Jon Gales


If you missed my last lesson read
it before you read this one. If not you have a good change of being lost. If
you don’t feel 100% about last weeks lesson that is ok, you will after today.
The last lesson was a quick into to the basics of Perl, scalar variables and
the print function. We will be covering them both in more detail as well as
standard input and comments.

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:

print
"WHAT YOU WANT TO PRINT";

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:

$VARIABLE_NAME
= <STDIN>;
chomp($VARIABLE_NAME);

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:

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:

#!/usr/bin/perl
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.

 

Homework:
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)

Intro to Darwinism

By
Jon
Gales

No,
this isn’t going to be a religious or scientific battle about the origin of
our universe. It’s going to be a first look at Darwin, the underlying level
of Mac OS X. Right now you are probably asking yourself why, how, or are confused
as to what Darwin really is. I can give a you a great nutshell answer for each.
Strap on your geek boots, here we go!

 

What:

Darwin
is FreeBSD for the PowerPC processor (G3 and up actually). It is open source
and can be downloaded free of charge from http://darwin.org/projects/Darwin/1.3/release.html
(an Apple site). The download is about 120 megs and the installation can’t
be easier. If you have ever tried to install Linux you will envy Darwin’s
install. It is truly drag and drop (and there’s not even any dragging!). Just
double click, select an empty partition and you are in UNIX. I said before
that Darwin is the underlying level of OS X. Here is what that means:

Aqua


Carbon Classic Cocoa

Open
GL
Quartz QuickTime


DARWIN

As you can
tell, Darwin is on the bottom of our chart; this means it controls all the
base level OS operations. You can access Darwin in the Terminal.app in OS
X (in the Utilities folder). That’s all good but we power users want more.
For that we move to why.

Why:

Why not? Well,
it isn’t user friendly, has no native GUI (X Windows can be installed but
isn’t part of Darwin), can’t run any of your applications, and the list gets
longer. What it does, it does well: all UNIX functions. Aqua is beautiful
but takes RAM and CPU cycles. That is a waste if you are using your box as
a server. OS X has real powerful server tools like Apache built-in, and MySQL
and PHP can be installed making OS X a powerful web server. The same server
can be run from Darwin without the wasted RAM and CPU cycles that Aqua brings.
In fact, Darwin can be your network’s NAT server (for sharing net access).
You can have an older G3 serve as your router/firewall/webserver all without
paying a dime. Even if you don’t want to use those features there is no easier
way to learn UNIX (except using it in the terminal).

How:

Once you run
the installer, go up to the startup disk control panel and select the disk
(or partition) with Darwin on it. If you don’t want to make it the default
disk, just hold Option at startup and select the boot disk at that point (instead
of using the startup disk control panel). After a few minutes you will have
a blinking cursor; type the following:

root

You will get
the following message: "Welcome to Darwin!". This signifies that
you are all logged in and are ready to do anything you choose. Here are the
commands I found most useful:

ls
= list files in current directory
cd
= change
directory (cd / takes you to the main directory)
mkdir
= make directory, creates folder

pico
= opens
up pico (a text editor). A file string followed by pico will open that file
in pico (Ex: pico /files/test.pl will open a file called test.pl located in
the /files directory)

All the OS
X directions in my Perl
tutorial
apply to Darwin (since it is OS X).

To connect
to the internet I edited the /etc/iftab file in pico to read:

en0
inet 192.168.1.102 netmask 255.255.255.0 up
en0 inet -DHCP-

The IP address
is specified by my router so that will change depending on your setup. If
you aren’t on DHCP try following the directions on this site: http://www.excel.net/~clobrien/darwin/Network.html

After I was
on the net I did an FTP transfer. To connect to an FTP server type:

ftp
ftp.servername.TLD
(replace TLD with the ending tld)

It will prompt
you for a username if necessary and then a password. You can use the cd and
ls commands to move around the file tree. Type: get FILENAME to download
files and: put PATH/TO/FILE.txt to upload files. Type ? for
a list of all commands.


That was my
experience with Darwin… I will write another article once I get PHP and
MySQL going (I am having some difficulties forwarding the right ports to my
computer at the moment).

 

If
you are having trouble or would like to ask me a question, please send me mail:
jonknee@macmerc.com

Perl on your Mac part 1

By Jon Gales

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:
  • #!/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 X’ers).


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!