* Suggestions for autogenerating function names to wrap the same code?
@ 2022-03-22 20:52 Steve Dondley
2022-03-22 21:03 ` Peter Stephenson
2022-03-22 21:03 ` Mikael Magnusson
0 siblings, 2 replies; 8+ messages in thread
From: Steve Dondley @ 2022-03-22 20:52 UTC (permalink / raw)
To: zsh-users
So I have these two functions:
function task() {
task_wrapper.pl $funcstack[1] "$@"
}
function tc() {
task_wrapper.pl $funcstack[1] "$@“
}
They are both wrappers for the same perl script which does its thing based on the value of $funcstack[1];
Works, but I’m wondering if I can spare myself the job remembering to create a new function for each and every new perl function I want to write.
I could write a little script to pull out the subroutine names from the perl script and dump them into a file sourced by zsh and then reload zsh.
Wondering if there might be some cool feature of zsh I don’t know about to assist with creating these functions on-the-fly.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Suggestions for autogenerating function names to wrap the same code?
2022-03-22 20:52 Suggestions for autogenerating function names to wrap the same code? Steve Dondley
@ 2022-03-22 21:03 ` Peter Stephenson
2022-03-22 21:03 ` Mikael Magnusson
1 sibling, 0 replies; 8+ messages in thread
From: Peter Stephenson @ 2022-03-22 21:03 UTC (permalink / raw)
To: zsh-users
On Tue, 2022-03-22 at 16:52 -0400, Steve Dondley wrote:
> So I have these two functions:
>
> function task() {
> task_wrapper.pl $funcstack[1] "$@"
> }
>
> function tc() {
> task_wrapper.pl $funcstack[1] "$@“
>
> }
>
> They are both wrappers for the same perl script which does its thing
> based on the value of $funcstack[1];
>
> Works, but I’m wondering if I can spare myself the job remembering to
> create a new function for each and every new perl function I want to
> write.
>
> I could write a little script to pull out the subroutine names from
> the perl script and dump them into a file sourced by zsh and then reload
> zsh.
>
> Wondering if there might be some cool feature of zsh I don’t know
> about to assist with creating these functions on-the-fly.
Recent versions of the shell allow you to copy functions from an old
name to a new name. It's efficiently implemented internally.
% oldname() { print I am $0: }
% oldname
I am oldname
% functions -c oldname newname
% newname
I am newname
pws
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Suggestions for autogenerating function names to wrap the same code?
2022-03-22 20:52 Suggestions for autogenerating function names to wrap the same code? Steve Dondley
2022-03-22 21:03 ` Peter Stephenson
@ 2022-03-22 21:03 ` Mikael Magnusson
2022-03-22 21:09 ` Steve Dondley
` (2 more replies)
1 sibling, 3 replies; 8+ messages in thread
From: Mikael Magnusson @ 2022-03-22 21:03 UTC (permalink / raw)
To: Steve Dondley; +Cc: zsh-users
On 3/22/22, Steve Dondley <s@dondley.com> wrote:
> So I have these two functions:
>
> function task() {
> task_wrapper.pl $funcstack[1] "$@"
> }
>
> function tc() {
> task_wrapper.pl $funcstack[1] "$@“
>
> }
>
> They are both wrappers for the same perl script which does its thing based
> on the value of $funcstack[1];
>
> Works, but I’m wondering if I can spare myself the job remembering to create
> a new function for each and every new perl function I want to write.
>
> I could write a little script to pull out the subroutine names from the perl
> script and dump them into a file sourced by zsh and then reload zsh.
>
> Wondering if there might be some cool feature of zsh I don’t know about to
> assist with creating these functions on-the-fly.
You can define multiple functions like this:
function f1 f2 f3 f4() {
this body is shared by functions f1 f2 f3 and f4
}
and you can copy them like this (somewhat newer feature)
functions -c f1 g1
so if you can get all the function names in an array, you could just say:
task_func_names=( $(script to pull names out) )
function $task_func_names() {
task_wrapper.pl $funcstack[1] "$@"
}
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Suggestions for autogenerating function names to wrap the same code?
2022-03-22 21:03 ` Mikael Magnusson
@ 2022-03-22 21:09 ` Steve Dondley
2022-03-22 23:37 ` Steve Dondley
2022-03-22 21:16 ` Bart Schaefer
2022-03-22 23:28 ` Steve Dondley
2 siblings, 1 reply; 8+ messages in thread
From: Steve Dondley @ 2022-03-22 21:09 UTC (permalink / raw)
To: zsh-users
Nice. This looks promising. Thanks!
> On Mar 22, 2022, at 5:03 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
>
> On 3/22/22, Steve Dondley <s@dondley.com> wrote:
>> So I have these two functions:
>>
>> function task() {
>> task_wrapper.pl $funcstack[1] "$@"
>> }
>>
>> function tc() {
>> task_wrapper.pl $funcstack[1] "$@“
>>
>> }
>>
>> They are both wrappers for the same perl script which does its thing based
>> on the value of $funcstack[1];
>>
>> Works, but I’m wondering if I can spare myself the job remembering to create
>> a new function for each and every new perl function I want to write.
>>
>> I could write a little script to pull out the subroutine names from the perl
>> script and dump them into a file sourced by zsh and then reload zsh.
>>
>> Wondering if there might be some cool feature of zsh I don’t know about to
>> assist with creating these functions on-the-fly.
>
> You can define multiple functions like this:
> function f1 f2 f3 f4() {
> this body is shared by functions f1 f2 f3 and f4
> }
>
> and you can copy them like this (somewhat newer feature)
> functions -c f1 g1
>
> so if you can get all the function names in an array, you could just say:
> task_func_names=( $(script to pull names out) )
> function $task_func_names() {
> task_wrapper.pl $funcstack[1] "$@"
> }
>
> --
> Mikael Magnusson
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Suggestions for autogenerating function names to wrap the same code?
2022-03-22 21:03 ` Mikael Magnusson
2022-03-22 21:09 ` Steve Dondley
@ 2022-03-22 21:16 ` Bart Schaefer
2022-03-22 23:28 ` Steve Dondley
2 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2022-03-22 21:16 UTC (permalink / raw)
To: Mikael Magnusson; +Cc: Steve Dondley, Zsh Users
On Tue, Mar 22, 2022 at 2:06 PM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> so if you can get all the function names in an array, you could just say:
> task_func_names=( $(script to pull names out) )
> function $task_func_names() {
> task_wrapper.pl $funcstack[1] "$@"
> }
When using the "function" keyword, you don't need the empty parens,
either. That keyword further ignores the state of the MULTI_FUNC_DEF
option, which is what you want if you're using this array method.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Suggestions for autogenerating function names to wrap the same code?
2022-03-22 21:03 ` Mikael Magnusson
2022-03-22 21:09 ` Steve Dondley
2022-03-22 21:16 ` Bart Schaefer
@ 2022-03-22 23:28 ` Steve Dondley
2022-03-22 23:44 ` Steve Dondley
2 siblings, 1 reply; 8+ messages in thread
From: Steve Dondley @ 2022-03-22 23:28 UTC (permalink / raw)
To: zsh-users
OK, this is semi-working. The corresponding perl function is definitely getting called. If I it has a syntax error, STDERR gets output to the screen.
But for whatever reason, I can’t get any output on STDOUT with something as simple as:
sub blah {
system('echo', 'hi');
}
> On Mar 22, 2022, at 5:03 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
>
> On 3/22/22, Steve Dondley <s@dondley.com> wrote:
>> So I have these two functions:
>>
>> function task() {
>> task_wrapper.pl $funcstack[1] "$@"
>> }
>>
>> function tc() {
>> task_wrapper.pl $funcstack[1] "$@“
>>
>> }
>>
>> They are both wrappers for the same perl script which does its thing based
>> on the value of $funcstack[1];
>>
>> Works, but I’m wondering if I can spare myself the job remembering to create
>> a new function for each and every new perl function I want to write.
>>
>> I could write a little script to pull out the subroutine names from the perl
>> script and dump them into a file sourced by zsh and then reload zsh.
>>
>> Wondering if there might be some cool feature of zsh I don’t know about to
>> assist with creating these functions on-the-fly.
>
> You can define multiple functions like this:
> function f1 f2 f3 f4() {
> this body is shared by functions f1 f2 f3 and f4
> }
>
> and you can copy them like this (somewhat newer feature)
> functions -c f1 g1
>
> so if you can get all the function names in an array, you could just say:
> task_func_names=( $(script to pull names out) )
> function $task_func_names() {
> task_wrapper.pl $funcstack[1] "$@"
> }
>
> --
> Mikael Magnusson
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Suggestions for autogenerating function names to wrap the same code?
2022-03-22 21:09 ` Steve Dondley
@ 2022-03-22 23:37 ` Steve Dondley
0 siblings, 0 replies; 8+ messages in thread
From: Steve Dondley @ 2022-03-22 23:37 UTC (permalink / raw)
To: zsh-users
And here’s what I get with `type`:
> $ type tbbbc
tbbbc is a shell function from /User/me/.local/zsh/aliases/taskwarrior
So the zsh function generated is definitely recognized as a function.
> On Mar 22, 2022, at 5:09 PM, Steve Dondley <s@dondley.com> wrote:
>
> Nice. This looks promising. Thanks!
>
>> On Mar 22, 2022, at 5:03 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
>>
>> On 3/22/22, Steve Dondley <s@dondley.com> wrote:
>>> So I have these two functions:
>>>
>>> function task() {
>>> task_wrapper.pl $funcstack[1] "$@"
>>> }
>>>
>>> function tc() {
>>> task_wrapper.pl $funcstack[1] "$@“
>>>
>>> }
>>>
>>> They are both wrappers for the same perl script which does its thing based
>>> on the value of $funcstack[1];
>>>
>>> Works, but I’m wondering if I can spare myself the job remembering to create
>>> a new function for each and every new perl function I want to write.
>>>
>>> I could write a little script to pull out the subroutine names from the perl
>>> script and dump them into a file sourced by zsh and then reload zsh.
>>>
>>> Wondering if there might be some cool feature of zsh I don’t know about to
>>> assist with creating these functions on-the-fly.
>>
>> You can define multiple functions like this:
>> function f1 f2 f3 f4() {
>> this body is shared by functions f1 f2 f3 and f4
>> }
>>
>> and you can copy them like this (somewhat newer feature)
>> functions -c f1 g1
>>
>> so if you can get all the function names in an array, you could just say:
>> task_func_names=( $(script to pull names out) )
>> function $task_func_names() {
>> task_wrapper.pl $funcstack[1] "$@"
>> }
>>
>> --
>> Mikael Magnusson
>>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Suggestions for autogenerating function names to wrap the same code?
2022-03-22 23:28 ` Steve Dondley
@ 2022-03-22 23:44 ` Steve Dondley
0 siblings, 0 replies; 8+ messages in thread
From: Steve Dondley @ 2022-03-22 23:44 UTC (permalink / raw)
To: zsh-users
OK, sorry for the noise. I introduced a bug in the script which prevented any functions from getting called. Everything is good. Thanks again!
> On Mar 22, 2022, at 7:28 PM, Steve Dondley <s@dondley.com> wrote:
>
> OK, this is semi-working. The corresponding perl function is definitely getting called. If I it has a syntax error, STDERR gets output to the screen.
>
> But for whatever reason, I can’t get any output on STDOUT with something as simple as:
>
> sub blah {
> system('echo', 'hi');
> }
>
>
>> On Mar 22, 2022, at 5:03 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
>>
>> On 3/22/22, Steve Dondley <s@dondley.com> wrote:
>>> So I have these two functions:
>>>
>>> function task() {
>>> task_wrapper.pl $funcstack[1] "$@"
>>> }
>>>
>>> function tc() {
>>> task_wrapper.pl $funcstack[1] "$@“
>>>
>>> }
>>>
>>> They are both wrappers for the same perl script which does its thing based
>>> on the value of $funcstack[1];
>>>
>>> Works, but I’m wondering if I can spare myself the job remembering to create
>>> a new function for each and every new perl function I want to write.
>>>
>>> I could write a little script to pull out the subroutine names from the perl
>>> script and dump them into a file sourced by zsh and then reload zsh.
>>>
>>> Wondering if there might be some cool feature of zsh I don’t know about to
>>> assist with creating these functions on-the-fly.
>>
>> You can define multiple functions like this:
>> function f1 f2 f3 f4() {
>> this body is shared by functions f1 f2 f3 and f4
>> }
>>
>> and you can copy them like this (somewhat newer feature)
>> functions -c f1 g1
>>
>> so if you can get all the function names in an array, you could just say:
>> task_func_names=( $(script to pull names out) )
>> function $task_func_names() {
>> task_wrapper.pl $funcstack[1] "$@"
>> }
>>
>> --
>> Mikael Magnusson
>>
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2022-03-22 23:47 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-22 20:52 Suggestions for autogenerating function names to wrap the same code? Steve Dondley
2022-03-22 21:03 ` Peter Stephenson
2022-03-22 21:03 ` Mikael Magnusson
2022-03-22 21:09 ` Steve Dondley
2022-03-22 23:37 ` Steve Dondley
2022-03-22 21:16 ` Bart Schaefer
2022-03-22 23:28 ` Steve Dondley
2022-03-22 23:44 ` Steve Dondley
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).