Showing posts with label Perl Best Practices. Show all posts
Showing posts with label Perl Best Practices. Show all posts

Tuesday, December 18, 2012

Perl Best Practices: Code Layout: Vertical Alignment

Perl Best Practices - O'Reilly Media

I came across this chapter a while ago, and I enjoyed the argumentation quite a lot. I think the Vertical Alignment idea applies to all sorts of code, not just Perl code.

I am personally a follower of the Vertical Alignment sect, although I think, that should be the main stream.

"The other day" I came across code not following the Vertical Aligment idea, and I was rather sad ("in a professional sense"). But I am rather aware of the different streams resp. sects, and of course, this is not meant as an insult but rather just a note.

Friday, August 3, 2012

Perl Best Practices: Command-Line Processing

Perl Best Practices - O'Reilly Media

Again: rather worth reading and making use of it.

Personally I have been using Getopt::Long and Pod::Usage together for quite a while. I can seriously recommend them.

Perl Best Practices: Regular Expressions: Always use /xms



Almost always at least.

DC explains quite well in the beginning of the section on Regular Expressions  [Link], why each of /x, /m, and /s is rather, rather reasonable to use.

  • /x : extended formatting / legibility
  • /m : let ^ and $ also match next to embedded \n; "multiline mode"
  • /s : let . also match newline; "single …"

Perl Best Practices: Regular Expressions: Backtracking – Prevent useless backtracking


OMG!

Is there a way resp. tool to check, that some code, that I will look at, will in fact comply to the recipes at the end of that chapter?
I mean, yes, of course, I can always by default encode regexes the way, DC suggests there, but that makes them far harder to read than necessary, right?

Wednesday, August 1, 2012

Regexp::Common : numbered captures, named captures

Regexp::Common - Provide commonly requested regular expressions - metacpan.org

I came across R::C through the chapter on Canned Regexes in the Perl Best Practices book [Link].

Don't get me wrong: I really appreciate R::C (i.e. this package)! Really! All due respect!

Under "-keep" results from R::C get returned as numbered captures.

If I figure that out correctly, R::C got (mainly) created and further developed before the dawn of named captures in Perl. And I guess, named captures never really made their way into R::C. (Pls let me know, if I'm wrong! I am glad to scrap this article, once I know better.)

Recently I wrote some code, where I parsed date+time strings using named captures. The regexes weren't really that fancy but rather precise.

Now I rewrote the code to make use of R::C resp. Regexp::Common::time. Pls get me right: I really like to specify the format the strftime way. But that's only half the cake. As mentioned I wasn't able to make use of named captures in order to access the parsed values then. It would be nice, to use the strftime specifiers for accessing the values through named captures by the same name, wouldn't it?!?

Maybe that's mainly stubborn me, because I don't like numbered captures any more, esp. if the regex is a little bigger and has the tendency to even grow.
Of course with R::C my code and esp. the date+time matching is far more flexible, but still with named captures and w/o R::C in this rather narrow context it is fare more readable and everything is in one place.

Monday, April 16, 2012

o'Reilly Media book: Perl Best Practices

  • Perl Best Practices - O'Reilly Media
  • its Appendix B gets provided for the public by the publishing house, it lists all the recipes with brief descriptions [Link]
  • "PBP Module Recommendation Commentary" on Socialtext [Link, actually Perl Foundation links to that via "perlfoundation.org/perl5/index.cgi?pbp_module_recommendation_commentary"]; some "yes", "mostly yes", "no", "maybe" regarding the modules recommended in the book; short resp. compact and worth reading resp. good for looking up a module, that you are tempted to use after reading about it in the book, so you have another (useful) opinion
  • a collection of 256 guidelines – find them listed on a refcard here!

  • it got released in 2005 though, and time went on in the meantime!
  • rather nice to read
  • its author also teaches classes in courses by this name, they should be rather up-to-date
From the TOC:
  • Chapter 2: Code Layout: …, Vertical Alignment, …
  • Chapter 7: Documentation (this is the most extensive text on POD, that I know in the Perl literature)