Parse Time vs. Execution Time [closed]

最后都变了- 提交于 2019-11-30 20:56:46

问题


I am trying to learn MS Batch, and I was specifically trying to understand the "setlocal" and the "enabledelayedexpression" aspects, when I came across vocabulary I did not understand:

execution time and parse time


回答1:


The parser has different phases, when parsing a single line.
So the percent expressions are all expand when a line or block is parsed, before the line (or any line in a block) is executed.

So at execution time they can't change anymore.

set var=origin
echo #1 %var%
(
  set var=new value
  echo #2 %var%
)
echo #3 %var%

It outputs

#1 origin
#2 origin
#3 new value

As at parse time #2 will be expanded to origin before any line of the block is executed. So you can see the new value just after the block at #3.

In contrast, delayed expansion is expanded for each line just before the line is executed.

setlocal EnableDelayedExpansion
set var=origin
echo #1 %var%, !var!
(
  set var=new value
  echo #2 %var%, !var!
)
echo #3 %var%, !var!

Output

#1 origin, origin
#2 origin, new value
#3 new value, new value

Now at #2 you see two different expansions for the same variable, as %var% is expanded when the block is parsed, but !var! is expanded after the line set var=new value was executed.

More details about the batch parser at SO: How does the Windows Command Interpreter (CMD.EXE) parse scripts?



来源:https://stackoverflow.com/questions/17265882/parse-time-vs-execution-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!