Wednesday, 21 October 2009

Does anyone else do this?

I've been away on holiday, and I'm just going back over some comments left on my blog, and got this one?

"Is that a new fashion to use q{string} instead of 'string'?"

My response:

"Just a habit I have gotten into since using perl::critic and PBP.

As '' and ' ' are not allowed by 'the rulez' and you should use q{} and q{ }, (and their double quote equivalents}, I have the coding habit of just using then straight off."

Anyone else doing this as a matter of course, or is it just me? I notice going through a number of recent books, ' and " are used in most of the code examples.

5 comments:

brunov said...

I try to add the minimum amount of noise around scalar and array definitios. I use single quotes '' unless I want to interpolate a variable, in which case I use "". Only if I need to have both inside the string I use q{}.

Similarly, I'll use quotes around the element of a single-item array when declaring it ( my @stuff = ('foo') ), but qw() when I have a bunch and hence quoting and commas would add clutter:

my @stuff = qw(foo bar baz quux);

Unknown said...

I did not realize that single/double quotes broke the rules ;0) I guess as long as you're consistent it does not matter which you use. I went through a phase where I used q{}, qq{}, qw{} in favour of their quote equivalents. For simple things, IMHO its easier to read the quote forms:

my $name = $person{'name'};
my $name = $person{q{name}};

Though in this example I usually leave off the quotes entirely ;0)

Unknown said...

I thought they only broke "the rules" for whitespace and some symbols where using quotes might be confusing.

For example: q{ } vs ' ' vs " "

I tend to use "" for most strings of any length.

fREW said...

I use q{} and qq{} for strings of length 0 or 1. That seems to be clear enough to me and PBP doesn't complain about it.

Unknown said...

I think my coment of '' and ' ' breaking the rulez wasn't clear, this is only when it is an empty string or just whitespace.

It is interesting to find out what others think, thanks.