Monday, June 24, 2013

Perl Scripts in OSX

Perl Scripts in OSX

Scripting is an "easy" way to get things done.   OSX has Automator, which is a great tool.  However, sometimes it's just easier to do certain text processing in other scripting languages.  The three "greats" are Perl, Python and Ruby.

When it comes to lazy, homegrown text manipulation, I like using tiny bits of Perl.  Well, for Terminal stuff.

All of what follows is intended, eventually, to run from the Terminal or an automator.


Organization: Create a Perl folder

The key to automation is organization.   Keep things separate, keep things simple, and our job of remembering stuff gets a lot easier!

In finder, I created a Projects folder under Documents.  In Projects, I created a Perl folder. 



Add new Perl Folder to your .profile

Now that we have created a folder to hold all our perl scripts, we want to make our lives easier and do some stuff that will allow us to run any file -- in this perl folder -- from any location in a Terminal session. 


First, run TextEdit (or your favorite text editor)...



First, check to see if you have a .profile already--you probably do not.  But just in case, File -> Open, navigate to your "Home", then press  Command + Shift + . to see all .dot files.   IF you see a .profile, then you will want to edit that file...very carefully I might add!

I was surprised to find I did not have a .profile.  So I cancelled out of the File Open dialog and created a new document. 

First things first--when editing "UNIX" style files, remember to change the "RTF" style document to a plain text document! Format -> Make Plain Text
  
Next, we are going to append to a system variable called "PATH".   PATH is used to tell Terminal sessions where commands exist.  Any folder in the PATH will be searched when running commands from a Terminal session.  So by adding our new Perl folder to the path, we'll be able to run our perl scripts from anywhere on our mac.

If you already have a .profile--be careful!  modifying an existing .profile is beyond the scope of this blog post.  Hopefully if you have a .profile, you know what you are doing!

I did not have a .profile, so I simply typed in:

export PATH="$PATH:~/Documents/Projects/Perl"

Notice the double quotes!  Very important.


Finally, lets save.  When saving, we will want the filename to be ".profile", the directory to be our home, and we will want to save w/out a .txt extension!  When prompted, confirm that yes we want tp use ".".  


When running terminal, in commands keyed into this /.profile file will be automatically executed.  There is all kinds of neat stuff one can do! However, I like to keep things simple and vanilla.  I'm sure there is an OSX way to "get-et-done", but whatever...

Create our first Perl script

now, once again, lets create a new document in Text Edit.  As usual--Make Plain Text.  Save, and when saving, uncheck "if no extension is provided, us .txt" checkbox.  I called this script "hello.pl"...and remember, I need to save it in 

The first line tells our Terminal session what kind of script we're running--so for Perl, the script needs to start with: #!/usr/bin/perl

after that...all our perl commands!  In this case, something simple...

print "Hello world!\n";

exit 0;




Setting up our script

Finally! Done w/TextEdit.  First, launch terminal.

Once in terminal, lets change directories to our Perl folder!

$ cd Documents/
$ cd Projects/
$ cd Perl/

Once there...lets add execute permissions to our perl script!

$ chmod a+x hello.pl


Running our script

From any directory in Terminal, key in hello.pl, and our program will run!

$ hello.pl


Recap: Creating Perl Scripts


  1. Create a "Plain Text" file -- usually ending in .pl 
  2. First line of file reads #!/usr/bin/perl
  3. Make file executable via chmod a+x
Phew!  Seems like a lot right?  Well...not really.  Lets find out why in a future post, as use a perl script to convert Reaper ReaBank's to something we can paste into a Max4Live objects.


No comments:

Post a Comment

Write what you think. If things get spammy, review time.