Perl Syntax Archives

Stealing from Python

We all know that Python, Perl and Ruby (ok, and PHP, and probably other languages) are always stealing ideas from one another. This is a nice thing to do.

After programming a few with Python there is a couple of details I like. I know not all are possible to implement in Perl. Some of them are, and I would love if they were. I know not all people agree with me (that good, too). Nevertheless, I present here a couple of ideas.

Although I do not like relying only in indentation for blocks, I like the fact that conditionals and loops does not need parenthesis. It makes the code mu…

Using the Record Separator

I use Perl for years, but there are some details that I still am unable to use correctly in Perl. I think they should be my fault. And probably, if I rtfm I would get an answer. Nevertheless, and although this is not perlmonks, I'll post my problem anyway.

I use local $/ = "\n\n" as record separator to read a file. The code that processes that file, uses a module, that decides to open a config file, and that expects that the record separator is a single new line. That library fails.

My main doubt is if all modules shout set the record se…

Perl Feature Request, or am I missing something?

One of the most simple things you can do with Perl, is process text files. For that, you usually use the diamond operator, you chomp the read line, and process, line by line.

This is great and works mostly of the times.

Or, in the other hand, works when you can control where your files, the ones being processed, come from.

Because, if you want to make a generic application, you need to know that when running on Unix, chomp will remove the newline, but not the carriage return character, unlike when the same script is run under windows.

And I think that is annoyi…

Are the new Perl features worth anything?

Yes, the title is provocative. That's my idea. Now that I captured your attention, let me explain what's going on. I have a project, Quiki, that is requiring Perl 5.10. This because I use the given/when construct.

Meanwhile we received a bug report from Adam Kennedy asking to back port the project to Perl 5.8.8 because there are some Linux distributions that still use it. Note, I am not against this request from Adam. I totally understand it, and I am willing to help, back porting Quiki. But this makes me think. If Perl 5.10 is dated from D…

Grepping exact values

In my perspective Perl syntax for grep doesn't help making it faster when searching for exact values. Let me explain. While it is not that common, what you do when trying to grep for an exact string from an array?

Perl makes the use of pattern matching on greps easy:

@selected = grep { /^foo$/ } @list;

You can argue on the usability. But trust me, every once and then, some strange constructs get really useful. Unfortunately the above expression is not efficient. If you replace it by

@selected = grep { $_ eq "foo" } @list;

you get a two times faster code (check the bottom for Benchmark results).

Following the idea of split that accepts a string and uses it for the split process, I think grep could accept a string as well (at least on grep EXPR,LIST construct):

@selected = grep "foo", @list;

What kind of inconveniences would this raise?

Perl, a multi paradigm language

Perl is a great language. Not just because it is flexible, it has a great community, it has Larry Wall as its creator or because it has CPAN. Perl is a great language because it is multi-paradigm. That is, you can write Perl code in different ways, resembling imperative languages, functional languages or object-oriented languages (yes, we can discuss on this division, but that is not relevant at the moment).

What I want to say here is that I love to write functional programming lines with Perl. Check, for instance, on the task of reading a table (tab separated lines) with name,…

Mapping substitutions

I am always wondering what is the best procedure to perform a substitution on a set of elements. While map could be a good idea,
@foo = map { s/bar/foo/g; $_ } @bar
is not legible. Probably better to use
@foo = @bar; s/bar/foo/g for @foo;
While I understand that the substitute operator return value is useful and relevant, I wonder if there is a cleaner syntax for this behavior with map.

Quote delimiters

Was writing a script and stopped just to write this and ask... what is your favorite quote delimiter? I mean, what do you use to delimit quote words, or regular expressions?

I find out that most of the time I use the slash for regular expressions because with them I can not type the preceding 'm'. But, for instance, on use CGI qw.:standard.; I am liking more the dot. When substituting paths, I prefer to use the exclamation mark.

Rarely I remember that I can use balanced braces. Probably that would be the better choice on most situations, but I forget it...


About Alberto Simões

user-pic I blog about Perl. D'uh!