PBP: 035 Single-Character Strings

Much like empty strings, the PBP suggests being careful of how you write single character strings.  It points out a number of them it considers “ambiguous”, including:


$thing = ' ';

$thing = '        ';

$thing = ',';

For the comma, other punctuation, and whitespace, the PBP suggests using q{}, which I find a terrible suggestion for anything, ever.  q{} and qq{} are some of my least favorite things to read in Perl, and the idea that engineers can’t figure out how many spaces are between a pair of quotes confounds me.  I can’t think that telling the difference between one, two, or three spaces between {} marks will be any better.

Heck, even my blog has context coloring.  Let’s not make our lives harder than we need to!

For tabs, the PBP suggests using \t, which I do agree with.  \t interpolates to the next tab stop and is an easy lazy way to line things up.  I don’t know if it is reliable enough to do complex things but for quick hacks or debug code it’s really a great character escape.  I’m an old-time C guy, though, so know them all anyway.

I think this is one perlcritic actually takes further and demands any string without letters be marked with q{}.  No.  No.  No.  I kill this right away.

Leave a Reply