![]() |
|
Come for the quick hacks, stay for the epiphanies. | |
PerlMonks |
RFC: How to succeed with your Perl homeworkby angiehope (Pilgrim) |
on Nov 02, 2010 at 16:46 UTC ( [id://869041]=perlmeditation: print w/replies, xml ) | Need Help?? |
So you're studying biology, linguistics or management and taking a programming class?
You're comfortable working on your Windows PC, using Word documents, Excel spreadsheets or Access databases. Maybe you've already used VB.Net or created an HTML page.
And now you're supposed to write a Perl script under Unix/Linux..
1. Preparations1.1. EditorMost Unix/Linux systems offer you two possibilities to edit your script: emacs and vi. Both use different menus and keystrokes than other Windows or Linux applications. If you already know how to touch-type, vi will be more comfortable, once you've learned how to use it. Open a terminal window and enter "vimtutor" to get started. If you don't touch-type or have little time left, I would recommend using Emacs. easymacs will make things much easier for you - allowing you to use Emacs the same way as any other software you might already know. However, it consumes 70 MB of disk space (version 2.0, uncompressed), so check your disk quota before trying to install it. (Open a terminal window and enter "quota -v" to see how much disk space you're allowed to use.) Otherwise, I would recommend Effective Emacs by Steve Yegge and the Emacs tutorial (start Emacs and click on "Help > Emacs tutorial" or just type <Ctrl-h> t.1.2. Version ControlVersion Control software permits you to manage different versions of your script. You can easily change back to an earlier version of your script when you notice that it stopped working after saving your 99th version at 2 am in the morning...Personally, I'm using subversion, because it's available under various operating systems and can be used for single and team projects. You can find out how to set up subversion as a single user reading the High Speed Tutorial. An additional benefit of version control: if you enter descriptive messages when checking in a new version, the log file produced by svn log will be a valuable record of how you created your script. If you ever need to prove that you really did your homework yourself, this might help you. 2. Writing your script2.1. Make an outlineSo you've set up version control, started emacs and used <Ctrl-x> f homework.perl to create your first Perl file. Stop. If you just start typing Perl commands (or pasting them from another web site), you'll end up with a mess of code later.Take your assignment text and either print it out and use a text marker or copy the relevant phrases/words into your file that describe what you're supposed to do. This might look like: The "#" at the beginning of the line tells Perl to ignore those lines as comments. Sometimes, your assignment will be clearly written, so you just have to extract the relevant text, sometimes you'll have to think of an appropriate description yourself. In each case, your outline will be a good basis, should you need to explain what your script is doing. 2.2. At the beginningAt the top of your text file, I would recommend adding the following lines:The first line makes it possible to run your Perl script on its own, i.e. "./homework.perl" and not "perl ./homework.perl". All you need to do is to type "which perl" into your terminal to find out the correct path to your Perl binary, e.g. "/usr/bin/perl" or "/usr/local/bin/perl" and then "chmod 755" to tell Unix that your file is an executable script. use strict; offers you a lot of helpful advice on commands that might cause trouble when you run your Perl script and use warnings; provides more informative error messages. use Data::Dumper;: print Dumper [your Perl variable] will help you tremendously, when you need to examine what type of data you are passing around inside your script. 2.3. One step at a timeTry to solve one task in your outline at a time, i.e. first make sure that your command line arguments get into your script, then look at how to best read your data file and then at how you process/change it. Make sure that each step works by printing out the results (or part of them) on the command line.2.4. Don't repeat yourselfEspecially if you're in a hurry, it might be tempting to write an entire list of statements.You'll save yourself many lines of code (and lots of work) if you delegate this type of work to a subroutine: If you've got to apply your function to an entire array of values, things get even easier: This applies your subroutine to every value in your array. 2.5. Using referencesUnlike other scripting languages, Perl uses references, i.e. variables that contain links to another variable. If you want to send data to a subroutine (or back) that is more complex than a simple number, string or an array, this is extremely useful.You can create a reference, pass it to a subroutine and unpack it there:
If you suspect that your references might not work as intended, do use print Dumper @_; at the beginning of our subroutine to look at your variables. 2.6. Keep it simple!There is an entire chapter in "Programming Perl" devoted to arrays, hashes, arrays of arrays, hashes of hashes, hashes of arrays ... . If you need to store your data in a complex data structure before writing it back into another file, don't let it get to intricate. Put a note into your outline (or draw on a piece of paper) how your data structure should look like.will work better than
2.7. Test your regular expressionsAs soon as you're told to replace part of a string by another string, you'll start using regular expressions.A regular expression is a string that contains a pattern you can use to find or extract specific sections in your data. You can find a first introduction at perlrequick. However, if you just want to replace one string by another and store the result in a new variable, here's a quick solution:
3. Practice, practice, practice3.1. Perl at homeEven if you don't want to program for a living, you won't get far using Perl if you don't practice. And you won't enjoy practicing Perl if you can't connect it to what you do outside programming.As soon as you decide to install Linux on your computer, you can get Perl and most of the modules and programming tools that interest you by using the package manager of your distribution, for example (Ubuntu. If you want to use Perl on your computer at home without leaving Windows or OS X, the easiest solution would be to install <a href=http://www.activestate.com/activeperl/downloads">ActivePerl and a text editor of your choice. There's a modified version of Emacs for Windows and OS X by Vincent Goulet, which contains lots of other useful Emacs modules. vim also provides Windows and OS X versions, and an excellent Perl support plugin. Another solution would be to use Padre a dedicated Perl editor packaged with Strawberry Perl. In case you want to take your Perl homework home with you, write your script under Windows and expect the result to work on a Linux machine inside your computing lab, that's the version I would recommend. 3.2. Read codeYou want to know how a certain Perl module works, for example the BioPerl module used to process gene data, Lingua::Stem to find word stems in several languages or Finance::Quote that collects stock quotes and fund prices from various sources on the net? Download the source code and try to understand what it does, what data is collected and in which files it is processed. EasyEclipse and the Eclipse Perl Integration plugin give you an excellent tool to understand how the parts of a complex Perl module are connected to each other.Don't panic .. Perl might look and feel different from what you've gotten to know so far, but once you have become used to it, you'll appreciate its reliability and speed -- at least when it comes to process large amount of data in the background while you might go off and have a coffee :-) ... Update Applied corrections suggested by moritz and toolic (on Data::Dumper). Thank you!
Back to
Meditations
|
|