3 ms·
Perl's Undefined Behaviors
- jerf 4y ago"my $foo = 0 if 0" I was stuck on a version of perl at work that could crash if you did that. If the clause was false, it did something like create the internal C-level variable for that variable, but never initialized it to anything, resulting in a true crash-the-process segfault if you ever looked at that variable again. That was fun to track down, and resulted in a slow trickle of trying to explain to a new programmer why they shouldn't do that. I don't think it reads terrible well anyhow. And I don't mean that as a postfix condition argument; I have opinions about that, but I don't mean that argument. I just mean "create this variable conditionally" doesn't really make a lot of sense under Perl's model and while I learned what happened when the clause was false, I never felt like that was really obvious.
- thyrsus 4y agoWhat would motivate such an expression? Was the intent that the variable have value undef in the false case and 0 in the true case?
- thyrsus 4y agoIs that all the undefined behaviors? If so, it's an incredibly short list. It must exclude all the accessible undefined OS behaviors.
- wahern 4y agoI think the truncate undefined behavior example is an example of OS behavior. On POSIX systems specifying a size greater than the current file size extends the file; it's well-defined[1]. So, presumably, Perl targets some other, non-POSIX operating systems where doing so is undefined. [1] "If the file was previously shorter than length, its size is increased, and the extended area appears as if it were zero-filled." https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html https://pubs.opengroup.org/onlinepubs/9699919799/functions/t... EDIT: Ah, it's VOS: "(VOS) Truncation to same-or-shorter lengths only." https://perldoc.perl.org/perlport#truncate https://perldoc.perl.org/perlport#truncate