Closed schittli closed 3 years ago
Hi,
A simple solution would be to generate this value once before calling pp. e.g. on bash with $RANDOM
: pp -DGUID=$RANDOM
. Then !GUID
is a constant always returning the same number.
➜ echo '!UID !UID' | pp -DUID=$RANDOM
19090 19090
When a macro is called its value is recomputed by evaluating its definition. So if you define:
!define(GUID)(
!ifndef(_GUID)(!define(_GUID)(!sh(echo $RANDOM)))
!_GUID
)
Then _GUID
is defined once but its definition is !sh(echo $RANDOM)
and you get a new random number each time you call GUID
.
pp
evaluation is sometimes a bit tricky.
I have written a much simpler preprocessor (https://github.com/CDSoft/upp) that can be scripted in Lua.
e.g.:
:(guid = math.random())
$(guid)
$(guid)
$(guid)
or with an external command (bash, powershell, ...):
:(guid = io.popen("echo $RANDOM"):read())
$(guid)
$(guid)
$(guid)
:(...)
is a block of Lua code that is executed but nothing is returned to the document (unless the block explicitly returns a non nil value).
$(...)
is a Lua expression which value will appear in the document.
Thank you very much, @CDSoft, for your great support!
I'm still studying UPP as well - generic text preprocessors are a tricky and surprisingly exciting topic... and very useful :-)
Good evening
It is an excellent feature that we can execute a PowerShell script with
!powershell(...)
and then use the result in PP macros.This way, we can e.g. create a new GUID every time we call the PP macro:
Is it somehow possible to evaluate the result of a shell call only once and reuse the same result multiple times?
I tried to use
!quiet()
and!eval()
combined with!ifdef()
but I did not found a working solution. It should act as following:My failed tests
I tried to use
!quiet()
and!pp()
, but it does not work :-(Thanks a lot for any help, kind regards, Thomas