Table of Content

This article is one part of a collection. All integrated, one related to another, featured with summary. So we can compare each other quickly.

Tutorial/ Guidance/ Article:

[ Tag Status Overview ]  [ BASH ]  [ Perl ]  [ Python ]  [ Ruby ]  [ PHP ]  [ Lua ]  [ Haskell ] [ Global Notes ]

Example Using Dzen2:

[ BASH ]  [ Perl ]  [ Python ]  [ Ruby ]  [ PHP ]  [ Lua ]  [ Haskell ]

Example Using Lemonbar:

[ BASH ]  [ Perl ]  [ Python ]  [ Ruby ]  [ PHP ]  [ Lua ]  [ Haskell ]

Global Variable and Constant

Why discuss about Coding here ?

Because I also include Haskell. And Haskell distinct clearly between global constant and mutable state.

Global variable is requirement in this project

Some say that global variable are bad. But there are quick and dirty condition, where global variable looks more suitable. The other way is change the entire logic design.

This output module require a variable that can be called from function, without passing them as a parameter argument. So we have to make scope of these variable global. But since these variables live only in output module, these are not so global after all.

Before getting deep, we need to distinct between global variable and global constant.

  • global variable: mutable state, must be initialized.

  • global constant: it is usually defined as a global variable, except that, the value won’t be altered during script execution. The value is defined at the beginning of program.

Global Variable

Accessing a global variable from within function need different trick for each language.

  • BASH: Variable is global by default.

  • Perl: Variable defined using my can be accessed within function in the same module.

  • Python: Access within function using global

  • Ruby: In Module scope, variable defined using @ can be accesed within function.

  • PHP: Access within function using global

  • Lua: Variable scope using table module such as _M.

  • Haskell: Officialy there is a no way to define global variable Haskell. But Haskell provide a few workaround to simulate mutable state. The easiest one is using unsafePerformIO.

Global Constant

Each language maintain global constant differently

  • BASH: There are ways to define constant in BASH. Constant in PHP may begin with the word readonly.

  • Perl: There are a several ways to define constant in Perl. Constant in Perl may begin with the word use const.

  • Python: Officialy there is a no way to define constant in Python. Python does not differ between these two.

  • Ruby: Constant in Ruby start with capital case.

  • PHP: Constant in PHP begin with the word const.

  • Lua: Officialy there is a no way to define constant in Lua. Lua does not differ between these two.

  • Haskell: Every variables in Haskell are immutable.

Global Conclusion

There is no special need to worry about global variable, except with Haskell.


Blog Post

The rest is in their respective article.

Let's get it started.

Dotfiles (Dzen2 Source Code)

Dotfiles (Lemonbar Source Code)


Thank you for Reading.