One of the most common things you might want to do is calculate Perl string length. For instance, you might ask the user to enter some text and want to make sure that they haven't entered too much or too little. Piece of cake. Just use Perl's length() function.
But that's just scratching the surface of what Perl can do with strings. Being designed from the ground up as a text manipulation language, Perl has a lot of string functions. Try some of these out in our quick and easy guide.
Photo © Bagwold | Dreamstime.com. Used with permission.
Unless you know in advance exactly what values your Perl programs are going to deal with, then at some point you're going to need to test one value against another. For instance, you might want to display a simple "Continue? (Y/N)" message to your user. If they enter 'Y' you will perform some processing. If they enter 'N' your program should exit.
Perl uses comparison operators to achieve this. In this simple beginner tutorial Comparing Values in Perl we look at how you compare two values and direct program flow accordingly.
What's that you say? Perl 6 hasn't been released yet?
Well, of course, you're right. Perl creator Larry Wall has been telling people at just about every Perl conference over the last couple of years that Perl 6 will be out "by Christmas". Of course, he never told us which Christmas!
However, some folks just aren't prepared to wait for an official release and want to start playing with Perl 6 straight away. If you're among them, you can access a preview release of Perl 6 called Rakudo Star.
And if you're cutting edge enough to playing with Perl 6 already, why not enter the Perl 6 Coding Contest? There are five tasks to complete:
- Find a way to express an integer as an expression containing four 9s
- List numbers which are sums of cubes in more ways than one
- Calculate addition chains
- Slide a hex-shaped piece across a board
- List all possible trees of a certain type
You'll be judged on the clarity, consistency, brevity and efficiency of your code. If you fancy your chances, find out more at the Strangely Consistent Perl 6 blog.
Image © Larry Wall - used with permission
Ok, so we're focusing on regular expressions over the next couple of weeks. But let's not forget all the other things you can do with Perl strings.
Perl is famous for its ability to parse text. In fact it was designed initially as a text manipulation language. Creator Larry Wall's main objective in creating Perl was to make it simpler for programmers to extract and format text by combining all the things he liked about sed, awk and C. Hence the name: PERL (Practical Extraction and Reporting Language).
Although it wasn't long before developers (particularly web developers and system administrators) harnessed the power and flexibility of Perl for other applications, it remains to this day one of the best languages out there for slicing and dicing text. It handles strings faster and more efficiently than many other languages and has a whole range of functions for comparing, sorting, extracting and manipulating strings. Learning how to work with strings is fundamental to understanding Perl. Check out our basic string manipulation tutorial.
Image © Wong Chee Yen | Dreamstime.com
Regular expressions. You either love them, or hate them. But normally, if you hate them, it's probably because you've never taken the time to get to know them. And I can understand why.
Because, to the uninitiated, regular expressions look more like Klingon than anything you would ever write yourself. All those funny symbols - how can anyone make sense of all that, let alone write it in the first place?
Well, the fact is that anyone can learn to read and write regular expressions. All you need to do is understand a few simple rules. And in a new series of tutorials, we're going to break down regular expressions into easily digestible chunks that anyone can understand. By the end of the series, you'll be able to craft regular expressions at all levels of scope and complexity ready to do your bidding.
Ready? Let's get started. Head over to Simple Matching with Regular Expressions
Image © Mark Lewin
The Mars Curiosity Rover is now hurtling through space on its way to the Red Planet. After a mind-boggling 350 million mile journey, Curiosity is destined for arrival approximately 8.5 months from now.
Compared to its rather more subtle predecessors Spirit and Opportunity, this particular craft sounds more like the Terminator. Curiosity is the size of a small family car, runs off plutonium 238 and will fire lasers to kick up debris on the planetary surface for analysis by an on-board spectrometer.
Like all the Mars missions, Curiosity is a real feat of human engineering. Being a bit of a geek, I got to thinking about the complexity of the computer systems that make it all work and then of course, what role, if any, Perl had to play in such systems. After all, I know scientists have a thing for Perl and how it's regarded as a tried and tested solution compared to some of the more new fangled languages out there (like the rather weird but very promising Ruby for instance).
So, I got Googling. And I although I couldn't find anything relating to Perl and Curiosity in particular, I did find a rather interesting exchange between some Perl/Space geeks on the Perl Monks forum a while back.
It seems like a whole lot of Perl is being used at NASA and not just for trivial tasks either. Does anyone reading this know whether Perl has been used in the Curiosity at all? Let us know in the comments!
Image © Chrisharvey | Dreamstime.com
Many people I speak to express surprise that I'm still passionate about Perl after all these years. Hasn't it been replaced by more "modern" languages such as Ruby or Python?
The answer is a resounding no! Perl is out there being used for an extraordinarily diverse range of applications. The reason Perl doesn't get as much attention as it deserves is that it's very much a "glue" language - providing a way of linking applications written in other languages together and being used to write utilities quickly and succinctly which systems can use for input, output or intermediate processing.
So I thought it would be fun to list a few of the bigger companies who use Perl:
- Cisco Systems
- Morgan Stanley
Need I go on? None of these companies are regarded as being "behind the times" in their use of technology. They have realized the extraordinary power of Perl and use it to run some of their most business-critical applications.
And if they can rely on Perl both now and the future, then so can you!
Image Â© US National Oceanic and Atmospheric Administration
Do you want to use Perl at work? Having trouble convincing your manager that Perl is the best tool for the job?
Being a COBOL shop, it never occurred to anyone to use anything other than COBOL to achieve this. The programs we created to do this were long-winded and error-prone - especially when different engineers calibrated machines in different ways.
However, I'd just started playing around with Perl and saw how much better it was at manipulating text than COBOL. In particular, its use of regular expressions made it a cinch to search for the entries we needed to report on.
Using a 350-line COBOL program that everyone hated as a test case, I wrote a 40-line Perl script that did the same job better and faster. What's more, because we were 'pattern matching' text with regular expressions without relying on the same characters being exactly in the same place each time, it could usually cope with formatting changes without being rewritten.
Soon we were all Perl converts. And once our manager saw how much more productive we were in Perl for some tasks, he revised his "COBOL for everything" approach.
Maybe there's a task where you work which is just begging to be turned into a Perl script. Why not take the initiative and see if you can create some Perl converts where you work? Let us know how you get on.
Image © Jiri Vaclavek | Dreamstime.com
Let's say that you, like me, are addicted to World of Warcraft, and you decide that you'd like to track the progress of your characters, or maybe your friends' characters, on your personal website.
You could hard code all that data, updating it whenever you have the time. Or, with some simple Perl scripting magic, you could automate the whole process! Learn more in this tutorial of how to read character information from the World of Warcraft servers.
"Pragmatic" can often mean "boring" -- but this in-depth look at building a web crawler in Perl is not boring at all. In fact, it's informative and a great example of a truly useful Perl application. Check out this fantastic tutorial from perlhobby: A pragmatic approach to writing a mp3 crawler in perl.