zsh-users
 help / color / mirror / code / Atom feed
* Assign to outer parameter from function called by zargs
@ 2023-03-28 16:24 Eric Nielsen
  2023-03-28 17:50 ` Roman Perepelitsa
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Nielsen @ 2023-03-28 16:24 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 762 bytes --]

Hi. I'm trying to assign to a parameter set by an outer function from a
function called by zargs.

Accessing instead of assigning works:

access_outer() {
  print ${outer}${1}
}
() {
  autoload -Uz zargs
  local outer=a
  zargs -n 1 -P 0 -- b c -- access_outer
}

results in:
ab
ac
as expected.

But assigning does not work as I was expecting:

assign_outer() {
  outer=${outer}${1}
}
() {
  autoload -Uz zargs
  local outer=a
  zargs -n 1 -P 0 -- b c -- assign_outer
  print ${outer}
}

results in:
a
and I was expecting to get:
abc
(or acb, I don't care about the order since I'm using -P 0 and I expect
paralellism could interfere with the order)

What am I missing?

--
Sent with HEY <https://hey.com/sent>

[-- Attachment #2: Type: text/html, Size: 3367 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Assign to outer parameter from function called by zargs
  2023-03-28 16:24 Assign to outer parameter from function called by zargs Eric Nielsen
@ 2023-03-28 17:50 ` Roman Perepelitsa
  2023-03-28 19:53   ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2023-03-28 17:50 UTC (permalink / raw)
  To: Eric Nielsen; +Cc: zsh-users

On Tue, Mar 28, 2023 at 6:25 PM Eric Nielsen <ericbn@hey.com> wrote:
>
> assign_outer() {
>   outer=${outer}${1}
> }
> () {
>   autoload -Uz zargs
>   local outer=a
>   zargs -n 1 -P 0 -- b c -- assign_outer
>   print ${outer}
> }
>
> results in:
> a
> and I was expecting to get:
> abc

assign_outer runs in a subshell, so it cannot modify the parameters of
the parent shell. It's similar to this:

    outer=a
    ( outer+=b ) &
    ( outer+=c ) &
    wait
    print $outer  # still "a"

Roman.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Assign to outer parameter from function called by zargs
  2023-03-28 17:50 ` Roman Perepelitsa
@ 2023-03-28 19:53   ` Bart Schaefer
  2023-03-28 20:34     ` Eric Nielsen
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2023-03-28 19:53 UTC (permalink / raw)
  To: Eric Nielsen; +Cc: zsh-users

On Tue, Mar 28, 2023 at 10:50 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> assign_outer runs in a subshell, so it cannot modify the parameters of
> the parent shell.

More specifically, there's this comment in zargs:

# Everything has to be in a subshell so that we don't "wait" for any
# unrelated jobs of the parent shell.

The "wait" is necessary to set the return status of zargs in (as
nearly as possible) the same way that xargs would do, so you can't run
current-shell actions with zargs.

What's the context of your question and your desired end result?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Assign to outer parameter from function called by zargs
  2023-03-28 19:53   ` Bart Schaefer
@ 2023-03-28 20:34     ` Eric Nielsen
  0 siblings, 0 replies; 4+ messages in thread
From: Eric Nielsen @ 2023-03-28 20:34 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1120 bytes --]

>> assign_outer runs in a subshell, so it cannot modify the parameters
of
>> the parent shell.

> More specifically, there's this comment in zargs:
>
> # Everything has to be in a subshell so that we don't "wait" for any
> # unrelated jobs of the parent shell.
>
> The "wait" is necessary to set the return status of zargs in (as
> nearly as possible) the same way that xargs would do, so you can't run
> current-shell actions with zargs.

Thanks Roman and Bart for the explanation. Makes perfect sense.

> What's the context of your question and your desired end result?

My use case might be too specific: I wanted to somehow capture the end
result of the processing of all the calls made by zargs. I didn't want
to rely on the zargs return code when zargs is used with -P as it was
missing values before Zsh 5.9 and it's intermittently failing in Zsh 5.9
and macOS. (https://www.zsh.org/mla/workers/2022/msg00611.html)
So I though having the called functions assign a status code in an outer
parameter could work. It was not a good idea...  :- )


--
Sent with HEY <https://hey.com/sent>

[-- Attachment #2: Type: text/html, Size: 3763 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-28 20:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-28 16:24 Assign to outer parameter from function called by zargs Eric Nielsen
2023-03-28 17:50 ` Roman Perepelitsa
2023-03-28 19:53   ` Bart Schaefer
2023-03-28 20:34     ` Eric Nielsen

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).