From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 6432 invoked from network); 26 Jan 2023 20:43:20 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 26 Jan 2023 20:43:20 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1674765800; b=JG6ksLLOUQXMJVabi5L8jTFvwcnYDN6FNtzv/PCcxATAenVLl79NVb/grGrTKX8g3ZRtq1l1FW wuyP+p+hQSooH0jsXbE2nD/gX4BrC3NinIWFeUphSmtLJKzLT4smmQ/6U0uqCoNfsaa2nKR9ca zE2VKMjYtNm6nEuntMqjV6JjWjjkjxBMs1jRI4WnSu5E4ki+GwFsjgFvC3EnBGwPSFkvJGfu0N 1K6Of37I/5toLsC49LfvXk+8cvpGgdmHMMTuahC8jsdeN7i/pFeBXXYwYxcc2jLGTJAfYzSXtw KqHfln8KHvBLFmB89SC/+w/t/h+LOtVltxSk80lSeYdFPA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f54.google.com) smtp.remote-ip=209.85.218.54; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 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=1674765800; bh=gwsIkDiTdCqBRBm7mfXw/IxthmoIcHyl7KhBP5BXD/g=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:To:Subject:Message-ID:Date:From:MIME-Version: DKIM-Signature:DKIM-Signature; b=HK907BSg9HThqs9ljsEN2n7dsT99S8MRvWoSMOMddAlfKQvrk517rZfIU4mPOC6bOkSc1nh6TL jek2YJimefodtMBmfiUP89H4N87WwQq5vLa0H6CKz3qQc4UK67QLZtvf8lZtVjYRTazMutdS44 m7q2YdPsWTPGCmupBdrEV3EkOgwp7dWYCYT4yNoNaQA4w9mQ3kCYNmG/xjg4XSMVP5s3OG/TZj 2Zi3bF3aIjVl4grH4nlCNTPk186oDEbMBK8fCp7xPIUyJJfMznI1ZZCq36XcR4HqvOJizVKwYP g7IDjDyca3tU++v9FGlUdxTth11+JHUdWjYRCxPMJa91dQ==; 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-Type:To:Subject:Message-ID: Date:From:MIME-Version:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=l7nac2aCWaVRVWb8GjgrzLnYCK08huPzSDjkdbK9vN4=; b=pCPFRwkN7qU/T2iuxNG2oLuaV+ zZXgNqVQiFWoZ59UvWN/d9sVbEB5PZ+E5LOQigPloUdJ99nFkMFuHylliVYKTrK+AJACVtXkqfQ11 nogCbT4oWOJ+n5mAh3EhWgw2vhRzqk24XqErurkLgzEGIBNkAw0IW/eaOTH5mGa0akkjoQhJmVrPg qzq7OyvA/2EwKyp+nlGG4F0lWryjtDhcFViAXFhJVeh8oVfkkMBxPhYoYAzN/4l4/XNy5Ee3Qk9bz sMSt8wDWkJQ3JIGIRHZJcwOieCKgcSF7EDtin0wrKsdQlELQuq3y9rDpn3MuO7s3xIqNNL5qkKWWu SINrQrww==; Received: by zero.zsh.org with local id 1pL95b-0000zY-UK; Thu, 26 Jan 2023 20:43:19 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f54.google.com) smtp.remote-ip=209.85.218.54; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ej1-f54.google.com ([209.85.218.54]:34578) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1pL94W-0000IF-BJ; Thu, 26 Jan 2023 20:42:13 +0000 Received: by mail-ej1-f54.google.com with SMTP id k4so2923183eje.1 for ; Thu, 26 Jan 2023 12:42:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20210112.gappssmtp.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=l7nac2aCWaVRVWb8GjgrzLnYCK08huPzSDjkdbK9vN4=; b=ariW3K1xz2u4HZJoOrMFERNH0rFdch57FbKmQh5xIYvwPxhJSadIoTn/FjkvSpncT9 EvQFphHp2Jjee1OA2U4XtncJVOVzooTCrPF+UHkWycAfhPG17cyNwHiL6AOyNOxpFxbj FWyO/x8DrvCoZzTjImTpqFf1iUA6Wi9kRhRzPx/m+1wK7Rpmg66u8+wrhVjywQ8c/9oJ 2hlI1V0YdrRZkcRV8bG9Vmf1sKXv5WcUKpQstrzBxh48HkuGUr2lsP50rr6z0BQI4vVo vQbCk8uO8EPm4H6gyEp2ixbIMinXp6fJSpr+RIBNMqPcWn8nRTgHXFPsF2urUxs0lCdX PKgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=l7nac2aCWaVRVWb8GjgrzLnYCK08huPzSDjkdbK9vN4=; b=0klF5Nis14mzFsCXHNRPal6G/X4ShhXb/R+Hf24lPYY/R9vSGJWXEAUPqRa/Tsw175 t1NzYO1RcF+44WBSQ2hzJUG2FWSIRTmZg07w/isHMj8duZkbqdfXKB+VNtG6ulm2mOka scx0GM+u0WbhbYXPDYQnNMBGSRxxkxZCqqLYfv7lP0jI+lcySUeDyU4mC88frFdQBw/s PpSYfYv6JlVWT5psx0YoY4wSX7cwrzRk8ohe+Ozg9wA/gX6d9v+ngKOgrWHGWqyswtuj WJhvq1vAnzjQh5HUZ8rBRweYOBHymScohdJlLkCOVneEqzJol5ZdQZdzjbyuDi1dxIRV UMqA== X-Gm-Message-State: AO0yUKUsdC8KcawT3t80jxyhBDJJ+mqYK5yL6L9V1K/gEE2yLY+6JkfH hPJJG6KWP3NG66ehW64OrwFIzAHxfFSc38vItr6lrTseWYOGCwVJ X-Google-Smtp-Source: AK7set84G7XDRz9ALIqXF0hpGn3RsjcC0NcFTf2FuwCdt1PE6CHk+IbJvibKxhsx1p3AAGz6YTqBM+C8eOdH45zIFvk= X-Received: by 2002:a17:906:c449:b0:878:4ee0:7ded with SMTP id ck9-20020a170906c44900b008784ee07dedmr1156186ejb.9.1674765731907; Thu, 26 Jan 2023 12:42:11 -0800 (PST) MIME-Version: 1.0 From: Bart Schaefer Date: Thu, 26 Jan 2023 12:42:00 -0800 Message-ID: Subject: Function wrappers around "typeset" To: Zsh Users Content-Type: text/plain; charset="UTF-8" X-Seq: 28816 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: Ordinarily if you use "typeset" in a function, any parameters it creates are in the current function scope and can't be seen outside. This can be amended by using "typeset -g" but then the parameter is created at the farthest enclosing scope where it already exists, or the true global scope if it doesn't exist anywhere in between. So what can you do if you want to create a parameter only one function scope up the stack? You can take advantage of the special semantics of the "trap" command. createparam() { emulate -L zsh trap 'typeset '${(j: :)${(qq)@}} EXIT } The drawback is that you can only use the syntax of the builtin typeset command, not those of the reserved word typeset -- so you CANNOT for example createparam -a foo=(a b c) Obviously this "createparam" is a silly example, why would you not make an alias or use typeset directly? Suppose you want to change or augment the arguments of typeset: tieparam() { emulate -L zsh trap 'typeset -T '${(j: :)${(qq)@}} EXIT } tieparam FOO=a:b:c foo It also works to push the typeset out of the caller's scope, e.g. tieparam -g FOO=axbxc foo x This "tieparam" is still a simple example, but there are other cases where "deferred typeset" might come in handy. Of course this same trap-EXIT trick works to defer any command to the calling scope as long as it's the last command the called function needs to perform (and you remember to carefully quote any parameter references to substitute them in the correct scope). Also be careful with the options (hence "emulate -L zsh") or your EXIT trap might remain set in a scope where you don't want it.