news.volyx.in

Perl code that is syntactically correct only on Fridays (github.com)

466 points by pabs3 · 1673 days ago · 223 comments on HN

Article summary

The article presents a Perl code snippet that is syntactically correct only on Fridays. This is achieved using Perl's BEGIN block, which runs before the main part of the code. The code leverages the day of the week to conditionally compile different code, resulting in a syntax error on non-Friday days. The article showcases the unique properties of Perl's parsing and compilation process.

Main themes

  • Perl syntax and compilation
  • Conditional compilation and meta-programming
  • Time zones and time standards
  • Programming languages and features
  • Error handling and testing
  • Code complexity and maintainability

What commenters say

  • Some programming languages, like Perl and Common Lisp, allow for code to be conditionally compiled or run at compile-time, enabling unique constructs like the Friday-only syntax.
  • The use of macros and conditional compilation can lead to complex and potentially error-prone code, but also offers powerful possibilities for meta-programming.
  • The distinction between time zones and time standards, such as GMT and UTC, is crucial in programming and can lead to subtle errors if not properly accounted for.
  • The Perl code snippet is an example of a syntax error that only occurs at runtime, due to the conditional compilation and execution of the code.
  • Other languages, such as Ruby and Pike, also have features that allow for similar constructs, but with different approaches and limitations.
  • The discussion highlights the importance of careful programming and testing to avoid errors and unexpected behavior, particularly when dealing with time and date-related code.
  • The use of BEGIN blocks and conditional compilation can be useful for certain tasks, but also requires careful consideration of the potential consequences and edge cases.