From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: zsh-users-return-23612-ml=inbox.vuxu.org@zsh.org X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.1 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id e1cb327f for ; Sun, 9 Sep 2018 03:24:34 +0000 (UTC) Received: (qmail 16052 invoked by alias); 9 Sep 2018 03:24:20 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 23612 Received: (qmail 4539 invoked by uid 1010); 9 Sep 2018 03:24:20 -0000 X-Qmail-Scanner-Diagnostics: from mail-lf1-f52.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(209.85.167.52):SA:0(-1.9/5.0):. Processed in 1.768749 secs); 09 Sep 2018 03:24:20 -0000 X-Envelope-From: schaefer@brasslantern.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=+E+lXeLZERhoHqnxIOi+vm+NxKFQXpZ5hSBIvJmGgtI=; b=y8UvRY39/SDb2RKQrvJ+GsGObdhb1AMNrQ1QYtoTZfnakBIYl3ZKMi4t4qwCu8FCk1 +4zc3uuqpi+KAvW7ttyqjbdTobQFo6KfpuDpa00asLW8HInfL+09fSXjUlEL8Jn78l7v nhlvrjv9oV3Zoabi8EEBzRS6regfl9l9SYFicR6BjjIkglibnBVZ+Cqmo4a6VMmVEfSP PlOwyhOZ8iffsXl0lff5oC0k+nQerAviOUfUMO6sn+PzSd/aMrsaM1s0K6CVgNS+LuiM GfBkpHPOcGf7N5NfGRjY76mAFNw/KmCjcKJi7m8glag3HsycPZD2RGtT33s6wqwedQ8j E93g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=+E+lXeLZERhoHqnxIOi+vm+NxKFQXpZ5hSBIvJmGgtI=; b=BpH8tYsVnsO2APSsDJ1v6vVHrhv+PFbf9mKYYOwiCNkp3ZJWBMajuTogGNZk7BygLi +qvhnpXhmsoe4E75wKAeHDjC5fZicYEaCf7NmuqiS+y60aYr6n2QEVbwKx6YrTVlSZGI +CRxQUxMtus4ADP3SSQSL0zmxhPHGGbKLDAuUoUf4+9LuXE7DFQmfBzdtyh2vSDEbDyn LlpeLwkkGz4bPF7k9YlrhHzOR+kfIBT0NApjn/gxleRpq935aYF6S4aT7lP2eOx2Age+ R7YWAlgmSpT8EPzUYFAIRovbA/haTDR160Hu2lKO/LqljjjC5C5E5eRepe6o8Qj+7vRE ZISA== X-Gm-Message-State: APzg51CDHZpedOR6rbcKzUgCnm8qp6y61eiia8qt2yVFZj+TT/T5Br6a 3m2oh4Gdvo1/exXRIg3BM4oxhKms+favAculvi+e7w== X-Google-Smtp-Source: ANB0VdaG3ME+hsDRrIW6KBMiFGmRKV1CFdRNywA4C4dcaunKBl/Fgc3HF4yloTbKR9DY2IoCGS/1nEvpLnvV5nPPoy8= X-Received: by 2002:a19:5353:: with SMTP id h80-v6mr9405586lfb.9.1536463454024; Sat, 08 Sep 2018 20:24:14 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Bart Schaefer Date: Sat, 8 Sep 2018 20:24:13 -0700 Message-ID: Subject: Re: How do you decide whether to make something a function or a script? To: TJ Luoma Cc: Zsh-Users List Content-Type: text/plain; charset="UTF-8" On Sat, Sep 8, 2018 at 6:22 PM, TJ Luoma wrote: > > Why make any functions at all? Why not just make them all scripts? The primary reason to make a program into a function is when it needs to alter the state of the parent shell. User-defined ZLE widgets are the most obvious example, but there are other cases to want to make persistent changes to the current shell. (Yes, you can get some of this with "source", but then you have to worry about parameter scoping, etc.) A secondary reason is efficiency. Running a script requires forking a subshell, examining the #! line to see if an external interpreter is needed, and parsing the code, all before the script can begin doing anything, and this happens every time it is run. With functions, there's no subshell, no guessing at the interpreter, and all the parsing has been done up front (or is done only once on the first run, in the case of an autoload). Another reason is modularity. A single file can define several related functions, which leverages both of the first two advantages. Yet another reason is to facilitate recursive programming. A script that runs itself creates many subshells, but a function that calls itself only uses some recoverable memory space in the current shell. There are more reasons, but most of them begin to be stylistic rather than practical. I'm sure others can add to my list.