[sf-perl] Make a hash key's value dependent on the key itself?

David Alban extasia at extasia.org
Sat Jul 1 20:47:41 PDT 2006


On 7/1/06, Michael Budash <mbudash at sonic.net> wrote:
> you might want to explain why you can't simply handle this in the
> code that makes use of this data.

Yeah, that's what i finally did. :-)

The more I thought about it, the more I became convinced that if there
was a way to do this, it would probably be unnecessarily complicated.
So I did:

$hosts => {
    foo => {
        source_tree  => '/some/dir',
        rsync_target => "bat:$spool_dir/::SOURCEHOST::/",
    },
    bar => {
        source_tree  => '/some/other/dir',
        rsync_target => "bat:$spool_dir/::SOURCEHOST::/",
    },
    bat => {
        source_tree => '/yet/another/dir',
        rsync_target => "mumble:$raid_root/::SOURCEHOST::/",
    },
    .
    .
    .
};

Then no matter which machine I'm on, I can do:

  $rsync_target
      = $hosts->{ $hostname }{ rsync_target } =~ s#::SOURCEHOST::#$hostname#;

What I *really* wanted was to be avoid copy errors if I added a new
host in the future.  That is, in the editor, copy and paste the foo
data, rename the foo key with, say, baz, but then forget to change foo
wherever it appears in the values of the new baz hash.

Thanks.

-- 
Live in a world of your own, but always welcome visitors.


More information about the SanFrancisco-pm mailing list