にゃごすwiki

スクリプト関連/2009-6-12

  dofile の位置に注意

dofileする位置によって、定義される場所が違うぞってことです。
以下の例の場合、inc1.nutで定義されるのは、aaaですが、inc2.nutで定義されるのは
test.aaaになります。気をつけないと、思わぬバグに遭遇する羽目になるので、注意しましょう。

inc1.nut

aaa <- 10;

inc2.nut

aaa <- 20;

test.nut

dofile( "inc1.nut" );
test <- {
  function main()
  {
     print( aaa + "\n" );
     dofile( "inc2.nut" );
     print( "inc2.nut execute\n" );
     print( aaa + "\n" );
  }
}

test.main();

print( aaa + "\n" );
print( test.aaa + "\n" );
test.clear();
print( aaa + "\n" );
print( test.aaa + "\n" );

実行結果

10
inc2.nut execute
20
10
20
10
AN ERROR HAS OCCURED [the index 'aaa' does not exist]

CALLSTACK
*FUNCTION [main()] test10.nut line [19]