From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id 2C4F429E6D for ; Thu, 29 Feb 2024 06:16:43 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1709183803; b=TcDN8WSKHJ8HZjb3G9N3ccB0PrHajtziN/C6av3+b1wyGQNmPUwG1O5Fk+DdcMUjxtkb5t5L9i 3iPbIiB8lmzSDVmw9JflkjYDLDcvPaYKIDYfkWSN7tWYpnSILW4/yu3YlIkDTs+14Nd0NBn6JZ 1NepNE9W7BGzjIsgvi+GAJYiHUE8rkvd3ZMJSwPhPWVpQe3ZEA8MOuL06ZU4lMdBO7/QhqW4f+ 6pV11phyTSFIYWB4OYNJH8T1Rw6W+3FLlJp9vMHF0HqVLZhE8lEHb2RBdnmdZ6oUNnpKUvzofd 2m/hot8Q9veGYqMv/QfEUaVKxM0VfP12nB/4VG9362iS4g==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f43.google.com) smtp.remote-ip=209.85.218.43; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1709183803; bh=lA5Jq4TBCgoOPfFaeBYt0aFneGejtWwBNDAa4PBgtl0=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:To:Subject:Message-ID: Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature:DKIM-Signature; b=WJgo9r56zkDQFosdZb9nYshCTdm9Pa87PUDWnPX5tPJiDBnZly1WDH4BnmX7xXw+Ph2XAMi1Rh wpCSfWJdyQxpX+YnoMP6ALg03NEDIJBqP8/1EsEQEKIkpHpSddJmvRtSOyGXJM3HkGhjwP3Apz R5sBNEF7u2dJJt+/8qfsZyTS63MzjedzsqYcZbZEL5pPf8ZsZVLOMald0Q9kKKziWRylWFdefm 7dJXF1JUSLJ/LRdvo8nNm1sIb7IhKRbjXJzQzwvo2sOt4txLpbKajlXEId+w2w/e5waCMZGx41 gPyST1isCbxOfrPwpKgavhGraSMqjSkOvQRqEnpyF6wC9w==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Cc:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=XeD0wFjwizbFJUAyxVe88XdTsGsyMp5glHh2RPMXgGI=; b=BVxEWDNrBd7T2fXRlLR774G42z Yq7j7G/p/4Znelbi9SgGEmFD6cwpZfpxFkOuLagfrVk8jKZc8F1OZLELsGuewz/+AhV/wsmSCcR13 8K4TBHjFzQKBqIjXMhd9DgVk4NxTE3a2Ij+kGeZ35FYH/anmmnFKXQi9k9ZDnUrsauGQPzp0UZWDV rgApweq6RWwG65sqzQnOLptMCibYxaFeuhxk4F9vxwGhF//SXEIK4zgYbpW8qJXdzTewAQpgJ4+vy iB6MVby5xCHpwBOOSiu8qqVb2AFOsLnc/g7RRLyponDVih2dEzMix6wVhtJ3WTfuDzFZQr2keHp1x BZEPhZDA==; Received: by zero.zsh.org with local id 1rfYmh-000GZ4-L8; Thu, 29 Feb 2024 05:16:43 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f43.google.com) smtp.remote-ip=209.85.218.43; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ej1-f43.google.com ([209.85.218.43]:49262) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1rfYmP-000GFP-DK; Thu, 29 Feb 2024 05:16:27 +0000 Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a3fb8b0b7acso70818966b.2 for ; Wed, 28 Feb 2024 21:16:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20230601.gappssmtp.com; s=20230601; t=1709183785; x=1709788585; darn=zsh.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XeD0wFjwizbFJUAyxVe88XdTsGsyMp5glHh2RPMXgGI=; b=f34c/VK7T+wvBRh90xoaF0ltaELmy8Gmy+uhvxKdrz1b1uPMXc9+jQ+m5z+fqPjo+o vJhnCvI/6/bYRvHkqlkOJRktC/QwdBe/HfTTNz78ch2zs/h2hjfZJVaNTA35qpoJtLkF NxwSdL4qZ0Q8GXevMP1fyuHXf6g1c0lhPv0eBfi+JniGbjDpu6jbQOefa2fBYxJiN2LQ xdjB9FApsEwhqhVOyfQxkiiUV9s2VyG33iu3O6ytmNCmZ2fHd/7ufaSqr/hYSIoduIHa GfVD3QaXFXSotF3fT/a59nslkZ15ppPrPALbTMKj1irO0VhaMUlHDWqzo6lPdmHuGiLR bmfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709183785; x=1709788585; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XeD0wFjwizbFJUAyxVe88XdTsGsyMp5glHh2RPMXgGI=; b=HfMuGgtd00U0EIkFaV6VdyQhddHdFEY0BRLYaJZOBlZwALuh/6Fr7aFFLbdtpvXaL1 SI3jrL2+qW8EG8x7IqV/Si1mdsP/q2DP4R3M4A7Y4OfPbS8ALzy27WBDBrqA7Mml0WtW Z4BCAFMyM+r+HVDb1B/Ip1tC9Dxkfxy5z7jCpHzNJjxtAFWq3APXtt9zjV2YcxBtAwvP Cer5e6oicoxVU3NT/MwQmJVtWMAp3Vyh8MFoqdyIjyErVf8rlNiEcP/gSF6WzsnT78Pc Uis7deyS/GqjmpgUS3A1RTllTRz8upijZaZvEDZDsNddH7JnvS+E88OSnvfkhnjCdCsh eMHQ== X-Gm-Message-State: AOJu0Yzu9SqRBZg2MAAN14D0Hzp8y79BRyDbzc2RvSyWM/scevwsSspU goyZ49T8xPgzsiHagBvuIhd2y9aBMmsdwoGTUrVR32ul5mwDBptu2oV6UAebFfckLczq2rFf6nK 22DPRf70HawFdrWI+Btbt4UZ5g8B9HNOtOixyswcU9AcFP/16KA== X-Google-Smtp-Source: AGHT+IFFl0adklmCEPY0jWgsii7hoXhcO50WGNHMg6an7sIusab0PCe3Yt/ongjMcnjtCHwnfgi3DAYIBnrZwmhXZWU= X-Received: by 2002:a17:906:a291:b0:a44:234:e621 with SMTP id i17-20020a170906a29100b00a440234e621mr739918ejz.10.1709183784558; Wed, 28 Feb 2024 21:16:24 -0800 (PST) MIME-Version: 1.0 References: <66045-1675975796.128039@FBF_.0yMO.Y8fk> <40726-1676098925.110777@U2kb.d0Pd.I9ml> <20240211070042.4j37hkgjjn3dfjqd@chazelas.org> <20240220210553.g6imt3op6zahz4pa@chazelas.org> <20240221201215.anpjcfav6na55gg6@chazelas.org> In-Reply-To: <20240221201215.anpjcfav6na55gg6@chazelas.org> From: Bart Schaefer Date: Wed, 28 Feb 2024 21:16:13 -0800 Message-ID: Subject: Re: Up-scope named references, vs. ksh To: Zsh hackers list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 52640 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: Lost track of this for a bit .. On Wed, Feb 21, 2024 at 12:12=E2=80=AFPM Stephane Chazelas wrote: > > Like: > - if var is found (declared) in the current scope, record that > it's that of that scope that it is to get the value of or set > or unset (and reset again thereafter) > - if not, repeat for parent scope > - if not found at all, record that it's to be at the global > scope. That's where this whole discussion started: It's not good enough to "record that it's to be global" because if a local of the same name is later declared, there is no way to insert a new global "above" that, it's akin to attempting to allocate new space in your caller's stack frame in C (except there's one stack for each parameter name). The workaround in shell code is to start with "typeset -g a" (using your example code) to initialize the top stack frame. The workaround in underlying C code would be to have "typeset -n r1=3Da" (again your example) implicitly create the global $a as early as possible. > f() { typeset a; unset a; g; echo "$a"; } > g() { h; } > h() { > typeset b=3Dx > typeset -n r1=3Da r2=3Db r3=3Dc > typeset a=3Dfoo c=3Dbar > r1=3Dx r2=3Dy r3=3Dz > unset r{1..3} > r1=3Dx r2=3Dy r3=3Dz > } > f > > r1=3Dx sets the $a of f, r2=3Dy sets the $b of h, and r3=3Dz sets a > global $c both times. It works that way for "the $a of f" and "the $b of h", but r3 assigns "the $c of h" both times. Change that to: h() { typeset b=3Dx typeset -n r1=3Da r2=3Db r3=3Dc typeset a=3Dfoo r1=3Dx r2=3Dy r3=3Dz typeset c=3Dbar unset r{1..3} r1=3Dx r2=3Dy r3=3Dz } And now the first r3=3Dz creates the global $c so the second r3=3Dz continues to refer to it.