Live data from Hacker News

Perl code that is syntactically correct only on Fridays

github.com

11–20 of 233 posts

Re: Perl code that is syntactically correct only on Fridays

#11
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.

Re: Perl code that is syntactically correct only on Fridays

#12
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.

If you tried, you couldn't even come up with this.

Re: Perl code that is syntactically correct only on Fridays

#13
post #6

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 written code (at work) that crashes when the moon is in a specific phase. I had a "switch(moon_phase)" and forgot one of the cases... And the Android app was in debug mode, so any uncaught exception brought the whole thing down.

Not nearly as cursed as two parent comments, but there was a widespread problem in my very popular Android app. The relative date formatting function worked incorrectly for many users, it was off by one hour. It turns out that when Russia (where most of our users were) had abolished DST, and then changed timezones a couple of times, people got really confused. Since Android's timezone data is part of the system, it was basically set in stone. Most of the users didn't choose the timezone with the correct offset, but instead set their time an hour forward so the incorrect timezone and incorrect unixtime cancel each other out for a "correct" displayed time.

I ended up getting the known correct unixtime from the server to try to guess which timezone offset the user actually meant and correcting the result of System.currentTimeMillis() and my formatting for that.

Re: Perl code that is syntactically correct only on Fridays

#14

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

Re: Perl code that is syntactically correct only on Fridays

#15

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.

As a part of a release always got borked, I wrote a small bash script that took currently deployed files, archived them to rollback_dateandtime.tgz, then put the freshly released files in its place.

It turns out we never did a release before 10:00, because it ran fine for a few years, but the regex constructing the filename could not deal with hours consisting of 1 number.

Oh well, never write production shell scripts while your platform is burning.

The fix is literally 1 character, but I only get access when production burns. So I was not allowed to change it and the owner does not want to implement it and file all release paperwork it entails. Instead, mgmt forbid this type of release before 10:00.

Re: Perl code that is syntactically correct only on Fridays

#16

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

Re: Perl code that is syntactically correct only on Fridays

#17
post #9

Somewhat related: Perl Cannot Be Parsed: A Formal Proof (2008) https://news.ycombinator.com/item?id=5770531

Well, to be a little pedantic, Perl cannot be statically parsed, since some constructs require runtime context.

If it could not be parsed it could never work!

And this is totally OK in my book, I use Perl for prototyping ideas and an expressive language that allows creativity from me the programmer is a feature as it promotes looking at a problem from multiple aspects.

Re: Perl code that is syntactically correct only on Fridays

#19
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.

Right, the beauty of this is that it's a syntax error he's produced. Sure any language can write code to produce a runtime error under any condition at all. And a lot of languages have features that allow them to run code at compile time, and similarly produce errors then too. But those languages don't allow you to ruin the syntax of the language. I guess it's something that requires macros. Would it be safe to say that any language with macros can do this? Or is there something even more interesting with perl going on here?

Re: Perl code that is syntactically correct only on Fridays

#20

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

Yeah it’s horrible. I had a contract a number of years ago to fix a bug that they had been fighting for 5 years. When winter time kicked in all appointments on their booking system collapsed to zero minutes long.

To fix this they ran a SQL script that changed the end date on the DST switch over but that had some poor assumptions in it and made it worse.

It also had to be correct in the past and the future.

I just rewrote the whole fucking thing from scratch.

Post reply on HN