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 E381E25196 for ; Sun, 11 Feb 2024 08:10:22 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1707635422; b=XTDhtvU7KoYMznAyTItJKv+mRvzQVtcHGKn56znOZCn2jw3Vt/KB9tdnTB/tgBQutoCTfRJTCr M4RwJwTGIrT97MgnLuP/yKigQoJWUWWIaZlEzdxJAJ01j8b9O6ulVmaELYFSSE36TJPgqkfTZ+ Bz2OqOfVIYE8N4rx26jH1WWmTs/TW5sbXvGSSMtkT6XDF1aZL/+Jgtnjz9AmGIIP6D0hVbTD/9 446EvgbAaM7nCJs8YEexCcpx0tRiyvVkQrBZtEYyH6pVDHD4GMDaLCCqMUNnfRuEassg2ogqKw s+pH0pEaiToZtWdNY1I8190njYwkC4JvtR4skMM+HLG9iQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay3-d.mail.gandi.net) smtp.remote-ip=217.70.183.195; 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=1707635422; bh=mw6sCH+/0JRF4NngoQGv9qFzkyi3wHNKIfsHgBQihCo=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:DKIM-Signature; b=QdiOfwI9VzI33wkY7dgjL8ags5Pft/CTHV1jDFkZXXpVALjaPoEpy5uOscTdksx/8Zwm27vP/U QW+bLFUoZwjwk37KsWfI3at/TdUUXpBg8PQzLMBZWVoUJvwYRQUooZXPTR8QVQBARfx2TnNHaX /2p8uEAwhoL/FekYov2LL+MF2P1Wv7CKTjTfzQNr5QZbPbt2X3zPaNtnzf+I7uAFi0vDaUeJnW G1jm9+yqNlnJ+l8PmYIZw3vzh66K6uWR/KgdsIGKrPDacITOFBAioF7+i1IqlUUMoQ3zgJxcuV sJRcLc+gEXJmCak6CE6aoGHHlZElmokbtTUYkK0dkA97Kw==; 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-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID; bh=OWvjnxxJTDlP81l2RtMA56IFkunTr6+f4/vz2Ni5+tY=; b=l30pK0V8mPo/LRb3zx0AVCt6EP a/3zrxPS44X6dxRzPuWLxK0h/x2ZcW84xF41gOIQSwGSpCb9e139VMt4OxTkEFeiXpWDm2s7o9Bla 0SMQ3ce6dKns342oXkhl5imIm24luuJ1gC991HT7eUinwFtbAAD07l2+DVltE22bYb/kmaYp9B/xa nlqucu9CgVJF7RtS/pj/afexAxVip5Wno0roKZnyEOnLBNqw5jvB9aplRGaUgKz1H5u08gGkiUL9O CkuyVx6P94x78CHaaDLTbJ25QrFFGJ6tfUbaHCW5bR+J5xqvK6gBU+HFfNqAkzvfap9SiwLNnZGf3 yc29DQ3w==; Received: by zero.zsh.org with local id 1rZ3yo-000CJT-89; Sun, 11 Feb 2024 07:10:22 +0000 Authentication-Results: zsh.org; iprev=pass (relay3-d.mail.gandi.net) smtp.remote-ip=217.70.183.195; dmarc=none header.from=chazelas.org; arc=none Received: from relay3-d.mail.gandi.net ([217.70.183.195]:52391) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1rZ3yG-000Bd6-90; Sun, 11 Feb 2024 07:09:49 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 62DD160003; Sun, 11 Feb 2024 07:09:47 +0000 (UTC) Date: Sun, 11 Feb 2024 07:09:46 +0000 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh Users Subject: Re: Slurping a file (was: more spllitting travails) Message-ID: <20240211070946.3owqbuz75vxvn7iw@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh Users References: <205735b2-11e1-4b5e-baa2-7418753f591f@eastlink.ca> <20240210204803.2ogpi5h752uzwf6e@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-GND-Sasl: stephane@chazelas.org X-Seq: 29657 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: 2024-02-10 22:46:15 -0600, Bart Schaefer: > On Sat, Feb 10, 2024 at 2:48 PM Stephane Chazelas wrote: > > > > IMO, it would be more useful if the result was returned in the > > variable whose name was given as argument (defaulting to REPLY > > if none was given like for read or sysread). > > Could also read a file provided by name as an argument instead of only > reading stdin, but I elected to commit the most straightforward > version. Yes, using redirection as in: zslurp a < file1 zslurp b < file2 is easy enough > > > And would be better if upon error the returned variable > > contained either what was successfully read or nothing (like > > read but unlikely sysread). > > I had the impression this slurp-er was intended to work like examples > from other languages, which do not have "read"-like behavior. It sounded safer to me to avoid leaving the variable unmodified if the input could not be read in case the caller forgets to do error handling, but then again in zslurp a < file1 above, if file1 cannot be opened, zslurp won't be called so $a will be left unmodified regardless (same in read var < file1), so yes, probably pointless. > > > zslurp() { > > emulate -L zsh -o no_multibyte > > typeset -n _zslurp_var=${1-REPLY} > > Is there really any reason to prefix the locals with "_zslurp_" ? > That's good practice if the function might call other code that's less > careful about it's names and scoping, or if you need to the variable > to become global, but nothing like that occurs here. See https://zsh.org/workers/52530 >From my quick testing I had assumed zsh's nameref worked like bash ones and were just a lexical dereferencing, hence the namespacing. But it seems that is only needed in cases where referenced variables were not declared in the caller. If workers/52530 is addressed, that namespacing is no longer needed. -- Stephane