From FAIWiki
Revision as of 15:31, 7 September 2007 by Betterworld (talk | contribs) (Perl style)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Perl style

I was very delighted to find this hook, because it was exactly what I needed. However I'd like to note that the Perl code ([1]) is not very clean. I'm going to fix some things:

  • open FILE, ">$filename" || die "blah"
The "||" here has more precedence than ",", i. e. "die" is never executed (because ">$filename" is always a true value). Use "or" rather than "||" because it has lower precedence (or use parentheses).
  • $result = `sh -c '$command'`;
Backticks invoke a shell anyway, so using "sh -c" here is redundant and only makes the quoting mess worse. In some particular case, $command is something like "echo 'y' | mdadm --create $config{$md}{options} --verbose /dev/$md --level=$config{$md}{level} --raid-devices=$config{$md}{num_devices} $config{$md}{devices}" which has the effect that the green single quote terminates the red one, which is certainly not what the author intended. I've changed the backticks to a system call because the result is only used for printing anyway.
  • When writing to a file, one should check errors for the print and close calls, otherwise the script will not notice when the disk is full.

--Betterworld 16:31, 7 September 2007 (CEST)