in reply to Is "die" the best way to be atomic?

Looking at your program, it seems die is probably a nice way to make this whole thing atomic. I'm assuming it doesn't matter if the staging directory is in a state of disarray, as long as the rsync command is not run. Other than the issue with die's return value not being what you seem to expect, your code looks ok.

That said, there are some minor stylistic problems I see with your code. Here's how I would have written the lines you pasted, if I were using Perl at all (this thing reeks of a shell script, though if it's a learning experience, then hopefully I'm helping you learn a bit):

chdir $staging_dir or die "chdir: $!"; system qw(svn update base) and die "up base: $!"; system qw(svn update hostgroup) and die "up hostgroup: $!"; system qw(svn update host) and die "up host: $!"; system $copier_prog, $staging_dir, $pre_launch_dir, $copier_conf and die "copy: $!"; system $perm_prog, -c => $perm_conf and die "permissions: $!"; system qw(rsync -avz --exclude=.svn --delete-after), $pre_launch_dir, $live_dir and die "rsync: $!";

There are a few things I've done here. First, I removed superfluous quotes. In Perl, unlike most shells, you don't need quotes around your variables to be safe. In fact, doing string interpolation when you don't really want to can have negative consequences. I also made use of the qw and => operators to remove a few more quotes, and I removed some unneeded parentheses. I find these things just clutter up the program. I also shortened the error messages and variables, though that was mainly to make the code fit better in a browser.

Update: fixed silly bug caused by last-minute variable renaming.

Replies are listed 'Best First'.
Re: Re: Is "die" the best way to be atomic?
by BuddhaNature (Beadle) on Apr 27, 2004 at 17:17 UTC
    Yeah, you have the atomic part right (that is, in sync with what I think of as atomic). Yes, I know it does "reek" of shell script, but it is a learning experience - I just cracked open Ye Olde Camel Book and learned about the use of the => operator. => - Its not just for Hashes anymore!

    Thanks. I think I will take the rest of your stylistic notes to heart as well. I al so plan on doing the dry-run rsync before the real one - it might be slower, but at least it should cut down the probability of something going awry and propogating BadMojo(tm)...

    -Shane