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 C99742B16B for ; Tue, 12 Mar 2024 09:49:49 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1710233389; b=kM1KLcXXEsdO3Y4I5ubDeUVXIFxGQ41ahgARGdgOVWZOHoNqREE8aNbOrM0X8S08pHWrlizVqH TBIRBjYV9YLc+ScKXT7MYgE6vlTMMxwLB2kQa8tsQRdi+f21j22kdrZgfNFObDYbMqO/COTXqM caIrWb4atQk0zYO0uxkmP9epCDJwE4AnJPWMJHzdF+l4nVOELEEwn0O2+MVr+fHMpECsptiTCf N0iEM1+AWmNAp4wxQEBY/SC2WrO7m9AAkF09FbEgkMIBloeZopUGYYXTpkWn+jCeoUqErtjGvF 0GX8OJA6cqXsoTNUXDL1zPghMHtfOZAOOto6/Ws7+4HEuQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay6-d.mail.gandi.net) smtp.remote-ip=217.70.183.198; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1710233389; bh=eb+qy4tIBd05uz4tGAOwxbQ2CggUGmnmQ4STwj2YnDE=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=fCxvwL9yvsIIApIWrRdc9in1I28X6Fw52x8HRy9M78p1NR7YAXNX2+ZUHz/AVAUqQWQZRskMWQ +7KPynIERpEYSau7RV/SnI8c1vFi0U3oVIS8cXK8NtjBUQE1DdJkMbnXhK2qaDiyQT0KrwDYyN dZ9y/N07nHeCvZ+Z9AwSyUH+3iUlHyPA/m9opP1aJMrMMS4baWODVtkOSTTdd8FU1gbGCUtcNd c4NV2kMlFJUtdH6nM4aTIGnyvkn60alWmKxdT7qHC+wwi0p0S7kNcRNzsVEeIQKAxAXIh59k8K a82h3AtMuJMlyynP9Hy5ZvfRxgnP1axmcRDogsc2kVqIgg==; 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:In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=TfHFnQsLsRSjU8k65sf4zPNFAIU8E0dRsKxO/28m1tA=; b=m2zItlti4R/MZFWwD0pgTgvez6 ZMl1qxXraC+dfTeaYRJYbtSftDalVAI3K3rGS2H2HybSOa0nMZO/5rVeaY2NV+7jlv87141+Ttt5I gC4EZonSRwfyimMORqjhl0lp+m6o5Q2e7XCKtpuU07is989teIATMdb97N8hpXGNSQ+FbNsVgkb+I xfVHO+aUsaBCFzGkxugW4nExmjHv+vjZRFdZdsIhTP7+0Hm4esbtQinCnsZ8LQ69NDGgF4wMbs8FE 4R4r89LE53eOKT7GmqPM7GAXkT6BsLdqabWrq6cC6DIXBsmno7eVdr/JO1Knm2S42FWGjGccrN7id IGmILtqQ==; Received: by zero.zsh.org with local id 1rjxpU-00056N-Ig; Tue, 12 Mar 2024 08:49:48 +0000 Authentication-Results: zsh.org; iprev=pass (relay6-d.mail.gandi.net) smtp.remote-ip=217.70.183.198; dmarc=none header.from=chazelas.org; arc=none Received: from relay6-d.mail.gandi.net ([217.70.183.198]:46769) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1rjxpA-0004n7-JI; Tue, 12 Mar 2024 08:49:29 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 7A68EC000E; Tue, 12 Mar 2024 08:49:27 +0000 (UTC) Date: Tue, 12 Mar 2024 08:49:26 +0000 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh hackers list Subject: Re: "typeset -p" and no_GLOBAL_EXPORT, other misc. Message-ID: <20240312084926.d6vbk75ozud7i2jm@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh hackers list References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-GND-Sasl: stephane@chazelas.org X-Seq: 52729 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: 2024-03-11 21:13:32 -0700, Bart Schaefer: [...] > To even start fixing this mess, we'd have to explain what a function > considers "local" and what "global". Are all "inherited" scopes > global, so local is the current scope only? Gaah. [...] IMO, typeset -g should only mean: don't make it local, do not instantiate a new variable in the current scope. And we certainly don't want bash's typeset -g behaviour where it means *the outermost scope*, which in shells with dynamic scoping is essentially what "global" means, but it's not particularly useful to single it out. For instance, in bash: bash-5.3$ integer() { typeset -gi -- "$@"; } bash-5.3$ integer i=1+1 bash-5.3$ echo $i 2 fine, but: bash-5.3$ f() { local i; integer i=2+2; echo "$i"; } bash-5.3$ f bash-5.3$ echo $i 4 That "integer" changed the type of the global (outer-most) i variable instead of that of its caller. IOW, most of the usages of "typeset -g" like with that "integer" above is when you want to change attributes of a variable (set the type with typeset which bash renamed to declare) without wanting to make it local, like export or readonly do in Bourne-like shells (except zsh for the latter), not change the type of the variable in the outermost scope which I can't think why you would want to. In mksh or yash, readonly var is like typeset -gr var. In bash, readonly var is like mksh/yash's typeset -gr var, but unlike its own typeset -gr: ~$ bash -c 'b() { local a=1; a; echo $a; }; a() { readonly a=2; echo $a; }; a=0; b; echo $a' 2 2 0 ~$ bash -c 'b() { local a=1; a; echo $a; }; a() { typeset -gr a=2; echo $a; }; a=0; b; echo $a' 1 1 2 ~$ mksh -c 'b() { local a=1; a; echo $a; }; a() { readonly a=2; echo $a; }; a=0; b; echo $a' 2 2 0 ~$ mksh -c 'b() { local a=1; a; echo $a; }; a() { typeset -gr a=2; echo $a; }; a=0; b; echo $a' 2 2 0 In zsh, readonly var, when not emulating other shells is more like typeset -r: ~$ zsh -c 'b() { local a=1; a; echo $a; }; a() { typeset -gr a=2; echo $a; }; a=0; b; echo $a' 2 2 0 ~$ zsh -c 'b() { local a=1; a; echo $a; }; a() { readonly a=2; echo $a; }; a=0; b; echo $a' 2 1 0 ~$ zsh -c 'b() { local a=1; a; echo $a; }; a() { typeset -r a=2; echo $a; }; a=0; b; echo $a' 2 1 0 ksh93 does static scoping, so things are different there anyway, there's only a global scope and one local scope not a stack of them, so -g can mean "global" without ambiguity and functions cannot access the variables of their caller unless they're exported. ~$ ksh -c 'function b { typeset a=1; a; echo $a; }; function a { typeset -gr a=2; echo $a; }; a=0; b; echo $a' 2 1 2 ~$ ksh -c 'function b { typeset a=1; a; echo $a; }; function a { readonly a=2; echo $a; }; a=0; b; echo $a' 2 1 2 ~$ ksh -c 'function b { typeset -x a=1; a; echo $a; }; function a { readonly a=2; echo $a; }; a=0; b; echo $a' 2 1 0 ~$ ksh -c 'function b { typeset -x a=1; a; echo $a; a=3; }; function a { readonly a=2; echo $a; }; a=0; b; echo $a' 2 1 0 readonly is still not equivalent to typeset -gr as it affects the variable in the scope where it currently exists (whether local, global or exported from an ancestor), not only in the global scope: $ ksh -c 'function a { typeset a=1; typeset -gr a; echo $a; }; a=0; a; echo $a; a=2' 1 0 ksh: a: is read only $ ksh -c 'function a { typeset a=1; readonly a; echo $a; }; a=0; a; echo $a; a=2' 1 0 -- Stephane