Perl style

Beatnik on 2006-06-06T15:49:37

How I love those moments where I can baffle one of the senior techies by giving some perl pointers. Just now, I had my 5 minutes of gloating. The problem: Add a catch-all string into the following snippet:

$foo = $bar unless $foo;
My solution was something like:
$foo ||= $bar || "Catch";
I tried explaining this by using both
$result = open(FOO,"<..") || "Oops";
and
$a = $a + 1; versus $a += $1;
It slowly sunk in. The unless bit reminded me of a section in the Perl Best Practices book I'm reading. Thanks Damian! I pointed out some extra funny bits by bringing in the C-style short hand for if-then-else. Maybe I was doing this to get a 5 minute break from work..


Is that production code?

Limbic Region on 2006-06-06T21:37:15

I would be rather perturbed to find that in production code without explicit comments that values such as

''
0
'0'
reference to an empty hash, array, or any of the above

for $foo and $bar are invalid and assigning over them is safe

Re:Is that production code?

Beatnik on 2006-06-06T23:51:25

Obviously, $foo is not in production code :) but the style is.

Re:Is that production code?

TeeJay on 2006-06-07T09:17:46

why?

Why should any of those be valid apart from 0.

If you can't see the type and usage of a variable, the solution is to name it correctly.

It should also be clear if 0 is acceptable or not in the code, that would be the only comment possibly needed.