How can I make Perl and Python print each line of the program being executed?

怎甘沉沦 提交于 2019-11-27 00:27:02

问题


I know that bash -x script.sh will execute script printing each line before actual execution. How to make Perl and Python interpreters do the same?


回答1:


Devel::Trace is the Perl analogue, the trace module is Python's.




回答2:


Devel::DumpTrace was released in 2011 and has more features than Devel::Trace, such as evaluating variable values in the trace output.




回答3:


python -m trace -t main.py

Test program:

main.py

from a import g
def f(i):
    g(i)
for i in range(3):
    f(i)

a.py

def g(i):
    print i

Output:

 --- modulename: main, funcname: <module>
main.py(1): from a import g
 --- modulename: a, funcname: <module>
a.py(1): def g(i):
main.py(2): def f(i):
main.py(4): for i in range(3):
main.py(5):     f(i)
 --- modulename: main, funcname: f
main.py(3):     g(i)
 --- modulename: a, funcname: g
a.py(2):     print i
0
main.py(4): for i in range(3):
main.py(5):     f(i)
 --- modulename: main, funcname: f
main.py(3):     g(i)
 --- modulename: a, funcname: g
a.py(2):     print i
1
main.py(4): for i in range(3):
main.py(5):     f(i)
 --- modulename: main, funcname: f
main.py(3):     g(i)
 --- modulename: a, funcname: g
a.py(2):     print i
2
main.py(4): for i in range(3):
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

Tested on Ubuntu 16.10, Python 2.7.12.



来源:https://stackoverflow.com/questions/2872089/how-can-i-make-perl-and-python-print-each-line-of-the-program-being-executed

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