|
Hammerite posted:This sounds extremely bad and needlessly confusing, and likely to be a huge source of bugs. It seems to me that it would be much better to have the user explicitly indicate what sort of behaviour they want than try to be clever and let them change the program's behaviour subtly through context. Can someone explain why I'm wrong? You're not really wrong, but Perl's gimmick is preferring idiomatic shorthand convenience over almost everything else, and the differences are usually not subtle. There's very intentionally no consistency whatsoever in what the builtin functions that can return lists do in scalar context - the documentation uses the (in)famous phrasing "in general, they do what you want, unless you want consistency". localtime() returns a string representation instead of a C-like time struct if used in scalar context. map() - which is more like what many other languages would call flatMap since each input element can map to any number of output elements, and in fact even to zero elements - returns the number of elements of the resulting list rather than the list itself. Some of the file I/O functions like readline() and readdir() return the next entry and advances the filehandle pointer in scalar context, while using them in list context reads until EOF and returns a list of files/lines/whatever, so they're stateful in one context but not really in the other. splice() - which splices like an array like Array.splice() in JavaScript - returns the last element removed in scalar context, or undefined if no elements were removed. sort() in scalar context has undefined behavior. In Perl's defense though it's usually not hard to see what the context is because of the sigils that prefix variables - scalars are prefixed with $, arrays with @. You declare the context with the sigil, like Perl code:
Perl code:
Another example of this focus on idioms is the return values from low-level syscalls that return 0 or a positive integer on success but a negative value on failure. In languages with modern exceptions you'd write something like code:
Perl code:
Perl is full of little weirdnesses like this and that's why it's fun. It's a very old language, which shows, and it has wildly different design goals from basically any language that's in common use today, but that's part of the charm. Unlike some of the more obnoxious languages like PHP or JavaScript that are full of weird and annoying issues because of historical mistakes, Perl isn't weird by accident - it was very intentionally designed to be weird. It was the first language I learned back in the day and I have quite a bit of fondness for it. Wouldn't want to work with it, though. TheFluff fucked around with this message at 17:41 on Jan 9, 2020 |
# ¿ Jan 9, 2020 17:11 |
|
|
# ¿ May 12, 2024 22:56 |
|
Man, writing that up made me remember how hilarious Perl is. Did you know that substr() can be used as the left hand side of an assignment?Perl code:
|
# ¿ Jan 9, 2020 17:47 |
|
Ape Fist posted:Isn't Perl supposed to be, like, profound unpleasant to write? Where did you get that idea? It's completely the opposite. Perl is loving hilarious to write, you'll sit there giggling to yourself about whatever cleverness you might've come up with. It can be really annoying to read though because there are so many ways to express the same thing and there are many subtleties hidden everywhere.
|
# ¿ Jan 19, 2020 19:18 |