G'MIC Scope Fun!

I don’t know where I’ve been all my life. This should have whacked me upside the head — oh, 8 or 9 years ago — but only just now have I sauntered into the ill-lit barn and stepped on the rake.
echobar.gmic:

echobar :
   bar={e}
   foobar
   echo "In scope: "$/". This is bar: "$bar

foobar :
      bar={3*4}
      echo "In scope: "$/". This is prefoolocal_bar: "$bar
      local[]
          bar="Great Scott, Watson! You have it!!!"
          echo "In scope: "$/". This is the foolocal bar: "$bar
      endlocal
      echo "In scope: "$/". This is the postfoolocal bar: "$bar

Let’s run echobar.gmic, shall we? Pay particular attention to the perambulations of $bar:

$ gmic verbose 3 echobar.gmic bar={pi} echobar echo "In scope: "$/". This is the commandline bar: "\$bar

This produces the shell log:

[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ Set verbosity level to 3.
[gmic]-0./ Input custom command file 'echobar.gmic' (2 new, total: 4458).
[gmic]-0./ Set local variable 'bar=3.1415926535897931'.
[gmic]-0./echobar/ Set local variable 'bar=2.7182818284590451'.
[gmic]-0./echobar/foobar/ Set local variable 'bar=12'.
[gmic]-0./echobar/foobar/ In scope: ./echobar/foobar/. This is prefoolocal_bar: 12
[gmic]-0./echobar/foobar/*local/ Set local variable 'bar=Great Scott, Watson! You have it!!!'.
[gmic]-0./echobar/foobar/*local/ In scope: ./echobar/foobar/*local/. This is the foolocal bar: Great Scott, Watson! You have it!!!
[gmic]-0./echobar/foobar/ In scope: ./echobar/foobar/. This is the postfoolocal bar: Great Scott, Watson! You have it!!!
[gmic]-0./echobar/ In scope: ./echobar/. This is bar: 2.7182818284590451
[gmic]-0./ In scope: ./. This is the commandline bar: 3.1415926535897931
[gmic]-0./ End G'MIC interpreter.

All the command line scopes have their own copy of $bar. I am amazed at myself for being amazed with this so late in the party. Can’t say the log doesn’t warn me. The phrase Set local variable appears four times. local as in: not global. Get it? Not me. Didn’t connect the dots — but now light dawns over Marblehead.

The scopes established by local…endlocal pairs do not have their own copies, however: foobar's $bar first acquires numeral 12, then a quote from Sherlock Holmes. 12 got trashed. There are scopes — and then there are scopes.

Methinks, perhaps, maybe Substitution Rules ought to have some explicit hints on scope and the relation of such with local variables. Or maybe that is written somewhere and I haven’t noticed. Certainly, I have a Cheatsheet in mind, at present ill-formed, and to be thrown on the TADOO stack for the present. No shortage of things to write about…

EDIT: poor word choice

Yes, It’s not really “each scope has its own definition of a local variable” , but “each command has its own definition of a local variable”.

That’s why a local variable defined in a command can be accessed in sub-scopes local...endlocal, if...fi, do...while, repeat...done and so on
(having local variables for those would be really annoying for coding things to be honest :slight_smile: ).

1 Like

In addition to this, if one wants to add local variable, one can always add _a or _b next to the name of the variable.