· Offer Kaye > Perl
Offer Kaye's Perl page
What's my connection with Perl
Well, for one thing I'm an Israeli Perl Monger:
The Israeli Perl Mongers, one of many similiar Perl Monger groups
spread around the world, are a group of Perl users dedicated to the advancment of the Perl programming language,
specifically in Israel.
We do so by creating a stable and viable group which through an
active mailing list and
monthly meetings seek to help one another and learn new things
regarding our favourite programming language and computer science issues in general.
In fact, Israel.pm has become so succesful that for the second year in a row we are having our very own Israeli Perl
Conference, called YAPC::Israel::2004 -
Of which I'm one of the organizers.
If you're a Perl user and still haven't registered to the mailing list I heartly recommend you do so - it's low-volume
enough not to be a bother, the signal-to-noise ratio is excellent, and I guarentee you'll learn something new and
useful regarding Perl... ☺ oh, and you don't have to be from Israel, either - we already have some
subscribers from abroad and all the emails are in English.
What is Perl
Actually, there is "perl" and there is "Perl". "Perl" referes to the language, while "perl" referes to the compiler.
This distinction may not matter to most people, but I think it can be a little confusing for a beginner, as for example the
perl manpage starts out by giving a synopsys
of the command line options available for the "perl" command (compiler), but followes this up with a section called "description"
which describes the Perl language.
Learning Perl
Regardless, if you want to learn about Perl, the best way is to start reading the above mentioned
perl manpage, go browse the
perl.com website and the many many Perl related links it
offers (the Google Perl
directory is also a good links resource), and of course practice writing and using Perl.
Getting Perl
Of course, to practice Perl, you will need perl installed.
To check if it is installed, just write "perl -version" at the command line. If you don't have it installed, you'll need to get it:
- You can get the perl source and compile it for yourself.
This should nearly always work, but it is not easy.
- If you're using a modern Linux or Unix system, perl should already be installed. If for some reason it isn't, check the
ports section of the CPAN (Comprehensive Perl Archive
Network) site.
- For Windows users, the best perl distribution (arguably) is from
ActiveState, it is
called ActivePerl.
Perl Oneliners
I've been studying and using Perl for the last couple of years, and recently got interested in Perl oneliners.
A Perl oneliner is like concentrated regular Perl code. If you think of regular Perl code as a swiss-army-anything, than a Perl oneliner is like
a Swiss-army scalpel: short, sharp, and used for a particualr purpose.
While Perl oneliners are usually defined to be less than 80 characters in length, I've seen longer ones which I would still call a oneliner.
Because a Perl oneliner is meant for a single particular purpose, and is meant to be run from a command line and so should be as short as
possible, oneliners are usually associated with Perl Golf, the practice of attempting to
find the shortest (fewest keystrokes) Perl code to solve a given problem. Thus, Perl oneliners are good solutions for solving very specific
problems from the command line, but should never be used for production code.
To use a perl oneliner, you usually run it from the command line, as in "perl -e 'oneliner perl code'".
Following are some example oneliners, including links to explanations of the code. I think studying Perl oneliners will teach you more
about Perl than any other method, due to their condensed nature, so I hope you benefit as much from these as I did.
A couple of notes: I never bothered to keep track of where I found these, and frequently I changed the original if I could see a way to make
the code shorter. So if you are the original writer and want me to credit you or remove the code, please let me know. In addition, if you think
up a shorter way of doing the same thing, I'd be really glad to hear of it... ☺
- Print a random line from a file (explanations):
perl -e'rand$.<1and$q=$_ while<>;print$q'
- Print the lines of the input file in random order (explanations):
perl -e'@l=<>;@l[$i,$s]=@l[$s=rand$i,$i]while++$i<@l;print@l'
- Reverse the order of the lines but not the text order inside each line
(explanations):
perl -e'print reverse<>'
- Reverse the text order inside each line but not the order of the lines
(explanations):
perl -lne'print scalar reverse$_'
- Reverse both the order of the lines and the text order inside each line
(explanations):
perl -e'print scalar reverse<>'
- Reverse the order of the paragraphs in the text (explanations):
perl -00e'print reverse<>'
- Print the length of each line in the input file (explanations):
perl -lpe'$_=length'
- Uppercase the entire input file:
perl -ne'print uc'
- Lowercase the entire input file:
perl -ne'print lc'
- Print the odd-numbered lines (1, 3, 5, etc. ; explanations):
perl -pe'<>'
- Print the even-numbered lines (2, 4, 6, etc. ; explanations):
perl -pe'$_=<>'
Perl Lectures
For now there are only 2 lectures here - one that I gave to Israel.pm on the
December 2004 meeting.
The title is: "Syntax Highlighting for Fun and Profit". Here are the
slides, there is also a page with all the slides in
one large page.
The second is a lecture about Intoduction to Perl Subroutines
(there is also an "all in one" page).
Offer Kaye /
To contact me, email me at: offer.kaye no spam at gmail dot com /
Last Modified: Sun Dec 5 22:32:59 2004