Looking for a cure for weird scope/memory of variables

Lawrence Velázquez larryv at macports.org
Tue Jul 16 11:50:43 PDT 2013

On Jul 13, 2013, at 2:48 PM, Gustaf Neumann <neumann at wu.ac.at> wrote:

> In general, the better strategy seems to for me to avoid the
> definition-time substitutions of the subport body at all.
> This could be achieved with an associative array
> indexed by the version numbers. When we have an array
>    array set foo {
>       1.1,version "bla 1"
>       2.0,version "bla 2"
>    }
> and we assume, the subport version number is available
> at the execution time of "pre-fetch" etc. as global
> variable "subport"

It isn't. The "subport" variable in this case would be "foo-1.1" or what have you. It's easy enough to extract the version, though. Or use the full subport strings as the array keys.

> one could use the following:
>    foreach {foo.version foo.string} ${foo.versions} {
>        subport foo-${foo.version} {
>            pre-fetch {
>                 system "echo $foo($subport,version)"
>            }
>            fetch {}
>            extract {}
>            use_configure no
>            build {}
>            destroot {}
>        }
>      }
>    }
> This works, since the bodies of "pre-fetch" etc. see all
> global variables including the associative array.

I don't see this extending cleanly to multiple substitutions (which Mojca is doing in the actual Portfile), since you're only allowed one value per array key. I suppose you could have something like this:

    array set foo {
        foo-1.1,val1 "abc"
        foo-1.1,val2 "def"
        foo-2.0,val1 "ijk"
        foo-2.0,val2 "xyz"

and use $foo($subport,val1) and $foo($subport,val2) as necessary in the pre-fetch script:

    subport foo-${foo.version} {
        pre-fetch {
            system "echo $foo($subport,val1)"
            system "echo $foo($subport,val2)"

Frankly, the pre-fetch script starts to get very ugly at this point. The benefit of the "string map" substitution route is that the actual script is the same as it would be otherwise; it just gets wrapped in some eval wand-waving.


More information about the macports-dev mailing list