Hacker News new | ask | show | jobs
by ars 1582 days ago
What happens if you run this code in a loop Thursday night just before Friday?

Does the parse that happens on Thursday take precedence or is it reparsed every single time through the loop?

1 comments

The BEGIN block only runs once. That is:

  while (1) {
    BEGIN {print "hello\n"}
    sleep 1;
  }
Will only print "hello" one time.

You could loop inside the BEGIN block and then drop out of the loop at some point. If you dropped out on friday, after the code assigning *f, it would run correctly. So:

  BEGIN {
       sleep 86400;
       *f = (localtime->wdayname eq 'Fri')
        ? sub() {}
        : sub {};
  }
  f/1;#/+
Would run correctly if you started it on Thursday.