Adventures in Perl: Part 1.
I decided recently that I'd make some effort to learn Perl. I've long been aware of it but have never found the need or inclination to use it. When I've had cause to do server side stuff for web content I've used PHP. For general *nix scripting I've coped with Bash, GNU core utils, sed and the like. On a couple of occasions recently I've found myself trying to do something which seems clumsy or just pretty much impossible in Bash etc and thought, now is the time to look at Perl. I should probably work through some sort of 'learn perl' guide at some point, I acquired a couple of O'Reilly books recently, but I'm currently taking the brute force and ignorance approach of having something I want to do and trying to do it in Perl aided by many Google searches. Some thoughts thus far:
- There is no built in function for removing trailing and leading whitespace from a string. Sure it's easy enough to do it with regular expressions, but I'm surprised there's no pre-defined function like PHP's trim().
- Lack of boolean variables. Though I'm not sure that's the right way to say it. You can't say my $blah=true or my $blah=false like you can do in JavaScript and PHP. You can use 1 or 0 though which is just as good really I guess.
- I've struggled with arrays quite a bit. Things like trying to work out how to reference the required element and how to get the contents of an array rather than a reference to it. Nothing insurmountable though and I've made it difficult by wanting to use an associative array each element of which is another associative array some elements of which are list arrays.
- I really should find out what a scalar is. I keep encountering references to them but haven't bothered to actually find out what one is.
One comment
Chris May
I used to quite like perl, a long time ago, but now my mind has been so twisted by years of Java that I find perl’s bolted-on OO just a bit too clunky. Ruby’s a much nicer language IMO. Nevertheless, perl’s ubiquity is a strong selling point, as is the enormity of CPAN
I think that the reason there’s no trim() function is that regexes are regarded as a normal and natural part of the language, rather than some esoteric weirdness for use only by bearded unix types, as is the case in most right-thinking languages :-)
So from that POV there’s no reason to prefer trim($string) over
ah, sweet sweet line-noise code :-)
The lack of an explicit true and false is a little bit strange, but I find that I don’t need to actually assign true/false very often – if you find yourself doing
then you’re Doing It Wrong :-)
Scalars are just variables that have only one value, as opposed to Arrays and Hashes, which have several. Making arrays and hashes ‘special’ like this always seemed a bit weird to me; they’re just data structures, after all – if hashes are special why not have variable types for trees, or queues?
Lists-of-lists, hashes-of-hashes, and variants thereof, are kinda fiddly, because of the need to explicitly create references and then dereference them all over the place. It’s worth a read of perldoc perlref.
Here’s an example. The square-bracketted lists make references to lists, which are then de-referenced using @{$reference}, or using $reference->[$index]
The for-loop code is so much nicer in a language that deals with references all the time – e.g. Ruby:
(actually it would be more ruby-ish to use Enumerable.each rather than a for loop, but that’s beside the point).
19 Apr 2009, 15:02
Add a comment
You are not allowed to comment on this entry as it has restricted commenting permissions.