bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing variables by reference conflicts with local


From: Matthew Woehlke
Subject: Re: Passing variables by reference conflicts with local
Date: Mon, 03 May 2010 20:01:22 -0500
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.23) Gecko/20090825 Fedora/2.0.0.23-1.fc10 Thunderbird/2.0.0.23 Mnenhy/0.7.5.0

Freddy Vulto wrote:
     # Param: $1 �variable name to return value to
     # Public library function
     blackbox() {
         local __1
         _blackbox __1
         [[ $1 == __1 ]]&&  echo "ERROR: variable name conflicts"\
                 "with local variable: $1"
         printf -v $1 %s "$__1"
     }


  # Param: $1 �variable name to return value to
  # Public library function
  blackbox() {
    [[ "$1" =~ [_[:alpha:]][_[:alnum:]]* ]] || die # TODO: error instead
    if [ $1 == "__1" ]; then
      local __2
      _blackbox __2
      printf -v $1 %s "$__2"
    else
      local __1
      _blackbox __1
      printf -v $1 %s "$__1"
    fi
  }

Wouldn't that avoid having variable names that can't be used?



It should be possible to place this code in a generic wrapper such that you can do:

blackbox1() { call_varref _blackbox1 "$@"; }
blackbox2() { call_varref _blackbox2 "$@"; }

This requires, of course, that only one variable is 'passed by-ref' and is at a consistent position (though you could easily add a parameter specifying what position). How to do this is left as an exercise for the reader :-).

--
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
--
Oops. -- Shannon Foraker (David Weber, Ashes of Victory)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]