Hacker News new | ask | show | jobs
by tyingq 1582 days ago
>incorrect code raises syntax error only when it is reached by execution.

That's not generally true for Perl. The BEGIN block is used to get in that state here. "Some incorrect code raises syntax error only when it is reached" is true.

It's generating this on Fridays:

  &f() / 1;
And this on other days:

  f(/1;#/+);
If you run the same code, but without BEGIN blocking the assignment to *f, it isn't incorrect code. It evaluates as:

  'f' / 1;
1 comments

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?

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.