this post was submitted on 12 Apr 2025
196 points (90.2% liked)

Technology

69154 readers
3140 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] sugar_in_your_tea@sh.itjust.works 3 points 1 week ago (1 children)

You get the length of the stringified length of the array. And a warning if those are enabled.

That's absolutely cursed.

[–] palordrolap@fedia.io 1 points 1 week ago (1 children)

Well, you see, Perl's length is only for strings and if you want the length of an array, you use @arrayname itself in scalar context.

Now, length happens to provide scalar context to its right hand side, so @arrayname already returns the required length. Unfortunately, at that point it hasn't been processed by length yet, and length requires a string. And so, the length of the array is coerced to be a string and then the length of that string is returned.

A case of "don't order fries if your meal already comes with them or you'll end up with too many fries".

[–] sugar_in_your_tea@sh.itjust.works 1 points 1 week ago (1 children)

the length of the array is coerced to be a string and then the length of that string is returned.

it's the string coercion that I have a problem with. I'd much rather have an error than have things silently be coerced to different types.

[–] palordrolap@fedia.io 2 points 1 week ago

Perl was originally designed to carry on regardless, and that remains its blessing and curse, a bit like JavaScript which came later.

Unlike JavaScript, if you really want it to throw a warning or even bail out completely at compiling such constructs (at least some of the time, like this one) it's pretty easy to turn that on rather than resort to an entirely different language.

use warnings; at the top of a program and it will punt a warning to STDERR as it carries merrily along.

Make that use warnings FATAL => "syntax"; and things that are technically valid but semantically weird like this will throw the error early and also prevent the program from running in the first place.