zsh-users
 help / color / mirror / code / Atom feed
* How to have function local variable but must behave just a simple one
@ 2023-08-26  7:25 Budi
  2023-08-26  7:34 ` Roman Perepelitsa
  0 siblings, 1 reply; 8+ messages in thread
From: Budi @ 2023-08-26  7:25 UTC (permalink / raw)
  To: Zsh Users

How to have function local variable but must behave just a simple /
plain variable which is retaining its value all the time, no matter
how many times exit and enter the function


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to have function local variable but must behave just a simple one
  2023-08-26  7:25 How to have function local variable but must behave just a simple one Budi
@ 2023-08-26  7:34 ` Roman Perepelitsa
  2023-08-26 20:26   ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Roman Perepelitsa @ 2023-08-26  7:34 UTC (permalink / raw)
  To: Budi; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 456 bytes --]

On Sat, 26 Aug 2023 at 10:26, Budi <budikusasi@gmail.com> wrote:

> How to have function local variable but must behave just a simple /
> plain variable which is retaining its value all the time, no matter
> how many times exit and enter the function


Sort of like a function-scoped static variable in C?

    int counter() {
      static int n = 0;
      return ++n;
    }

Zsh doesn't have the equivalent. You'll have to use a global parameter.

Roman.

[-- Attachment #2: Type: text/html, Size: 1389 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to have function local variable but must behave just a simple one
  2023-08-26  7:34 ` Roman Perepelitsa
@ 2023-08-26 20:26   ` Bart Schaefer
  2023-08-26 20:38     ` Ray Andrews
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2023-08-26 20:26 UTC (permalink / raw)
  To: Budi; +Cc: Zsh Users

On Sat, Aug 26, 2023 at 12:34 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> Zsh doesn't have the equivalent. You'll have to use a global parameter.

More generically speaking, shells typically use dynamic scoping rather
than lexical scoping, which means a variable only persists until the
scope where it is declared finishes, and while it persists it is
available to all deeper scopes.  The zsh/param/private module changes
some of the availability rules, but nothing changes the persistence.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to have function local variable but must behave just a simple one
  2023-08-26 20:26   ` Bart Schaefer
@ 2023-08-26 20:38     ` Ray Andrews
  2023-08-26 20:58       ` Roman Perepelitsa
  0 siblings, 1 reply; 8+ messages in thread
From: Ray Andrews @ 2023-08-26 20:38 UTC (permalink / raw)
  To: zsh-users


On 2023-08-26 13:26, Bart Schaefer wrote:
> On Sat, Aug 26, 2023 at 12:34 AM Roman Perepelitsa
> <roman.perepelitsa@gmail.com> wrote:
>> Zsh doesn't have the equivalent. You'll have to use a global parameter.
Why not have a global variable and then just make the assignment to a 
local variable at each call of the function and update the global when 
and where desired or let the local variable just die otherwise?  I 
suspect I don't really understand the problem.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to have function local variable but must behave just a simple one
  2023-08-26 20:38     ` Ray Andrews
@ 2023-08-26 20:58       ` Roman Perepelitsa
  2023-08-26 21:10         ` Ray Andrews
  0 siblings, 1 reply; 8+ messages in thread
From: Roman Perepelitsa @ 2023-08-26 20:58 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

On Sat, Aug 26, 2023 at 10:39 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> On 2023-08-26 13:26, Bart Schaefer wrote:
> > On Sat, Aug 26, 2023 at 12:34 AM Roman Perepelitsa
> > <roman.perepelitsa@gmail.com> wrote:
> >> Zsh doesn't have the equivalent. You'll have to use a global parameter.
>
> Why not have a global variable [...]

Indeed, given that in zsh there are no function-scoped parameters that
outlive an invocation of the function, Budi will have to use a global
parameter and take necessary precautions to ensure the name of the
parameter does not clash with other globals.

Roman.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to have function local variable but must behave just a simple one
  2023-08-26 20:58       ` Roman Perepelitsa
@ 2023-08-26 21:10         ` Ray Andrews
  2023-08-26 21:13           ` Roman Perepelitsa
  2023-08-27  0:50           ` Budi
  0 siblings, 2 replies; 8+ messages in thread
From: Ray Andrews @ 2023-08-26 21:10 UTC (permalink / raw)
  To: zsh-users


On 2023-08-26 13:58, Roman Perepelitsa wrote:
>
> Indeed, given that in zsh there are no function-scoped parameters that
> outlive an invocation of the function, Budi will have to use a global
> parameter and take necessary precautions to ensure the name of the
> parameter does not clash with other globals.
>
> Roman.

I'm puzzled as to what the problem really is from a practical point  of 
view.  Easy enough to create a 'safe' name, no?  I suppose for  tidiness 
sake one might not want variables specific to only one function to be 
global, but there's zillions of that sort of variable already so one 
lives with that rather easily. And this is universal across all shells, 
yes?  I have a set of local variables that I need to survive reboot, so 
I write 'em to a file which is reread on startup.  That would work for 
Budi, but again it seems like there's not really a big problem.  (Mind, 
I have a long history of seeing problems where there is no problem ;-)





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to have function local variable but must behave just a simple one
  2023-08-26 21:10         ` Ray Andrews
@ 2023-08-26 21:13           ` Roman Perepelitsa
  2023-08-27  0:50           ` Budi
  1 sibling, 0 replies; 8+ messages in thread
From: Roman Perepelitsa @ 2023-08-26 21:13 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

On Sat, Aug 26, 2023 at 11:11 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> On 2023-08-26 13:58, Roman Perepelitsa wrote:
> >
> > Indeed, given that in zsh there are no function-scoped parameters that
> > outlive an invocation of the function, Budi will have to use a global
> > parameter and take necessary precautions to ensure the name of the
> > parameter does not clash with other globals.
> >
> > Roman.
>
> I'm puzzled as to what the problem really is from a practical point  of
> view.

You aren't missing anything.

Roman.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to have function local variable but must behave just a simple one
  2023-08-26 21:10         ` Ray Andrews
  2023-08-26 21:13           ` Roman Perepelitsa
@ 2023-08-27  0:50           ` Budi
  1 sibling, 0 replies; 8+ messages in thread
From: Budi @ 2023-08-27  0:50 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

Aim to have a global var. name protected, it's in PROMPT inevitably
require full private protective level from interactive-shell actions


use below to understant what it means

https://github.com/abdulbadii/smart-directories-navigation


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-08-27  0:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-26  7:25 How to have function local variable but must behave just a simple one Budi
2023-08-26  7:34 ` Roman Perepelitsa
2023-08-26 20:26   ` Bart Schaefer
2023-08-26 20:38     ` Ray Andrews
2023-08-26 20:58       ` Roman Perepelitsa
2023-08-26 21:10         ` Ray Andrews
2023-08-26 21:13           ` Roman Perepelitsa
2023-08-27  0:50           ` Budi

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).