bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: declare -F incorrect line number


From: Martin D Kealey
Subject: Re: declare -F incorrect line number
Date: Fri, 7 Oct 2022 01:28:59 +1000

I write nested functions quite often, usually with a subsequent `unset -f`
but sometimes (necessarily) without.

Being able to write `local -F funcname { ... }` or `function -L funcname {
... }` would be a nice replacement for the former, but the latter is
usually about different phases of execution, rather than abstractions for
different data.

For example, when writing an explicit shift-reduce parser as part of a tab
completion function, the logic for "match a token" depends on both the kind
of token expected, whether or not we've arrived at the word that's the
target of the tab expansion, and how many times the tab key has been
pressed. Being able to redefine the various "match a token" functions by
calling a single function makes for a much less cluttered grammar
definition.

It would be helpful for the caller function to return the correct function
name and source filename and line, though I can see the latter being
somewhat tricky if it's created inside an eval.

-Martin

On Thu, 6 Oct 2022 at 08:29, Robert Elz <kre@munnari.oz.au> wrote:

>     Date:        Wed, 5 Oct 2022 15:36:55 -0400
>     From:        Chet Ramey <chet.ramey@case.edu>
>     Message-ID:  <3d89acac-4c0a-64c9-e22c-1a3ca68600d1@case.edu>
>
>   | Other than that, there's no advantage.
>
> There can be.   I have, on occasion (not in bash - I don't
> write bash scripts) had a need to redefine one of the standard
> commands, while executing a particular function (which calls other
> more standard functions which run the command) - and define the
> same command differently when running a different function, which
> runs the same standard functions running the command, but in a
> different way.
>
> Kind of like
>
>         f1() {
>                 diff() { command diff -u "$@"; }
>                 dostuff
>                 unset -f diff
>         }
>
>         f2() {
>                 diff() { command diff -iw -c "$@"; }
>                 dostuff
>                 unset -f diff
>         }
>
> where dostuff() does what ever is needed to make "newversion",
> and then, somewhere does one (or more) of something like
>
>         diff origfile newversion
>
> "dostuff" can also just be run to get the default diff format.
>
> or something like that.   Real examples tend to be far more complicated
> (this simple case could be done just by having DIFFARGS or something, but
> that would mean modifying dostuff() to use that as diff $DIFFARGS ....)
>
> kre
>
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]