Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: r/w access to calling function's $@



On Tue, 13 May 2014 18:42:06 +0200
Roman Neuhauser <neuhauser@xxxxxxxxxx> wrote:
> > Failing that you have to do something nasty like using "eval" to avoid
> > exposing the point where it gets transferred to the current argument
> > set.
> 
> not sure what exactly you have on mind here, could you give me a sketch?

Use "reply" as alluded to by Bart but disguise the update to argv.  I
can't think of a good reason to do this rather than just living with the
limitation and manipulating $reply explicitly.  Also, this method makes
it hard to pass *in* arguments.


my_function_that_does_the_real_stuff()
{
   typeset -g reply # in case the caller didn't localise it
   reply=(my reply value)
}

my_disguise='
my_function_that_does_the_real_stuff
argv=("${reply[@]}")
'

# Call a function, disguising the fact that it updates $argv.
eval $my_disguise


Bart's idea of an EXIT trap is a bit neater.  For functions the EXIT
trap explicitly gets called in the caller's scope.


% print $*

% pass_back() { trap 'argv=(my results)' EXIT; }
% pass_back
% print $*
my results


There's still what amounts to an eval in there, however.

pws



Messages sorted by: Reverse Date, Date, Thread, Author