Live data from Hacker News

Perl code that is syntactically correct only on Fridays

github.com

21–30 of 233 posts

Re: Perl code that is syntactically correct only on Fridays

#21

Earlier quoted context omitted.

> I have written code that only works in winter because of daylight saving times and some unfortunate date conversions Not sure where you are located, but this can be particularly bad in the UK where winter time happens to coincide with UTC.

Let's be pedantic about this, because this is one of the very few areas where pedantry matters: GMT ("British Winter Time") is NOT UTC, and it can differ from UTC up to 0.9 seconds. GMT is a time zone (corresponding to UT1). UTC is a time standard.

To be pedantic, this is an area where pedantry occasionally matters. For the vast majority of (casual) usage, it doesn't.

Re: Perl code that is syntactically correct only on Fridays

#22

Earlier quoted context omitted.

> I have written code that only works in winter because of daylight saving times and some unfortunate date conversions Not sure where you are located, but this can be particularly bad in the UK where winter time happens to coincide with UTC.

Let's be pedantic about this, because this is one of the very few areas where pedantry matters: GMT ("British Winter Time") is NOT UTC, and it can differ from UTC up to 0.9 seconds. GMT is a time zone (corresponding to UT1). UTC is a time standard.

Ok, fair enough. But it's close enough that it's very easy for anything that isn't working with second-level granularity to seem like they're working only to break as soon as the time switches BST.

Re: Perl code that is syntactically correct only on Fridays

#23

I have written code that only works in winter because of daylight saving times and some unfortunate date conversions, but having a temporary correct syntax is really a new high.

I have once heard of some software that would crash on some Wednesdays. Apparently Wednesday is the longest day name, and it was overflowing some buffer. But only if the day of the month was 10 or above, because the strings 1-9 still fit in the buffer.

Re: Perl code that is syntactically correct only on Fridays

#24
post #4

This leverages Perl's BEGIN block which runs before the main part of the code. I suppose the same fun could be had in other languages: if day_of_week is "Friday" eval(piece_of_incorrect_code) else do_sane_operation

Most other languages would require `eval` and having incorrect code as syntactically correct string literal. But because Perl is parsed as it is executed, incorrect code raises syntax error only when it is reached by execution.

>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;

Re: Perl code that is syntactically correct only on Fridays

#26
post #10

I had an intern once who wrote similar code. They confused 12 hour and 24 hour clocks, so the code would break after lunch. Unfortunately, the intern only worked the mornings, so it took several days of back and forth before the bug could be finally put to rest.

Fantastic. We had a similar problem with test code that always worked on a remote team's computers during their work day but failed when we got into the office and tried to run it in a different timezone.

Re: Perl code that is syntactically correct only on Fridays

#27
post #10

I had an intern once who wrote similar code. They confused 12 hour and 24 hour clocks, so the code would break after lunch. Unfortunately, the intern only worked the mornings, so it took several days of back and forth before the bug could be finally put to rest.

That's standard runtime issue right? This is syntax error at compile time.

Re: Perl code that is syntactically correct only on Fridays

#28
post #21

Earlier quoted context omitted.

Let's be pedantic about this, because this is one of the very few areas where pedantry matters: GMT ("British Winter Time") is NOT UTC, and it can differ from UTC up to 0.9 seconds. GMT is a time zone (corresponding to UT1). UTC is a time standard.

To be pedantic, this is an area where pedantry occasionally matters. For the vast majority of (casual) usage, it doesn't.

Indeed. I get invites from people from the UK who simply refer to the current local time (GMT or BST) as "GMT". It just means current London time to them.

Re: Perl code that is syntactically correct only on Fridays

#29

Pro tip: add test cases to your CI runs with modified system date.

The thing is, you need to have a pretty corkscrew mind to think about cases like this.

What date to change to? Far into the future? Far into the past? To a weekend? To a DST change? To Friday the 13th? To a US holiday? To a Muslim, Jewish, Chinese, Soviet, Albanian holiday? To a particular weekday? To Hitler's birthday? What if it's Lenin's? To that one day my coworker was depressed because the boss denied him a raise and talked shit to him? Too many possibilities. And your timebomb might be in one of your dependencies to boot.

Re: Perl code that is syntactically correct only on Fridays

#30

Why only things like these float up to the front page, and not elegant and/or wholesome things written in Perl?

Perl is like life - it contains both good and bad.

And good things in Perl don't get noticed because they Just Work.

Post reply on HN