step 1: install a perl interpreter (the perl “engine ... · usage. if so, then the perl...

6
Page 1 of 6 Setting Up Perl for Mac OS X (as of June 17, 2010) Step 1: Install a Perl Interpreter (the Perl “Engine”) Mac OS X is based on a UNIX operating system and thus comes with a PERL interpreter already installed! If your computer is running Mac OS X or above, you’re done with Step 1! You can move on to Step 2. Step 2: Install a Perl Programming Environment NOTE: TextWrangler 3.1 requires Mac OS X 10.4 or later. They still provide TextWrangler 2.1.3 for customers who have not yet updated to Mac OS X 10.4 and are running Mac OS X 10.3. We will guide you through installing TextWrangler™, a free environment provided by BareBones, the makers of BBEdit®. TextWrangler™ is a “lighter” version of BBEdit®, the industry standard Perl programming environment for Mac OS X. 1. Open up a web browser of your choice and a. Google using the keywords BareBones TextWrangler +Download -OR- b. Go to http://www.barebones.com/products/textwrangler/download.html 2. Click on Download to download the latest version of TextWrangler™. You may be asked whether you would like to open or save Text Wrangler. Choose save, and save it to the Desktop. You also may (briefly) see a progress bar while the download is taking place.

Upload: others

Post on 20-May-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Step 1: Install a Perl Interpreter (the Perl “Engine ... · usage. If so, then the Perl interpreter will “run” (execute) your Perl program. If all goes well, you should see

Page 1 of 6

Setting Up Perl for Mac OS X (as of June 17, 2010)

Step 1: Install a Perl Interpreter (the Perl “Engine”) Mac OS X is based on a UNIX operating system and thus comes with a PERL interpreter already installed! If your computer is running Mac OS X or above, you’re done with Step 1! You can move on to Step 2.

Step 2: Install a Perl Programming Environment NOTE: TextWrangler 3.1 requires Mac OS X 10.4 or later. They still provide TextWrangler 2.1.3 for customers who have not yet updated to Mac OS X 10.4 and are running Mac OS X 10.3. We will guide you through installing TextWrangler™, a free environment provided by BareBones, the makers of BBEdit®. TextWrangler™ is a “lighter” version of BBEdit®, the industry standard Perl programming environment for Mac OS X.

1. Open up a web browser of your choice and a. Google using the keywords BareBones TextWrangler +Download

-OR-

b. Go to http://www.barebones.com/products/textwrangler/download.html

2. Click on Download to download the latest version of TextWrangler™. You may be

asked whether you would like to open or save Text Wrangler. Choose save, and save it to the Desktop. You also may (briefly) see a progress bar while the download is taking place.

Page 2: Step 1: Install a Perl Interpreter (the Perl “Engine ... · usage. If so, then the Perl interpreter will “run” (execute) your Perl program. If all goes well, you should see

Page 2 of 6

3. Once the download is completed, if you see the Bare Bones window on your screen, proceed to Step 4.

If you do not see the Bare Bones window, once the download is completed you should see the TextWrangler_x.y.dmg icon on your screen. Double-click this icon. You should then see the TextWrangler x.y icon on your desktop.

Double-click the TextWrangler x.y icon and proceed to Step 4.

4. Drag the TextWrangler™ icon from the Bare Bones window to your Applications folder (follow the screen shot above). (Do not drag either the TextWrangler_x.y.dmg icon or the TextWrangler x.y installation disc icon to your Applications folder). You can find the Applications folder by opening the hard drive on your Desktop (your hard drive icon will be titled Macintosh HD, unless you have changed the name).

5. You may (briefly) see a progress bar while the copy/install is taking place. After dragging the icon over, you will see it in your Applications folder:

6. Once completed, confirm the installation by finding the TextWrangler™ Icon in your Applications folder (you opened this folder in step 4).

7. You should clean up your Desktop: put the TextWrangler_ x.y.dmg icon and the TextWrangler x.y installation disc icon in the Trash.

8. Assuming you have found the TextWrangler™ icon in your Applications folder, click,

hold, and drag the icon to the dock on your Desktop. Now all you need to do to launch TextWrangler™ is click on the TextWrangler™ icon in your dock.

Continue to the next page for help with testing your Perl programming

environment.

Page 3: Step 1: Install a Perl Interpreter (the Perl “Engine ... · usage. If so, then the Perl interpreter will “run” (execute) your Perl program. If all goes well, you should see

Page 3 of 6

Step 3: Test your Perl Programming Environment

9. Click on the TextWrangler™ icon.

10. Once TextWrangler™ has started, your screen should look like the following.

11. TextWrangler™ assumes you want to start with a new file. Of course, like most applications, you can always use the FILE menu to Open an existing file or create a New file also.

12. Enter the following Perl program exactly as shown into the left-hand side text window.

#!/usr/bin/perl print "hello DNA Land";

13. Using the File menu, Save your Perl program in a file called: hello.pl Notice the file extension .pl -- Perl programs by convention end with .pl. Once saved, notice that TextWrangler™ color-codes your Perl.

Your Perl program will be entered in this portion of the screen.

The “RUN” menu

Page 4: Step 1: Install a Perl Interpreter (the Perl “Engine ... · usage. If so, then the Perl interpreter will “run” (execute) your Perl program. If all goes well, you should see

Page 4 of 6

14. Using the #! menu (pronounced “hash-bang” or “she-bang”), select Run. This asks TextWrangler™ to call on the Perl interpreter to check that your Perl has correct syntax usage. If so, then the Perl interpreter will “run” (execute) your Perl program. If all goes well, you should see the following output window on your screen. Congratulations, you are ready to learn Perl!

Note: TextWrangler™ does not recognize input (data) files as you might think. If you are running a Perl script such as regex.pl inside a folder that also contains an input file such as big_english.txt and your Perl script wants to open that input file, TextWrangler™ needs some help. For example, in the code for the Ch. 4 Regex Play material (from the text Perl for Exploring DNA), this code needs two lines to be uncommented. In the regex.pl code, you need to uncomment the two lines for determining relative directory paths. You need these lines with TextWrangler™.

my $filename = "big_english.txt"; # : # TextWrangler needs these next two lines to find the input file # so remove any leading comment symbol (#) from both lines my @path = File::Spec->splitdir( File::Spec->rel2abs($0) ); $filename = File::Spec->catfile( @path[0 .. $#path - 1], $filename); # open the file of words or motif open(my $fh, '<', $filename) or die $!;

Your Perl program output!

As you work, you will toggle between editing/fixing your Perl program (hello.pl) and Run. We recommend that you close the Output window after each Run by selecting the X as indicated. Note: if do not close the output window after each run, TextWrangler will concatenate your output, for example: hello DNA Land hello DNA Land If this happens, just close the output window and RUN again.

Page 5: Step 1: Install a Perl Interpreter (the Perl “Engine ... · usage. If so, then the Perl interpreter will “run” (execute) your Perl program. If all goes well, you should see

Page 5 of 6

Step 4: Installing Perl Modules Some Perl scripts require special add-on packages called “modules” that provide Perl will extra functionality. There are countless modules available that help Perl do everything from convert between Roman numerals and regular numbers to access online databases. Although Perl comes with some of these modules, you’ll probably need to install most of the more unique ones yourself. We will walk you through this process with an example. Note: We assume you already have developer tools installed on your machine. A C++ compiler is required.

1) Open the Mac OS X Terminal.

2) A small window should appear with black text on a white background. Type cpan and press enter. You should see something like this:

3) Answer yes and wait for installation to finish. When it does, the terminal window should

look like this:

Page 6: Step 1: Install a Perl Interpreter (the Perl “Engine ... · usage. If so, then the Perl interpreter will “run” (execute) your Perl program. If all goes well, you should see

Page 6 of 6

4) For this example, we’re going to install a module that lets us convert Roman numerals into regular digits. Type install Roman and wait for cpan to finish installing your new module. Please note that you may need administrator privileges to complete this process. Also keep in mind that this process may require your input, so read the instructions carefully when you’re asked to do something.

5) Test your new module. Open up TextWrangler and write a new script like so: #!/usr/bin/perl use Roman; my $romanNumerals = “XIV”; my $decimalNumber = Arabic($romanNumerals); print “Roman Numerals $romanNumerals are equivalent to $decimalNumber.”; Your output should look like this: Roman Numerals XIV are equivalent to 14.

Alternative Method: Install Manually

1) Go to the CPAN website (http://search.cpan.org/). Search for the name of the module you want.

2) When you find the module you’ll be looking for, you’ll be taken to a

documentation page. If you look to the far right, you’ll see a download link. Click it.

3) Look for a readme file. It will tell you what you need to do to install

the module (though most likely you will have to run these commands in the Terminal (in order!):

- perl Makefile.pl - make - maketest - make install1

1 Note that you will need administrator privileges to use the “make install” command. If you know the password, type “sudo make install” instead and then enter your password when prompted.