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

Michael Budash mbudash at sonic.net
Sat Jul 1 20:27:11 PDT 2006


On Jul 1, 2006, at 6:42 PM, David Alban wrote:

> Greetings,
>
> Say I want to do something like:
>
>   $hosts => {
>       foo => {
>           source_tree  => '/some/dir',
>           rsync_target => "bat:$spool_dir/foo/",
>       },
>       bar => {
>           source_tree  => '/some/other/dir',
>           rsync_target => "bat:$spool_dir/bar/",
>       },
>       .
>       .
>       .
>   };
>
> But I don't want to hardcode hostnames "foo" and "bar" in the
> rsync_target values.  Is there a way to do something like:
>
>
>   $hosts => {
>       foo => {
>           source_tree  => '/some/dir',
>           rsync_target => "bat:$spool_dir/" . __KEY__ . '/',
>       },
>       bar => {
>           source_tree  => '/some/other/dir',
>           rsync_target => "bat:$spool_dir/" . __KEY__ . '/',
>       },
>       .
>       .
>       .
>   };
>
> That is, is there some magic string I can put in the source to
> indicate to the compiler to use the value of the current hash key?
>
> Of course, I just noticed that this ability would have to allow the
> coder to specify out to what level the magic string referred.  'Cause
> in my example, I wouldn't want the value substituted for __KEY__ to be
> the string  'rsync_target'. :-)

this doesn't answer your question about the existence of such a  
'magic' string, but...

you might want to explain why you can't simply handle this in the  
code that makes use of this data. of course, my question assumes that  
your example data describes your only need, and not something like  
this, where the key ref is floating:

>   $hosts => {
>       foo => {
>           source_tree  => '/some/dir',
>           rsync_target => "bat:foo/$spool_dir/",
>       },
>       bar => {
>           source_tree  => '/some/other/dir',
>           rsync_target => "bat:$spool_dir/bar/",
>       },
>       boo => {
>           source_tree  => '/some/other/dir',
>           rsync_target => "bat:$spool_dir/boo/boo/",
>       },
>       .
>       .
>       .
>   };

--
Michael Budash
Michael Budash Consulting
michael at budashconsulting.com
707-252-7670 off
707-363-4262 cel




More information about the SanFrancisco-pm mailing list